How to register the collision?

Topics: Developer Forum
Mar 23, 2011 at 1:36 PM

I am making progress with Farseer.

I have a player dynamic body and a static terrain body object made from an image. Running the game just makes the player body object fall into nothing passing through the terrain block. How do I make the sprite land onto the terrain? Or is it supposed to happen anyway in which case i'm missing something?

Any help is appreciated :)

 

Jamie.

 

Mar 23, 2011 at 5:56 PM

Check the documentation.  They have an example of it.  You can assign a collision event function to your objects.  It's pretty straight forward.

Mar 24, 2011 at 2:47 AM

Bodies should visibly collide with each other without extra code. Only if you need to be notified of particular collisions do you need to add collision event code.

My guess is your texture is being drawn at a different scale than the object you've generated from it so they don't match up on the screen. Play with the ConvertUnits() helper function.

Mar 24, 2011 at 8:03 AM

Ok, I still can't get it to work properly. Here is my code, the code is basically the same code as the samples project code from "AdvancedDemo1"(the polygon from image demo) combined with "SimpleDemo9" (Boxes on ramps) used to create a retangle body:

//SETUP TERRAIN POLYGONS
uint[] data = new uint[MapTestImage.Width * MapTestImage.Height];
MapTestImage.GetData(data);
Vertices textureVertices = PolygonTools.CreatePolygon(data, MapTestImage.Width, false);
Vector2 centroid = -textureVertices.GetCentroid();
textureVertices.Translate(ref centroid);
Origin = -centroid;
textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 4f);
List<Vertices> list = BayazitDecomposer.ConvexPartition(textureVertices);
Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * MapScale;
foreach (Vertices vertices in list)
{
    vertices.Scale(ref vertScale);
}
Compound = BodyFactory.CreateCompoundPolygon(BGWorld, list, 100f, BodyType.Dynamic);
Compound.IsStatic = true;
Compound.CollidesWith = Category.All;

PlayerBody = BodyFactory.CreateRectangle(BGWorld, 100, 64, 35, ConvertUnits.ToSimUnits(new Vector2(0, -15)));
PlayerBody.BodyType = BodyType.Dynamic;
PlayerBody.Friction = 1.75f;
PlayerBody.Restitution = 0.8f;

 

Then, in Draw (following the same method as the project):

//In separate collision class:
Batch.Draw(MapTestImage, ConvertUnits.ToDisplayUnits(Compound.Position),
                                           null, Color.Tomato, Compound.Rotation, Origin, MapScale, SpriteEffects.None,
                                           0f);


//In main draw class:
Batch.Draw(spriteAnimation.SpriteImage, ConvertUnits.ToDisplayUnits(farseerCollision.PlayerBody.Position), spriteAnimation.Frame,
                Color.White, farseerCollision.PlayerBody.Rotation, new Vector2(50, 60), 1, spriteEffect, 1);