Collision problems with big shapes?

Official forum for the Chipmunk2D Physics Library.
Post Reply
Goerki
Posts: 2
Joined: Thu Mar 21, 2013 2:13 am
Contact:

Collision problems with big shapes?

Post by Goerki »

Hi Guys! :D

I wanted to create a little iPad game to test all the features of Chipmunk. But I am facing a problem which i cant solve.

I just made a method that lets me draw new bodies (boxes) that are affected by gravity. It works really fine, but only for small boxes. If I make a bigger box (more than about 200pixel) these boxes start to overlap with the ground and sometimes even fall through it.
The shapes work fine, the boxes always collide in the right moment, but the big boxes never fall asleep. They just vibrate into the ground.

Image
(there is a static shape that starts 50px above the bottom)

It has nothing to do with the mass, as all the bodies have the same mass.

Here is the Method that creates the boxes:

Code: Select all

-(void) newObjectFrom:(CGPoint)start toPoint:(CGPoint)end onLayer:(CCLayer *)layer {
    numberBodies ++;
    CGPoint size;
    size.x = start.x - end.x;
    size.y = start.y - end.y;
    CGPoint center;
    center.x = start.x - size.x/2;
    center.y = start.y - size.y/2;
    cpFloat mass=1;
    
    
    sprite[numberBodies] = [CCSprite spriteWithFile:@"wood.png" rect:CGRectMake(0, 0, abs(size.x), abs(size.y))];
    sprite[numberBodies].position = center;
    [layer addChild: sprite[numberBodies]];
    

    body[numberBodies] = cpBodyNew(mass, 10);
    cpBodySetPos( body[numberBodies], center );
	cpSpaceAddBody(space, body[numberBodies]);
    
	cpShape* shape = cpBoxShapeNew(body[numberBodies], abs(size.x), abs(size.y));
	cpShapeSetElasticity( shape, 0.5 );
	cpShapeSetFriction( shape, 0.5 );
	cpSpaceAddShape(space, shape);
}
I also tried to solve it with more Iteration, a smaller CollisionSlop and a huge SpatialHash. Nothing worked.
Thats how i create my chipmunk space.

Code: Select all

-(id) init {
   if (self = [super init])
    {
    cpInitChipmunk();
  
    space = cpSpaceNew();
	cpSpaceSetGravity(space, cpv(0, -500) );
    cpSpaceSetIterations(space, 20);
        cpSpaceSetCollisionSlop(space, 0);
        cpSpaceUseSpatialHash(space, 1000, 5);

   cpShape *ground = cpSegmentShapeNew(space->staticBody , cpv(0,0), cpv(1024,0), 50);
    cpShapeSetElasticity(ground, 0.1);
    cpShapeSetFriction( ground, 0.4);
    cpSpaceAddStaticShape(space, ground);
        numberBodies = -1;
    }
    return self;
}
and my Update:

Code: Select all

-(void) update:(ccTime) delta
{
	int steps = 3;
	CGFloat dt = [[CCDirector sharedDirector] animationInterval]/(CGFloat)steps;

	for(int i=0; i<steps; i++){
	[space updatePhysics:dt];
	}
    [space updateSprites];
    }
}




-(void) updatePhysics: (CGFloat) dt {
    cpSpaceStep(space, dt);
}

-(void) updateSprites {
    for (int i=0; i <= numberBodies; i++) {
        if (sprite[i]) {
        sprite[i].position = body[i]->p;
        sprite[i].rotation = CC_RADIANS_TO_DEGREES(-1 * body[i]->a);
        }
    }
}
Can you help me? What did i do wrong :?:
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Collision problems with big shapes?

Post by slembcke »

You are creating all of the objects with a mass of 1 and a moment of 10. That's a huge problem. You should make big boxes more massive than little ones, but more importantly, you *must* use the cpMomentFor*() functions to calculate the moment if you don't know how. Your moment of inertia is probably wrong by a factor of 10,000x or more for those larger boxes assuming you are using pixels as units. That's what's causing the jitter.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Goerki
Posts: 2
Joined: Thu Mar 21, 2013 2:13 am
Contact:

Re: Collision problems with big shapes?

Post by Goerki »

Aaaahhh, I see.

I had different masses for bigger objects. I just thought maybe this causes the problem and set them all to 1.
I wanted to write my own formulas for the moment of inertia, they are just not finished yet. I would have never guessed that this causes the problems!

Thank you, now everything works perfect!!
Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests