BodyFactory.CreateRectangle

Topics: Developer Forum, User Forum
Sep 20, 2012 at 12:56 PM
Edited Sep 20, 2012 at 12:58 PM

Hi

I am developing for windows mobile platform.

Im creating my BodyFactory.CreateRectangle around my floor line texture that is drawn on the screen. position starts at at vector2(100,400)

But it seems that the BodyFactory.CreateRectangle around this image starts before position vector2(100,400)  and ends halfway across my floor line). I have tested it with random sprites falling.

Am i missing a setting. Heres my code using the floor object of my sprite class

 Its like my world is slightly out.

Thanks

 

            floor.Load("floor");
            floor.origin = Vector2.Zero;
            floor.alive = true;
            floor.position =  new Vector2(100, 400);            
floor.farseerRectangle = BodyFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(floor.image.Width), ConvertUnits.ToSimUnits(15), 0.001f); floor.farseerRectangle.BodyType = BodyType.Static; floor.farseerRectangle.Position = ConvertUnits.ToSimUnits(floor.position); floor.farseerRectangle.IsStatic = true; floor.farseerRectangle.Restitution = 0.15f; //Bounceability floor.farseerRectangle.Friction = 0.05f; floor.farseerRectangle.IsSensor = false; floor.farseerRectangle.Rotation = alpha;
Sep 20, 2012 at 6:15 PM

It's probably how you draw things. For XNA 0,0 is top left corner. For Farseer it's geometrical center of the rectangle (width/2, height/2). You should correct for that when calling SpriteBatch.Draw(). It's strange you don't have problems with rotation - they would be very different as they are around a different pivot point.

Sep 21, 2012 at 12:51 PM

Thanks for the reply.

I took what you said and applied it to my code, which works well. But like you said rotaions gives a problem as the farseer objects origin is in the middle of the object and my origin for the image is 0,0.

What i would like to do, is draw a line between 2 points that the user touches on the screen. I can do this by have a 1x1 pixel tecture and then rotate and give the width as the length. If you follow what i mean, but i cant rotate the farseer object around this as it rotates from its origin which is always the middle.

Is there any function in farseer that lets you draw from point x,y to x,y.

Thanks

 

Sep 22, 2012 at 9:32 AM

Farseer does not draw anything it just makes calculations. Your issue is more XNA related. The DebugViewXNA is just for debugging (it also is not very efficient). But you can easily adapt the XNA coordinate system by providing the origin parameter when you call SpriteBatch.Draw (midpoint of your rectangle) so they will be in sync. There are also libraries for more fancy drawing like these:

http://shapes.codeplex.com/

http://roundline.codeplex.com/

Also the Nuclex framework http://nuclexframework.codeplex.com/ has a primitive batch similar in spirit to the SpriteBatch.