Determining when to play impact sounds

Post Reply
Starfox
Posts: 37
Joined: Wed Jul 27, 2011 12:01 am

Determining when to play impact sounds

Post by Starfox »

I'm new to integrating physics with a game scenario and I was wondering when should the game engine play impact sounds for physics objects colliding, and what information in the collision callback should the decision to play the sound and additional info like the type of sounds, sound's volume etc. be based on?
inferno
Posts: 5
Joined: Wed Nov 27, 2013 1:53 am

Re: Determining when to play impact sounds

Post by inferno »

I'm interested in this as well so I'm going to bump this +1.

One way I considered doing this is by recording the vector3 velocity of the object every "game tick" and then comparing it's direction and magnitude. The difference in direction and magnitude each tick can basically be interpreted as "did I hit something and how hard".

I'm sure there's a better way though. I'm a nub so this is just my theory. :wink:
papaonn
Posts: 41
Joined: Wed Nov 20, 2013 4:14 pm

Re: Determining when to play impact sounds

Post by papaonn »

Hi,

Bullet will auto return a callback when there is any collision happens,
so to adapt to different sound-contact profiling,
i suggest customize your own contact protocol and generate different sounds based on different objects.

You could simply set a boolean value within your object class and when contact callback was called,
check if it is triggered once, only play sound when the value has a change :

Code: Select all

myObj : public btRigidBody {
     bool bFirstContact = false;
     int bodyIndex;
};

CollisionCallback(){
      updateRigidBodyContact(obj->getBodyIndex, isCollide());
}

void updateRigidBodyContact(int index, bool bCollision){
      bool earlierContactValue = rigidBody[index].bFirstContact;

      rigidBody[index].bFirstContact = true;

      if(earlierContactValue != rigidBody[index].bFirstContact){
             // the first contact found
             // play music
      }

}
Starfox
Posts: 37
Joined: Wed Jul 27, 2011 12:01 am

Re: Determining when to play impact sounds

Post by Starfox »

papaonn wrote:Hi,

Bullet will auto return a callback when there is any collision happens,
so to adapt to different sound-contact profiling,
i suggest customize your own contact protocol and generate different sounds based on different objects.

You could simply set a boolean value within your object class and when contact callback was called,
check if it is triggered once, only play sound when the value has a change :

Code: Select all

myObj : public btRigidBody {
     bool bFirstContact = false;
     int bodyIndex;
};

CollisionCallback(){
      updateRigidBodyContact(obj->getBodyIndex, isCollide());
}

void updateRigidBodyContact(int index, bool bCollision){
      bool earlierContactValue = rigidBody[index].bFirstContact;

      rigidBody[index].bFirstContact = true;

      if(earlierContactValue != rigidBody[index].bFirstContact){
             // the first contact found
             // play music
      }

}
I realize that's the general approach but there doesn't seem to be a collision callback as you describe, at least according to http://www.bulletphysics.org/mediawiki- ... d_Triggers - The callbacks there get called whenever a manifold is generated (not when a collision happens) and I'm also not sure how to extract the impact force from the data provided.
c6burns
Posts: 149
Joined: Fri May 24, 2013 6:08 am

Re: Determining when to play impact sounds

Post by c6burns »

The very first example in the link you just posted shows looping the manifold and checking if any contact points are < 0 distance. Using that data you could store metadata in your application to determine if the contact has just been made (eg. bool isTouching or some such). Play the sound when two objects first touch and then not again until they "untouch" and "retouch". From there you could take the linear velocity as the impact strength if nothing else.
Post Reply