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

Best way to cause angular rotation from an explosion

Topics: Developer Forum, User Forum
Aug 15, 2009 at 2:27 PM
Edited Aug 15, 2009 at 3:07 PM

I've been playing with the explosion/grenade demo (demo 7 of advanced) and I want to apply a grenade explosion effect to long thin bodies (like planks of wood) instead of the small square blocks in the demo.  The desired effect is that if there is a vertical plank of wood, and the grenade goes off eg near the top left, then the plank will spin away with a clockwise rotation.

From reading other threads, it seems that its pretty performant to cast out some lines from the explosion centre, and if the line touches a body I apply the explosion effect.  So here I guess I should use CollisionHelper.LineSegmentAllGeomsIntersect().

Assuming my reasoning above is correct, my question then is what should I call to apply the explosion's effect.  Should it be ApplyAngularImpluse?  ApplyTorque?  ApplyForceAtLocal/WorldPoint?

Thanks for any advice.


Aug 15, 2009 at 3:00 PM

If you apply the explosion like in the demo using a AABB, then you will also get the desired effect. It depends on area of explosion, magnitude and placement.

If you use rays, you cast them from the center of explosion and outwards. Whenever you hit a geometry you also get the point of intersection. You simply apply force (or impulse for a stronger effect) in the direction of the ray. Use ApplyForceAtWorldPoint() to do that - the geometry will automatically spil from the uneven distribution of force on the plank.

Aug 15, 2009 at 3:38 PM

Thanks genbox, yes I see the AABB already does give the spin effect when the plank is hit.

I see my problem now is that because the plank is very long, the explosion touches just the end of the plank, and the plank.Position is not inside the AABB.  So my next noob question is, how best to determine if any of my bodies/geoms lie even partially inside the AABB of the explosion?

Would I try that ray approach, or is this where I would setup a new geom as a sensor (I've read about these but not used them).  I know how I would approach this if I were doing it outside Farseer, I'd just loop through and use rectangles and say if rect.intersects the explosion rect, but I'd like to do things properly and not re-invent the wheel.

Aug 16, 2009 at 12:25 AM

To determine if the plank is inside or partially inside the AABB, you simply check each point of the geometry up against the AABB. You can use the aabb.Contains(Vector2 point) method for this. Remember to use the geometry.WorldVertices when checking.

As for the rays, there is a CollisionHelper class that can help you do line-to-line (and to AABB, and to geom) intersections. Take a look at it and see if you can use any of it.

Aug 16, 2009 at 3:50 AM

Aug 16, 2009 at 7:56 PM

Great, very useful thanks both for the suggestions.