views:

413

answers:

4

I'm trying to play a "boing" sound when a ball shape hit's any other kind of shape. Which works. But works a little too well....

When the ball comes to rest, or starts to roll, it's in a permanent collision with whatever it's touching, so the "boing" sound fires constantly.

I can't find anything in the chipmunk documentation to tell me when two things are permanently colliding. So I'm thinking I will have to somehow figure it out myself, probably with some sort of timer that compare the last collision against the current collision. But that sounds hacky to me.

Has anyone tackled this issue? How did you solve it?

+1  A: 

Shapes don't have velocity in chipmunk. The bodies they are attached to have it. You can access velocity like this: "myShape.body->v". I agree that you should just be able to check if the velocity is over a certain threshold to know when an 'impact' occurs. You can also check the rotational velocity to see if the ball is rolling.

falkone
A: 

I don't think that what I'm about to say is a good practice, but I'm sure it will solve your problem:

  1. For each object, keep two state variables: (1) The last object collided with, (2) The last collision time.

  2. Upon collision, only play a sound if the collided object is different (and a ball) OR a certain "delta time" has elapsed since the last collision. Then record the last collision stats.

This is pretty simple and very effective:

// In your ball interface
id lastCollisionObject;
double lastCollisionTime;


// When a collision occurs...
double now = GetTime();
id other = GetCollisionObject();
if ((now - lastCollisionTime) > 0.3 || other != lastCollisionObject) {
    PlaySound(kBoingSound);
}
lastCollisionObject = other;
lastCollisionTime = now;
Frank Krueger
A: 

Hi all,

I have the same issue .. permanently "Poing" while constantly Contact. I fired the Sound in Cihpmunk "cpSpaceAddCollisionPairFunc" and I have no Idea how to attach Vars like id lastCollisionObject and double lastCollisionTime in this Function. Any Help? thx in forward

n8dancer
+1  A: 

Couldn't you just play your "boing" sound when contact is BROKEN? There is a callback for that in chipmunk, typedef void (*cpCollisionSeparateFunc)(cpArbiter *arb, struct cpSpace *space, void *data)

That way you get boings whenever it bounces, but not when it's just rolling along. 'course, you'll also get one when it rolls off of your shape, but that could be a feature depending on how you look at it.

Kenny Winker
If a ball, say, rolls off of a ramp, as opposed to bouncing against a ramp, it should be silent when it rolls but loud when it bounces. Both cases are after collision is separated, so I don't think that on-separate is a good place to do this, unless the domain is one where things only bounce and never roll or slide.
JJ Rohrer