Finding the total force of a collision

Topics: Developer Forum, User Forum
Jul 22, 2010 at 11:36 AM
Edited Jul 22, 2010 at 3:46 PM
I have this working ok(ish) in my game at the moment, but i'm not fantastic at maths. When two primatives collide, I want them to smash up into tiny bits if the force applied to the primative was over a set threshold. My collision event handler at present looks like this. public bool Collision(Fixture fixtureA, Fixture fixtureB, Manifold manifold) { Vector2 position = manifold.LocalNormal; float angle = (float)Math.Atan2(position.Y, position.X); Vector2 force = Vector2.Zero; if (angle < 0) force = new Vector2((float)(Math.Cos(angle) * fixtureA.Body.LinearVelocity.X), (float)Math.Sin(MathHelper.TwoPi + angle) * fixtureA.Body.LinearVelocity.Y); else force = new Vector2((float)(Math.Cos(angle) * fixtureA.Body.LinearVelocity.X), (float)Math.Sin(MathHelper.TwoPi - angle) * fixtureA.Body.LinearVelocity.Y); double XForce = Math.Sqrt(force.X * force.X); double YForce = Math.Sqrt(force.Y * force.Y); double totalForce = XForce + YForce; if ((Breakable) && (totalForce > BreakForce)) { Breakable = false; Active = false; BreakUp(fixtureA, fixtureB); } return true; } I put that in a LONG time ago when I was just playing around. I'm actually serious about finishing this now and can't help but notice this causes a bit of a problem in certain situations. For example, if a primative is stationary on the floor and another primative falls onto it from a decent height, almost always, the falling box blows up and the resting box survives. Also if two boxes are falling side by side and give each other the tinyest of touches, then both boxes blow up mid air. Hmmmmm, not really perfect that. Does anyone have any idea how to improve my collision handler? Thanks in advance. Also, If anyone knows how to apply tags or something, please let me know and ill tidy this up. Thanks again
Jul 23, 2010 at 8:30 AM

Have you looked into BreakableTest.cs @ FP3 testbed? (You should edit your post and move your code into Code Snippet)

Jul 23, 2010 at 10:24 AM
No but i will. I added < code> around my code but it just changed the font.
Jul 24, 2010 at 3:07 AM
Edited Jul 24, 2010 at 3:11 AM

Like this:

Collision(Fixture fixtureA, Fixture fixtureB, Manifold manifold) 
{ 
    Vector2 position = manifold.LocalNormal; 
    float angle = (float)Math.Atan2(position.Y, position.X); 
    Vector2 force = Vector2.Zero; 
    if (angle < 0) 
        force = new Vector2((float)(Math.Cos(angle) * fixtureA.Body.LinearVelocity.X), 
(float)Math.Sin(MathHelper.TwoPi + angle) * fixtureA.Body.LinearVelocity.Y);
    else 
        force = new Vector2((float)(Math.Cos(angle) * fixtureA.Body.LinearVelocity.X), 
(float)Math.Sin(MathHelper.TwoPi - angle) * fixtureA.Body.LinearVelocity.Y); double XForce = Math.Sqrt(force.X * force.X); double YForce = Math.Sqrt(force.Y * force.Y); double totalForce = XForce + YForce; if ((Breakable) && (totalForce > BreakForce)) { Breakable = false; Active = false; BreakUp(fixtureA, fixtureB); } return true; }

The editor on these forums acts weird on various browsers. It was completely broken for anything but IE recently. At least it now has some features in FF and Chrome, but it still is best in IE. oh, and the html code for that code segment is "pre," although you're better off using the javascript editor (WYSIWYG).