Sign in to follow this  

Playing sound in response to collisions

This topic is 1319 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

Hi,

 

I am currently trying to come up with a good solution for the situation where I have several collisions and want to play sounds in response to them.  With most aspects of the game, I can control with a degree of certainty when I play what sound, but for something as dynamic as the collision detection system, I'm a little stuck.  For example, say I have a large stack of boxes that I knock over.  I would like them to play small bumping-into-each-other sounds as they are falling and colliding with one another and with the floor.  The issue is that, especially once they have reached the floor but before the physics system (impulse based) has put them to sleep, they keep firing collision events and playing the sound as they make minuscule adjustments to their positions.

 

One solution I've come up with is to observe the velocity of the impacts and not play a sound below a certain threshold.  This could be extrapolated to include playing softer sounds at lower impact velocities until a final cutoff (so resting but not sleeping objects would still be silent), but there's a lot of trial-and-error involved in getting the thresholds exactly right.

 

Are there any more direct or obvious approaches to handling this type of situation that I'm overlooking, or is this the right track?

Share this post


Link to post
Share on other sites

Maybe you can have a collection of pairs of objects that collided in one collision check and, for every collision detected in the next check, look if that pair of objects is in the pairs list. If the pair is NOT in the pairs list trigger the sound, if the pair is in the list do not trigger the sound, and if a pair of the list has not collided in the current collision check remove the pair, so if it's detected later it'll trigger the sound again.

Share this post


Link to post
Share on other sites

If the pair is NOT in the pairs list trigger the sound, if the pair is in the list do not trigger the sound, and if a pair of the list has not collided in the current collision check remove the pair


You generally want to do this most of the time so that you can send OnCollisionEntered and OnCollisionExited events (or whatever you want to call them). Aside from just sounds, there's tons of game events that need to react differently between these events and OnCollisionMaintained.

Share this post


Link to post
Share on other sites

Sean, that's also a good suggestions and mixes in fluidly with the setup I've created according to Diego's initial suggestion, as well as my existing event management system.  Thanks for the tip!

Share this post


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