Playing sound in response to collisions

Started by
3 comments, last by WFP 9 years, 10 months ago

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?

Advertisement

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.

Good suggestion, DiegoSLTS. That seems like it would solve the resting contacts problem well. I'll try it out.

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.

Sean Middleditch – Game Systems Engineer – Join my team!

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!

This topic is closed to new replies.

Advertisement