play sound on impact but NOT when shapes are stacked

Official forum for the Chipmunk2D Physics Library.
progressxoverdue
Posts: 22
Joined: Sat May 09, 2009 8:54 pm
Contact:

play sound on impact but NOT when shapes are stacked

Post by progressxoverdue »

Hello,

I'm working on putting sound in my game. The game involves lots of stacked shapes. When I try to play a sound in postSolve() it spams collision sounds when ever there is a pile of stacked shapes. I tried to stop this by calculating the impact with cpArbiterTotalImpulse(arb) and only playing the sound if the impact was beyond a specific threshold, but this made it so that shapes had to impact very hard before any sounds were played.

Any ideas would be greatly appreciated !
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: play sound on impact but NOT when shapes are stacked

Post by slembcke »

That's pretty much exactly what I do. We set the volume of the sound played to be based on the impulse and don't play it at all if it's lower than a threshold. I don't remember if the volume is linear though... I seem to recall not.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: play sound on impact but NOT when shapes are stacked

Post by slembcke »

I just realized something. At some point I added a cpArbiterIsFirstContact() function. You can use this in your postSolve() callback as an early exit and only check to play sound on the initial impact. Changing the volume based on the collision impulse is still a good idea though I think. I like the variation that it gives.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
progressxoverdue
Posts: 22
Joined: Sat May 09, 2009 8:54 pm
Contact:

Re: play sound on impact but NOT when shapes are stacked

Post by progressxoverdue »

slembcke wrote:I just realized something. At some point I added a cpArbiterIsFirstContact() function. You can use this in your postSolve() callback as an early exit and only check to play sound on the initial impact. Changing the volume based on the collision impulse is still a good idea though I think. I like the variation that it gives.
Oh cool, I'll try that too. The variable sound volume does sound good. It adds a lot of realism. I'm super confused about when and how to release the AVAudioPlayer though :oops: . I tried releasing it after the play function, but then the sound never plays. Now I have this, but it's saying I'm sending messages to freed objects :(

Code: Select all

	
static AVAudioPlayer *impactPlayer = [AVAudioPlayer alloc];//make a player that I don't need to release every time.
	
NSURL *filePath = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/wood%d.wav", [[NSBundle mainBundle] resourcePath],soundNumber]];//Get the file URL

AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:&error];
	
[filePath release];	

impactPlayer = newPlayer;
	
[newPlayer release];
	
impactPlayer.volume = playVolume;
[impactPlayer prepareToPlay];
[impactPlayer play];
How is one supposed to release these darned things ?

Thanks!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: play sound on impact but NOT when shapes are stacked

Post by slembcke »

I wouldn't use AVAudioPlayer for short sounds. It does streamed loading/decompression which can be really expensive and high latency. You should use OpenAL or something else for short sounds and AVAudioPlayer for bg music.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
progressxoverdue
Posts: 22
Joined: Sat May 09, 2009 8:54 pm
Contact:

Re: play sound on impact but NOT when shapes are stacked

Post by progressxoverdue »

slembcke wrote:I wouldn't use AVAudioPlayer for short sounds. It does streamed loading/decompression which can be really expensive and high latency. You should use OpenAL or something else for short sounds and AVAudioPlayer for bg music.
Ok, I'll look into that, thanks!
Johanovski
Posts: 24
Joined: Thu Apr 29, 2010 5:07 am
Contact:

Re: play sound on impact but NOT when shapes are stacked

Post by Johanovski »

Hi there!

I've followed this thread to put sound in my game too, but the cpArbiterTotalImpact gives me strange values and the sound doesn't work as I expected... When the "player" object takes some velocity and collides with a wall TotalImpact's components are 0, and sometimes when the player is just brushing against an object it gives high values... Also the sound is never played in the first collision, but on the bounce (which of course is not what I'm expecting to do)... Here is the code, I'm doing it in the "postSolve" function as you've said:

Code: Select all

static void postSolveCollision(cpArbiter *arb, cpSpace *space,void *ignore)
{
	if (cpArbiterIsFirstContact(arb)) {
		if (cpArbiterTotalImpulse(arb).x * cpArbiterTotalImpulse(arb).y != 0.0f) {		
			NSLog(@"Crunch!");

			Game *game = (Game*)ignore;
			
			if (game.sound) {
				[[SingletonSoundManager sharedSoundManager] playSoundWithKey:@"cop" gain:1.0f pitch:1.0f location:Vector2fZero shouldLoop:NO];
			}
		}
	}
}

(...)

// When initializing Chipmunk...
cpCollisionHandler handler = space->defaultHandler;
cpCollisionPostSolveFunc postSolve = postSolveCollision;	
cpSpaceAddCollisionHandler(space, 0, 0, NULL, NULL, postSolve, NULL, self);
I've tried both for the TotalImpulse's axis product and just checking if at least one axis' value is not equal to 0.0f but I think it does the same... Anyone know what I'm doing wrong?

Thanks in advance! :)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: play sound on impact but NOT when shapes are stacked

Post by slembcke »

Are you using elasticity (space.elasticIterations != 0)? This is a know bug that I'm not sure how to work around yet. From the docs for the impulse functions:
Returns the impulse that was applied this step to resolve the collision. Calling this function outside of a postStep callback function is undefined. Enabling elastic iterations will cause you to get incorrect results, this is a known bug.
I don't remember the details of what this causes as I rarely use elasticity myself.

If you are using elasticity another option is to estimate the impact force using the relative velocity of the collision. This works just about as well in most cases.

Code: Select all

cpfloat impact = cpfabs(cpvdot(cpvsub(body1->v, body2->v)))
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Johanovski
Posts: 24
Joined: Thu Apr 29, 2010 5:07 am
Contact:

Re: play sound on impact but NOT when shapes are stacked

Post by Johanovski »

Hi slembcke!

I think I'm not using elasticity in my project... I've found this in the code:

Code: Select all

space->elasticIterations = space->iterations;
However, I've commented this and added:

Code: Select all

space->elasticIterations = 0;
And anything have changed so I think that (space->iterations) is in fact 0, so I'm not using elasticity...
However, with 0 elasticIterations the program does the same...

When the object first collides with a wall with a high velocity it gives 0 in cpArbiterTotalImpact, but when it bounces it sometimes gives more than 0... Also, depending on which "side" of the wall the object collides it works better or worse, and even depending on which "side" of the wall the cube is sliding it also gives some != 0 values...

My wall are made with (16 x 16) squares, and my player object is made with a (14 x 14) square (I've made this to prevent it from getting stuck when trying to pass through a (16 x 16) corridor... Maybe can this be causing this trouble (I think it shouldn't be, but I'm quite new to Chipmunk)?

Thanks in advance! :)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: play sound on impact but NOT when shapes are stacked

Post by slembcke »

Did you try just using the relative velocity trick? I'm not sure why the postSolve() trick isn't working for you, but the relative velocity estimation works just about as good and is much simpler. If that doesn't work then there is something wrong with your simulation.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests