I'm having trouble finding any topics and resources on this. Can someone point me in the right direction?
Chipmunk collision sounds
(24 posts) (3 voices)-
Posted 1 year ago #
-
I'm assuming you want to make a sound when a collision event occurs.
If that's the case, inside the collision function you must play the sound.
Maybe you'll want to guarantee that the sound will be played only one time during a certain amount of time. In this case you'll need to control a counter, just like that:
static int wallCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data) { if(tCollisionSound<=0){ // play your sound here tCollisionSound = SOUND_INTERVAL_TIME; } return 1; }Posted 1 year ago # -
Is that a built in function or did you make it?
Posted 1 year ago # -
That's the function I've associate the collisions with walls:
cpSpaceAddCollisionPairFunc(space, 1, 0, &wallCollision, NULL); // collisions with walls0 = body collision type
1 = wall collision typePosted 1 year ago # -
I havent done any of that, I've literally just made bodies and shapes and let Chipmunk do the rest. Now I need to add sound to the collisions. I'll have a bash at the code you've sent me. Thanks!
Posted 1 year ago # -
warning: Semantic Issue: Implicit declaration of function 'cpSpaceAddCollisionPairFunc' is invalid in C99
Posted 1 year ago # -
import
Posted 1 year ago # -
I've added
cpSpaceAddCollisionPairFunc(space, 1, 0, &wallCollision, NULL); // collisions with wallsto my tick function and then declared the function in the .h file by:static int wallCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data);although its erroring wallCollision declared static but never defined.Posted 1 year ago # -
The cpSpaceAddCollisionPairFunc call must be after you create the bodies (not in your tick function), and the wallCollision function must be in your .m file.
Don't forget to associate the corresponding collision type to the shapes:
shape->collision_type = 0; // examplePosted 1 year ago # -
Ok it is in my m file but it complains i isn't declared, do I need to do something to my .h? I've put
cpSpaceAddCollisionPairFunc(space, 1, 0, &wallCollision, NULL); // collisions with wallsafter my bodies and shapes yet it's still complaining about C99. What is that? Use of undeclared identifier 'wallCollision'Posted 1 year ago # -
Thanks for that. I don't see what is wrong with where I'm putting the
cpSpaceAddCollisionPairFunc(space, 1, 0, &wallCollision, NULL); // collisions with wallssince it's in the same place as the tutorial.Posted 1 year ago # -
That function is old... maybe pre-version 5? You want this one:
void cpSpaceAddCollisionHandler( cpSpace *space, cpCollisionType a, cpCollisionType b, cpCollisionBeginFunc begin, cpCollisionPreSolveFunc preSolve, cpCollisionPostSolveFunc postSolve, cpCollisionSeparateFunc separate, void *data )Posted 1 year ago # -
For sounds on collisions I did something like this in Kill Timmy... it worked somewhat ok:
static inline void soundPlay(float ff, const float max, const float total, NSString* file) { if (ff > max) { //clamp ff = (ff > total) ? total : ff; float perc = (ff-max)/(total-max); [[SimpleAudioEngine sharedEngine] playEffect:file pitch:perc pan:0.0f gain:perc]; } } void box2boxCollision(cpArbiter *arb, struct cpSpace *space, void *data) { CP_ARBITER_GET_SHAPES(arb, b1, b2); const float max = 850.0f; const float total = 1500.0f; float ff = cpvdistsq(b1->body->v, b2->body->v); NSString *file = @"boxhit.wav"; soundCheck(ff, max, total, file); }Basically I check the difference between the velocities, make sure it's less than some maximum... do a calculation for the gain (I'm not sure why exactly I used "total" here, prob could be better). I also played with the pitch here to give it some variability.
Posted 1 year ago # -
Thanks @mobilebros !
Nice piece of code !Posted 1 year ago # -
Cool thanks! I's not erroring now. But how do I call wallCollision from `void
cpSpaceAddCollisionHandler(
cpSpace *space,
cpCollisionType a, cpCollisionType b,
cpCollisionBeginFunc begin,
cpCollisionPreSolveFunc preSolve,
cpCollisionPostSolveFunc postSolve,
cpCollisionSeparateFunc separate,
void *data
);`Posted 1 year ago # -
I've done
cpSpaceAddCollisionHandler(space, 1, 0, &wallCollision, nil, nil, nil, self);but it now says Use of undeclared identifier 'wallCollision'.Posted 1 year ago # -
In general you put the static declarations on the top of any other method (inside the class implementation however). This way the compiler will recognize your function, even if it's not declared on .h file.
Posted 1 year ago # -
Ok now it is saying: warning: Semantic Issue: Incompatible pointer types passing 'int (*)(cpShape *, cpShape *, cpContact *, int, cpFloat, void *)' to parameter of type 'cpCollisionBeginFunc' (aka 'cpBool (*)(cpArbiter *, struct cpSpace *, void *)')
cpSpaceAddCollisionHandler(space, 1, 0, &wallCollision, nil, nil, nil, self);
Posted 1 year ago # -
Man, this is another function you know... Different parameters...
If you take a closer look at the error message you'll see that the function is waiting for two cpShapes, not the cpSpace (this is the old function !). Please refer to the Chipmunk documentation.Posted 1 year ago # -
I know, so which of the cpCollisionBeginFunc begin,
cpCollisionPreSolveFunc preSolve,
cpCollisionPostSolveFunc postSolve,
cpCollisionSeparateFunc separate, is the one for wallCollision...If it's none of them how do I call static int wallCollision at all?
There is nothing different between mine and his http://www.cocos2d-iphone.org/forum/topic/4567 yet mine is giving me errors...
Posted 1 year ago # -
Slight adjustment, now it works like a gem. Thanks
cpSpaceAddCollisionHandler(space, 1, 0, (cpCollisionBeginFunc)wallCollision, NULL, NULL, NULL, NULL);Posted 1 year ago #
Reply
You must log in to post.