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

Relations of Positions and Using Farseer w/o Physics

May 21, 2009 at 4:49 AM


I'm currently working on a 2D Fighting game for PC (using XNA).  Most games like that check to see if an attack hits an opponent by running collision detection on the hitbox(es) of the attack against the hurtbox(es) of the opponent.  I am dissatisfied with the conventional use of only rectangles to describe these collision shapes that pretty much EVERY 2D fighting game uses (I'm spoiled by Amazing fighting games like Melee), and wanted more flexibility in shape; hence why I'm choosing the best thing out there for that sort of thing: Farseer.  I've got two questions regarding implementing Farseer for my needs.

Question 1:

I believe it would be most appropriate for me to only use Farseer for collision detection.  Which settings should I set to ensure that all that gets done is Collision detection, and what sort of obvious optimizations can I do to make it perform better?  Note that for my design no geometries remain constant from one frame to the next (i.e. their shapes change; speculatively I'll need to pre-instantiate all these geometries and add/remove them from the PhysicsSimulator on a per-frame basis as necessary).  With this being the case, all I'd need from Farseer is to check to see if any Geoms overlap and let me know which Geoms overlap, and let me take care of the rest. 

I beleive that I'm supposed to set the CollisionResponseEnabled = false on all geometries.  It seems to me like there's also something that can be done about the itteration count per update (since nothing's "moving" it doesn't need to make sure nothing penetrates).  How am I supposed to call update()?  would it matter what dt I pass?  In this usage, what do Bodys do?  They seem pointless, unless they help to solve my second question.


Question 2:

I need to describe arbitary polygons (vertex based of course) as Geoms, but what I'm unsure about is how the coordinates of the verticies line up to anything.  Are the coords of the verticies relative to the "Position" of the "Body"?  For instance: Say I want to describe the hitbox of a sword in front of my character.  Say I set the position of the "Body" (that only serves to represent the hitbox) to the upper left corner of the Sprite for the swordsman.  Would using a rectangle that goes from (I'm assuming Farseer treats the Upper Left quadrant as (-,-) and the lower right quadrant as (+,+); does it?) <50,50>-<100,200> for it's Geom allow me to just move the body around where the sprite is, and have the hitbox be out to the lower right of the draw coords?


Thanks for any help! I'll Greatly appreciate any, because I really hope this works out well

May 21, 2009 at 1:15 PM

1. If you need no physics at all, you can set the IsSensor property on the geometry to true. This makes it static (not movable by forces or impulse) and make it not respond to collisions around it.

As for performance, the default number of iterations is 5 - since your game by design does not need the physics reactions, it does not matter if you set this to 0 or 10000. The engine does not process geometries that does not react to collisions.

In the update method, you pass the time since last update. The samples use gameTime.ElapsedGameTime.Milliseconds * .001f as argument for dt (delta time).
Bodies are the ones responsible for forces and some physics properties. They are needed to move geometries and make them react.

2. The vertices should be relative to (0,0). Your body's center will be positioned at (0,0), once you change the position, the vertices will get translated to be the correct place. They should also be in counter-clockwise order (the engine will correct them if you don't). Farseer Physics use the center of bodies/geometries as the position.

May 21, 2009 at 6:29 PM
Edited May 21, 2009 at 6:35 PM

Thanks for the quick response!

I'm still a little unclear on the relative positioning (for question 2).  You say that the verticies should be relative to (0,0), and the body's center will be positioned at (0,0).  So basically, the coordinates that you specify for creating a Geom are just "offsets" from the Body's position.  So if there were some geom that was just a circle of radius 10 centered at (100,0), it would always be out to the right of wherever i set it's body's position to, right? 


Edit: I've seen some stuff about people having positioning issues with center of mass.  If I defined some polygon with coordinates relative to (0,0), it doesn't move afterwards to set it's centroid to (0,0), does it?  It'd be rather imperitive that it keeps it's position relative to the body position (as opposed to the centroid being at the body's position), becaues the geometries I'm working with will never care about their center of mass; i just need them to be relative to where the characters are (in the way that I defined them).

May 21, 2009 at 9:56 PM

There are some factories (GeomFactory) that helps you creating something like a circle. Since all shapes in Farseer Physics are polygons, creating a circle is just an approximation of a circle. As I wrote before, all vertices must be centered around (0,0). A rectangle would be like this:

Topleft: -5,-5
Bottomleft: -5,5
Bottomright: 5,-5
Topright: 5,5

As for the positioning; After you have created a polygon geometry, the factories will automatically translate all the vertices to be centered around the centroid instead of (0,0). This is better for the engine. There is a section in the manual about this.

Yobiv has also created this info about it.