single effect when detecting collision
Hello everyone :)
I am a beginner and have a very simple question that I hope someone can help answer.
I have many circular objects moving around and my collision detection using spatial hashing
is working fine (yaaaay!!! :) ). Now I want to add a certain effect when a collision is detected.
Lets say that entity A collides with B. In my system first the object A is send a message .collision
with object B as an argument and then the same happens for object B. This means that I have
two places to deal with my collision which is useful if I want to make the things bounce for example.
Where however should I write the code that decides on an effect? I can not code it in the collision
function because then I will get two effects!
I would be grateful for any help.
Best
Dionysis
In my own system (and other similar system that I've seen), when a collision occurs between two objects A and B, both A and B (specifically, references to them) are passed to a callback function that has been registered for that particular object type pair. The callback function can then manipulate one or both objects, and also do any 'object-neutral' work that's necessary, such as spawning an effect.
For example, a callback for an asteroid and a projectile might look like this (pseudocode):
For example, a callback for an asteroid and a projectile might look like this (pseudocode):
void AsteroidProjectileCallback(object A, object B){ ((Asteroid)A)->Fragment(); B->Remove(); SpawnEffect("explosion_effect", A->Position());}
Personally I prefer this method to the 'invoke a member function for each object' method. Both approaches are valid, but since you're wanting to do some 'object-neutral' work in the callback, maybe the approach shown above would work better in your case.
Thank you for your answer :)
I need to clarify how my system works a bit. My collision detection system checks for each object in turn, collects the objects that are found to collide in an array and then sends the message collision to the object with the array as arguments. In this way I can have a collision result that takes into account the velocities and masses of multiple colliding objects. For example:
If A, B and C are colliding:
- A will check and do A.collision([B,C])
- B will check and do B.collision([A,C])
- C will check and do C.collision([B,A])
As you can see my problem is that I don't have a place to put the callback function in order for it to be evaluated only once. Am I going about it the wrong way?
Best
Dionysis
Perhaps construct a Collision object with handles to all involved bodies. The Collision object could then either perform the collision logic itself, or pass itself to each of the objects it references.
To ensure each collision is only processed once per frame, you could store the resulting Collision object in an index that gets reset at the start of each tick. Every time you detect a collision, check the index to see if it's been processed already.
Alternatively, give each object in the world a unique identifier and only allow the object with the lowest ID (in a collision) to initiate a collision response. The others might still detect a collision, but if they're not the object with the lowest ID involved then they should just ignore it as it either has been or will be taken care of.
To ensure each collision is only processed once per frame, you could store the resulting Collision object in an index that gets reset at the start of each tick. Every time you detect a collision, check the index to see if it's been processed already.
Alternatively, give each object in the world a unique identifier and only allow the object with the lowest ID (in a collision) to initiate a collision response. The others might still detect a collision, but if they're not the object with the lowest ID involved then they should just ignore it as it either has been or will be taken care of.
Quote:Original post by adroid28I need to clarify how my system works a bit. My collision detection system checks for each object in turn, collects the objects that are found to collide in an array and then sends the message collision to the object with the array as arguments. In this way I can have a collision result that takes into account the velocities and masses of multiple colliding objects. For example:
If A, B and C are colliding:
- A will check and do A.collision([B,C])
- B will check and do B.collision([A,C])
- C will check and do C.collision([B,A])
As you can see my problem is that I don't have a place to put the callback function in order for it to be evaluated only once. Am I going about it the wrong way?
IMHO save your self the trouble of multiple collision and just handle collision pairs in the standard nested for loop. Then resolve the collision pairs in with a callback in the manner Jyk suggests above. Multiple collision still works itself out anyway because an object may still get hit multiple times in the collision loop.
Unless you are doing something that absolutely requires real physics (table pool or something like that) nobody is ever going to notice the difference in most games where there are dozens of objects on the screen colliding at 60 fps.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement