detecting collisions only with a certain force

Discuss new features and future development.
Post Reply
ashkanys
Posts: 2
Joined: Sat Sep 10, 2011 5:42 pm
Contact:

detecting collisions only with a certain force

Post by ashkanys »

HI Fellows,

I develop a game on iphone that has some similitude with angry birds although different
I use space manager v0.7
i have block piled on each other and i would like to detect impacts between this blocks
i cannot add the regular callbacks for each blocks between them because they touch each other and i would have multiple continuous callbacks calls to treat that would slow down the game
i would like to detect collisions between blocks only if the impact force is over a determined threshold.
In that case if blocks fall on each other i could make them break.
I know it is possible with box2D as angry birds do it.
I would like to know if there is a similar solution with chipmunk

Many thanks and regards,

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

Re: detecting collisions only with a certain force

Post by slembcke »

Yes, create a post-solve callback between the blocks and check the impact force. I have an Objective-C example on the downloads page called Angry Chipmunks that is vaguely similar:
http://chipmunk-physics.net/downloads.php

Basically it uses cpArbiterIsFirstContact() to check if it's the first frame two objects have hit each other. If it is, then it calculates the impact force and uses that to apply damage.

Code: Select all

static inline void BreakableApplyDamage(BreakableBox *box, cpArbiter *arb, GameWorld *self){
	if(cpArbiterIsFirstContact(arb)){
		// Divide the impulse by the timestep to get the collision force.
		cpFloat impact = cpvlength(cpArbiterTotalImpulse(arb))/FIXED_TIMESTEP;
		
		if(impact > 1.0f){
			box.strength -= impact/500.0f;
			if(box.strength < 0.0f){
				// Create the broken chunks and add them to the GameWorld.
				BrokenBox *broken = [[BrokenBox alloc] initFromBox:box];
				[self add:broken];
				[broken release];
				
				// If you broke the goal box you lose!
				if([box isKindOfClass:[GoalBox class]]) [self scheduleRestart];
				
				[self remove:box];
			}
		}
	}
}

// This is an Objective Chipmunk callback.
- (void)PostSolve_Default_BreakableBox:(cpArbiter *)arbiter space:(ChipmunkSpace*)space
{
	CHIPMUNK_ARBITER_GET_SHAPES(arbiter, unused, box);
	BreakableApplyDamage(box.data, arbiter, self);
}
Don't worry about calling collision functions to much. Realize that Chipmunk runs a lot of math for each collision pair it finds. Yeah, you might run a few hundred or maybe a few thousand of them per frame, just don't do anything really expensive in them like loading data from disk or calculate pi to a million digits.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ashkanys
Posts: 2
Joined: Sat Sep 10, 2011 5:42 pm
Contact:

Re: detecting collisions only with a certain force

Post by ashkanys »

Many thanks Slembcke, i'll try that
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests