public function Update(dt:Number):void
{
//
// add gravity and generate motion bounds
//
for ( var i:int=1; i<m_rigidBodies.m_Num; i++ )
{
var p:RigidBody = m_rigidBodies.Get(i);
// apply gravity
p.m_vel.MulAddScalarTo( kGravity, dt );
// generate new motion bounds for collision
p.GenerateMotionBounds( dt );
}
//
// collision detection
//
for ( i=1; i<m_rigidBodies.m_Num-1; i++ )
{
var rbi:RigidBody=m_rigidBodies.Get(i);
for ( var j:int=i+1; j<m_rigidBodies.m_Num; j++ )
{
var rbj:RigidBody=m_rigidBodies.Get(j);
//
// simple broad-phase
//
var overlap:Boolean = AABB.Overlap( rbi.m_MotionBounds, rbj.m_MotionBounds );
if ( overlap )
{
//
// narrow phase
//
...
}
}
}
//
// solve all constraints
//
if (m_contactPool.m_Num > 0 || m_joints.m_Num > 0)
{
Solver.Solve(m_contactPool, m_joints, dt, Constants.kNumIterations);
}
//
// integrate forward in time
//
for ( i=1; i<m_rigidBodies.m_Num; i++ )
{
p=m_rigidBodies.Get(i);
p.Integrate( dt );
}
}
I have simplified this down to make it more readable. This is only the physics part, so you'd do your rendering afterwards.
Note the call to GenerateMotionBounds() in the first loop where I'm applying gravity. Looking at the code I am actually transforming all the points to generate the AABB for the predicted next frame and then combining the current frame's AABB with next frames to generate the motion bounds. However, you might just be able to get away with transforming the AABB verts and then generating a new AABB from that and the current frame. It wasn't a performance concern for me, so I never attempted that.
As you can see, the broad-phase is just AABB vs AABB intersection, which is very simple.
If you have a lot of objects, you absolutely need something more intelligent than a simple nested for loop - I've used Sort & Sweep in the past and hash-grid. Hash-grid is great if your objects are similar sizes. AABB trees are the latest kid on the block in that regard, although I've never used them. The goal is to only consider objects which have moved - objects which are stationary should do little to no work.
[color="#1C2837"]for object A to have passed through object B, object A must move to opposite sides of object B on all three world-coordinate axes (X, Y, Z) [/quote]
[color="#1C2837"]Is that true? What about if object A is moving in only one axis?
[color="#1C2837"]I'm not a big fan of complex maths either - luckily collision detection is all geometry which I can get my head around quite nicely
[color="#1C2837"]Cheers, Paul.