This project has moved and is read-only. For the latest updates, please go here.

Drawing a Body

Apr 26, 2009 at 3:52 AM
This probably an easy problem to solve for you guys, but I am new to game programming and C#. I have some experience with C++ but that is surprisingly different from #. Just to let you know, this is for XNA. I am trying to draw a rectangle (I made the body, geom and texture) by using spriteBatch.Draw with bodyName.Position as SpriteBatch.Draw's rectangle argument. I don't know what's up with this because bodyName.Position is a Vector2 and the argument (it gives the position of Draw) requires a rectangle. I tried to get around it by making a rectangle that used bodyName.Position.X and Y as the position arguments, and the width and height from the body as the others, but these are floats and it said they couldn't be converted to ints. What do I do to solve this (hopefully I wasn't too confusing).  Thanks in advance, I am very confused and frustrated with all the problems I am having (this is one I can't solve).
Apr 26, 2009 at 4:02 AM
Edited Apr 26, 2009 at 4:03 AM
Well the draw method has different overloads, some need a rectangle and others need a vector 2.
spriteBatch.draw(texture2d, vector2, color.white) will except a vector 2 or a rectangle and is the most basic draw call.
What you should use is the last overload
spriteBatch.draw(texture2d, vector2, source rectangle, color, rotation, origin, scale, spriteeffects, layerdepth)
for this you would suppy something like this
spriteBatch.draw(image, geom.position, null, color.white, geom.rotation, new vector2(image.width/2, image.height/2), 1f, spriteeffects.none, 0);
the only thing there that may not be self explanatory is the origin, that is an offset parameter for the image, the bodys position is according to its center so versus the top left for images, s you offset it half the area of the image.
Also for future reference, if you are doing a rectangle and it tells you it needs int you can say (int)position.x,
for instance
new Rectangle(position.x, position.y, (int)image.width, (int)image.height);
hope i helped
Apr 26, 2009 at 4:05 AM
Thanks, I just never noticed the scale argument.
Apr 26, 2009 at 4:34 AM
I know right! That happened to me as well a while ago,
luckily ive been through that situation so i could give you a quick easy answer,
over at the xna forums people were not so easy to get an asnwer out of.
Apr 27, 2009 at 10:06 AM
you should have a look at the demo apps as the provide drawing functionality for Farseer.
also keep in mind that a Body is just a 2D point defined by it's position, mass and MOI. again: a Body has no form, no extension!!! if you use CreateRectangleBody(), what you really doing is calculating the MOI from the width, height you specify. only Geoms have a form.