Jump to content
  • Advertisement
Sign in to follow this  
adroid28

single effect when detecting collision

This topic is 3082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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):
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Hey thank you all for your replies. They were really helpful.
I think I have it sorted using the ID idea from TimmyC :) :)

All the best!!

Dionysis

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!