Question on dimensions

Topics: Developer Forum, User Forum
Oct 2, 2008 at 5:42 PM
Hello all, I have a newb question. What are the dimensions that farseer uses for width and height when using the factories (eg. inches, pixels)?

I am learning the aspects of using farseer; I have a game that has a floor and a ball that falls to the floor; the screen size is 600x480. In order for the ball to hit the floor correcly I have to set the floor position to y=590, and the size has to be around x=1600.

eg:

groundBody =

BodyFactory.Instance.CreateRectangleBody(myPhysicsSimulator, 1600, 25, 10);

 

groundGeom =

GeomFactory.Instance.CreateRectangleGeom(myPhysicsSimulator, groundBody, 1600, 25);

 

groundBody.Position =

new Vector2(0, 590);

 

Oct 2, 2008 at 6:44 PM
Well, found one problem. I thought i was using 640x480. I am using 800x600. I was also forgot about the alpha portion of my floor texture. When i used spriteBatch.Draw() the position was using the origin of the texture, not the upper-left corner of the floor block. (that accounts for the pixel account error)

But the question still remains, why did my Body/Geom need to be width=1600? Does farseer use 2 "units" per pixel?

Thanks
Oct 2, 2008 at 7:12 PM
I think farseer uses pixels, so just one "unit" per pixel.

Check your origin is set up correctly, and that you're drawing the texture in the same place as the geometry.  It's easy to draw the texture in the wrong place, and watch things collide in unexpected ways.

Adman
Coordinator
Oct 2, 2008 at 7:26 PM
Edited Oct 2, 2008 at 7:27 PM
Welcome nash9000. Hope you like Farseer Physics Engine so far.

Farseer Physics Engine use the center of objects as position. When drawing your textures, you will have use tell it the correct origin:

*****************
//fields:
private Body _rectangleBody;
private Geom _rectangleGeom;
private Vector2 _rectangleOrigin;
private Texture2D _rectangleTexture;

//inside LoadContent()
_rectangleTexture = DrawingHelper.CreateRectangleTexture(ScreenManager.GraphicsDevice, 128, 128, Color.Gold,Color.Black);
_rectangleOrigin = new Vector2(_rectangleTexture.Width / 2f, _rectangleTexture.Height / 2f);
_rectangleBody = BodyFactory.Instance.CreateRectangleBody(PhysicsSimulator, 128, 128, 1);
_rectangleBody.Position = new Vector2(256, 384);
_rectangleGeom = GeomFactory.Instance.CreateRectangleGeom(PhysicsSimulator, _rectangleBody, 128, 128);

//Inside Draw()
ScreenManager.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
ScreenManager.SpriteBatch.Draw(_rectangleTexture, _rectangleGeom.Position, null, Color.White,
                                           _rectangleGeom.Rotation, _rectangleOrigin, 1, SpriteEffects.None, 0);
ScreenManager.SpriteBatch.End();
*****************

Notice how the origin is equal to half the height and width. So the reason that you have to make the ground 1600px is that your screen is 800px wide and you are only seeing the second half of the ground. Place the ground in the center of the screen (On X axis) to see it all.

EDIT: The code above is from the Farseer Physics Engine 2.0 Alpha XNA samples

Oct 2, 2008 at 7:56 PM
Ahhh! Thank you guys. Indeed if you look at my first post, you will see that i am putting the geometery at x=0! I changed the position to Vector2(400, 590) and it works perfectly.
 
I may be doing a crude form of programming, my ground texture gets drawn by a "for" loop each draw cycle, and calculate the floor Body/Geom without actually tying it to a texture like in the examples. (guess i could have created bodies/geoms for each block,...)

Yes, genbox and other devs. I really like the way Farseer Physics Engine looks so far. I am excited to start creating something with it. But I want to walk before I run. I just wish there was more documentation. I am hoping that once i get rolling, that I can create some tutorials for other newbs. (Demos included with the source were a great help!)

Good job.
Coordinator
Oct 2, 2008 at 8:44 PM
@nash9000

We have had a lot of requests for more documentation. Even in this moment, I'm writing a manual for Farseer Physics 2.0, It contains a surprise I can't reveal right now :) It will surely make Farseer Physics a lot easier to understand.

We are always happy to receive help from the community, we have a wiki here for users to contribute. Right now it only contains a very limited amount of information.