Tim
Tim

Reputation: 196

Adjust volume of sound effect based on speed of collision

I am using Cocos2D with Box2D to create a simple physics game. I want to adjust the volume of a collision sound effect depending on the speed of the colliding body. The faster the body is travelling when it collides, the louder the sound. I am using the SimpleAudioEngine library which has a playSound method with a gain parameter. Is there a way to convert the speed of the colliding body (a b2Body object) to a value between 0 and 1 that I can apply to the gain?

Upvotes: 1

Views: 814

Answers (1)

Bongeh
Bongeh

Reputation: 2300

In the post solve function get an impulse value, divide it by 100 perhaps? I'm not sure what the levels of impulse you get are.

void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)

{
    b2Fixture* fixtureA = contact->GetFixtureA();
    b2Fixture* fixtureB = contact->GetFixtureB();

    void* userDataA = fixtureA->GetBody()->GetUserData();
    CCNode *myActorA = (CCNode*)userDataA;
    void* userDataB = fixtureB->GetBody()->GetUserData();
    CCNode *myActorB = (CCNode*)userDataB;

            // stuff above will allow you to work out which objects are hitting each other

            // get the impulse
        int impulseInt = impulse->normalImpulses[0];

}

Upvotes: 2

Related Questions