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

Enable body before colliding, how?

Sep 29, 2008 at 10:44 AM

Im trying to remake the old game Motherload made in flash, and i want to make an underground that can be "digged" into by a digger machine. I figured that it would work by creating alot of bodies like 48x48 and the "pod" would be somewhat the same size, though a little bit smaller. In order to create an underground, i would have to make like 25 x (x down) blocks of bodies. That would oviously slow down the simulator, so i figured i could just disable ALL underground bodies and only enable those which are close to the pod all around it.

In order to do so i would have to make an calculating method which always checks for the nearest bodíes (dirt blockes) around the pod. How could i make such method?

How's that sound? any better ideas?

Sep 29, 2008 at 11:11 AM
You could make a large AABB (Axis Aligned Bounding Box) around the pod and check for intersections with the other AABB's around it.
Then check every update to disable/enable bodies.

foreach (Body body in GroundBodies)
    if (largeAABB.Intersect(body.AABB)
        body.Enabled = true;
        body.Enabled = false;

Remember to test it and see if it gives better performance. There might be a better way :)
Sep 29, 2008 at 11:29 AM
How do i create such AABB? can i give me a simple example?
Sep 29, 2008 at 12:10 PM
where do i find any documentation on how to use AABB?
Sep 29, 2008 at 12:32 PM
Edited Sep 29, 2008 at 12:34 PM
AABB's is really simple. XNA does also have the ability of creating an AABB, but Farseer uses it's own.

When you create a Geom, a AABB is also created for you. The AABB is a square that defines the edges of the Geom. AABB means Axis Aligned Bounding Box, the AA part means that the BB part does not rotate, but is axis aligned all the time.

You can use AABB's to check if 2 elements are touching or intersecting each other.

If you turn on the debug view (press F1) in the Farseer samples, then you can see the AABB around the Geoms.

I have a screenshot of it in action here and here (in the second one, you can see them intersect.)
Sep 29, 2008 at 12:38 PM
but how do i make the AABB for my pod larger than the pot itself, so it will detect collision before the actual pod is?
Sep 29, 2008 at 1:04 PM
Nevermind i got it, works perfectly! ill test the performance now
Sep 29, 2008 at 1:18 PM
You beat me to it.

Could you post your solution when you get it working? Others might need the same :)
Oct 1, 2008 at 12:23 AM
Yeah I would like the solution to this, because this thread gave me an idea to increase performance and precision.  When I try to do it there seems to be no change when I look at the PhysicSimulatorView.  To change it I do something like:

Vertices biggerRect = Vertices.CreateRectangle(15, 20);
geom.AABB.Update(ref  biggerRect);

That doesn't work and neither does this:

geom.AABB = new AABB(Vertices.CreateRectangle(15, 20));

What am I doing wrong?  There aren't any syntax error because it compiles (I didn't copy paste), and geom is intialized beforehand.
Oct 1, 2008 at 12:50 AM
You don't need a geom to have an AABB.

I think this is what I did:

Vertices vertices;
AABB aabb = new AABB(new Vector2(0,0), new Vector2(100,100));
vertices = aabb.GetVertices();

and in update:
//Update the position of the AABB
Vertices.Translate(new Vector2(10,10));
aabb.Update(ref vertices);

That's from the top of my head, don't have a compiler right now, so it might not work.

Also, another way of doing it is calculating the distance to the pod on each of the bodies position. You could use GetNearestDistance() of the Geom class, but that tests each vertex of the Geoms, so doing it manually would be the best:

private Vector2 _distance;

//In update method:
_distance = Vector2.Subtract(circleBody.Position, rectangleBody.Position);

float length = _distance.Length();

If you do it like that, you need to make sure to take the size of the Geom you test it against into account. Example: If you test the length between 2 bodies and they are right next to each other, the length will equal: Geom1Size/2 + Geom2Size/2.

Try it out. I would really like to know if this works out.

Oct 1, 2008 at 9:46 PM
I actually want to have a Geom I just want to make the AABB bigger so there is more narrow phase collision.  Except when I try it, it doesn't seem to work.
Oct 6, 2008 at 3:36 PM
@SomethingNew - Sorry, I must have missed your post.

The reason you can't put a bigger AABB onto a geometry is that the AABB get's updated to match the vertices of the geometry. I should remove the setter from AABB since it's not working anyway.

AABB's need to be really fast, so it's a lightweigt class, but I could make it possible to adjust the AABB so that it get's some padding around the box.
Oct 6, 2008 at 11:45 PM
Thanks for your help, also could you add a little padding to the circle geom too.
Oct 6, 2008 at 11:48 PM
Add padding to circle geom? Could you explain it in a little more detail?
Oct 7, 2008 at 1:23 AM
Well, maybe I misunderstood you, but I took "padding around the box" as the Rectangle Geom.  But if you meant "padding around the box" as AABB then that would be universal for all Geoms.  This feature of being able to add padding to extend an AABB, I'm sure will be a big help to alot of people who want to increase performance and precsision.
Oct 7, 2008 at 2:04 AM
arh okay. I meant padding the AABB so that it's a little larger than needed. This should not be applied to all geoms as it would make performance worse. The broad phase collider uses the AABB to detect if other geoms are close. If they are close, an Arbiter is constructed and then used in narrow phase collision detection. The more arbiters constructed equals less performance. The narrow phase is an expensive operation.

Adding some padding to the AABB so that the size of it would grow a little, would make the narrow phase check more often between the geom and others near by. It would also enable Arextion to check if his pod is standing on the ground (thread here) without having problems because of the jitter.

It should only be used for special cases like his, I'm not sure if it should be in Farseer yet as it might hurt overall performance to even check if the padding is enabled. I have written it down on my TODO list and will take a look at it later.