Home > Game Physics, Tutorials, game dev > Collision Detection

Collision Detection

April 16th, 2008

Collision detection is a key component in creating a real world replica in the world of games. Writing your own collision detection system can be really painful and involves lot of mathematical calculations.So why waste your time writing something new when you have a lot of physics engines giving you out of the box support ( Ageia, Havok etc..)

We have compiled a small example (search for collision in the page ) wrt Adobe Director 11 and Ageia physics ( easy to code and implement ;) ) .

Please refer wiki for more details on Collision detection.

Collision detection can be achieved with 3 step process :

1.Enable Collision between rigid bodies. Use EnableCollision(rbA,rbB)
EnableCollision(void) - will enable collision for all rigid bodies in the world; true by default.
EnableCollision(rbA) - will enable collision of rbA with all rigid bodies.
EnableCollision(rbA,rbB) - will enable collision only between the mentioned rigid bodies.

2.Enable Collision Callback for the rigid bodies. Use EnableCollisionCallback(rbA,rbB)
EnableCollisionCallback(rbA) - will enable collision callback when rbA collides with any of the rigid bodies in the world.
EnableCollisionCallback(rbA,rbB) - will enable collision callback only between the mentioned rigid bodies.

3.Register collision callback handler for the collision. This is the function that will be called when collision is detected between 2 rigid bodies. Use registerCollisionCallback(collisionfunction)

Write code to handle the logic , what has to happen when the rigid bodies collide.

--This is how a sample collision handler would look like
--collisionfunction is just a name i decided to give
--It can be anything
on collisionfunction collisionReport
     --loop through the list to get all the collisions
     repeat with i = 1 to collisionReport.count
       --this gives model name of the model which was hit
       collision_hitList = collisionReport[i]
       rb_hit1 = collisionReport[i].ObjectA
       rb_hit2 = collisionReport[i].ObjectB
       --want to delete a rigid body ?
       --list.add(rbhit1)
       --Call a function on exitframe or after simulate() to delete the
       --model and rigid body in the list and reset the list.
       --DONOT do it within this method to avoid strange behaviors.
       contactPoint = collisionReport[i].pointOfContact
       collisionNormal = collisionReport[i].collisionNormal
     end repeat
end

1.NOTE: Remember, not to delete any rigid bodies inside the collision handlers!! , You can keep track of the models to be deleted in an array/list and then delete the same after the collision reports inside the handler function is taken care of. Trying to delete rigid bodies when you are inside the collision handler can result in unknown behaviors !! Also make it a habit to set the mass of every rigid body that you create, this saves you from breaking your head when you see strange things happen in 3d/physics world which were not supposed to happen that way :) …

Buy us a coffee mug !

  • Share/Save/Bookmark

  1. April 16th, 2008 at 20:59 | #1

    Thanks for the updated D11 samples. You ROCK!

  2. June 6th, 2008 at 03:47 | #2

    have you been able to do something with collision for a third person character

  1. No trackbacks yet.