CodeRecipe( );

one stop for all game coders..

Follow us on Twitter
Home
Collision Detection Print E-mail
User Rating: / 1
PoorBest 
Written by CR Team   
Monday, 18 May 2009 00:00

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 sample wrt Adobe Director 11 and Ageia physics ( link below ) .

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
--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

NOTE: Remember, not to delete any rigid bodies inside the collision handler!! , Instead you can keep track of the models to be deleted in an array/list and then delete the same(delete rigid body first, then the models ) 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 :) …

 Download Collision Detection in PhysX
File Title:Collision Detection in PhysX (Details)
File Type:zip
File Version:1.0
File Size:31.37 Kb
License:Coderecipe.com , All Rights Reserved, 2009. Read the Disclaimer: http://coderecipe.com/cr-disclaimer
File Author:Coderecipe.com
File HomePage:
Downloads:133
Rating: (1 Votes)
Your Vote: