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

Immediately Stop an Objects Motion

Topics: User Forum
Oct 25, 2009 at 12:44 AM

This is probably very simple, but i can't find out how to do it for the life of me. I want to detect when 2 square objects collide, but only one will have actual motion. When the collision is detected, i want to stop the motion of the first. I am using ApplyForce() and it is tied into the accelerometer on my Zune HD. I am using AABBs to detect collisions, since the objects are all rectangular. Here is my collision code:



if ((AABB.Intersect(boxGeom.AABB, topGeom.AABB) || (AABB.Intersect(boxGeom.AABB, botGeom.AABB)) || (AABB.Intersect(boxGeom.AABB, leftGeom.AABB)) || (AABB.Intersect(boxGeom.AABB, rightGeom.AABB))))

if ((AABB.Intersect(boxGeom.AABB, topGeom.AABB) || (AABB.Intersect(boxGeom.AABB, botGeom.AABB)) || (AABB.Intersect(boxGeom.AABB, leftGeom.AABB)) || (AABB.Intersect(boxGeom.AABB, rightGeom.AABB))))



but either the collisions aren't being detected, or ClearForce() isn't the way to go. I have made sure that collision detection is enabled for all 5 objects. boxGeom is the only object that has motion. Any help would be appreciated, and thanks in advance!


Oct 25, 2009 at 5:25 AM


Think about sitting on a sled on an icy pond.  A force in this case would be your friend behind you pushing you.  The force has started you moving, and given you a VELOCITY.  The force stops as soon as his hands leave your back, but you would still coast until friction slowed your velocity down to zero.

The ResetDynamics() method on the body could be used to totally stop a body from moving, but it looks like it also sets the position back to 0,0.

I haven't tried it out, but I think you should be able to do what you want to do this way:


LinearVelocity = Vector2.Zero;
AngularVelocity = Vector2.Zero;

I wonder though, if you're not making this a little harder than it needs to be.  Part of the idea behind Farseer is that if you create two geometries, they will move realistically together, and report their collisions back to you.  The Geom class provides a collide() method that you can use to see if two geoms are colliding (which should be more accurate than using the AABB intersect), and also provides an onCollision event, which I haven't tried, but should be pretty easy to use.

If you don't want a body to move at all when a collision happens, you can set its IsStatic property to true.  You can set this on and off during the simulation, to "pin" and "unpin" objects in their position.

Make sure to check your collision categories, too, and try some different things, there.  I know it took me a few tries to convince my Geoms to collide properly.  Once you get your collisions set up right, the simulator should do a pretty nice job of moving and stopping the boxes for you, but the code above will probably get you pretty close to stopping them dead in their tracks, if you don't like how the simulation is handling it.


Oct 25, 2009 at 8:50 PM

Thanks for the tips on stopping it. Between the time i posted this and the time you replied i found out about the Geom Collide() function. It works, but it acts weird. My geom only wants to collect the collision with the top wall. Although this could be because i never knew that i could pin objects down using isStatic, and maybe my walls are moving. So i will try setting my walls to be static and then see what happens.

Oct 25, 2009 at 9:18 PM

Ok, i set the wall's isStatic properties to true. But now i have another problem. Collision on the top wall is detected fine, still, and all other walls detect collisions. But, all walls but the top wall tunnel, or sometimes the square just goes right through them. The top and bottom walls are 5 pixels in Height and 272 pixels in width. The left and right walls are 5 pixels in width and 480 pixels in Height. The top wall has never tunneled, no matter how fast the square moves. But the other three only detect at very slow speeds, and even then the object will get stuck on those walls. I don't know how to fix this

Oct 26, 2009 at 8:57 PM

Tunneling will happen at pretty slow speeds if the walls are only 5 px. Your problem with the 5x480 geometry might be because you are using the default grid cell size for the distance grid. Look at the GeomFactory.CreateRectangleGeom() method. It has an overloaded method that takes in a collision grid cell size. Try a low grid cell size and see if that works better.