How can I detect a constant collision with the Chipmunk Physics engine

I try to make a boing sound when the shape of the ball hits any other shape. What works. But it works too well ....

When the ball stops or begins to roll, it is constantly confronted with a touch, so that the sound of “boing” is constantly on.

I cannot find anything in the chipmunk documentation to tell me when two things constantly collide. Therefore, I think that I will have to somehow figure it out myself, perhaps with some kind of timer that compares the last collision with the current collision. But that sounds awkward to me.

Has anyone solved this problem? How did you solve it?

+3
source share
3 answers

Could you just play your “boing” sound when contact is BROKEN? There is a callback for this to the chipmunk,typedef void (*cpCollisionSeparateFunc)(cpArbiter *arb, struct cpSpace *space, void *data)

This way you get fights whenever he bounces, but not when he just rolls. Of course, you will also get it when it rolls back from your figure, but it may be a function depending on how you look at it.

+2
source

I do not think that what I am going to say is good practice, but I am sure that this will solve your problem:

  • For each object, save two state variables: (1) The last object collided with (2) The last time of the collision.

  • , ( ) . .

:

// 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;
+2

Shapes do not have speed in the chipmunk. The bodies to which they are attached have this. You can access this speed: "myShape.body-> v". I agree that you should simply check if the speed exceeds a certain threshold in order to know when the “exposure” occurs. You can also check the rotation speed to see if the ball is rolling.

+1
source

Source: https://habr.com/ru/post/1720726/


All Articles