Page 1 of 1

Dynamic objects floating through static ones

Posted: Sat Apr 16, 2011 11:19 am
by sak
Hi -

I'm having an issue getting collisions to work correctly. I'm just getting started with chipmunk, and after successfully implementing a tutorial: http://www.alexandre-gomes.com/articles/chipmunk/ I'm trying to create my own simple program from scratch.

Basically all I do is:

1. initialize chipmunk with gravity
2. create a rectangular static object (floor)
3. create a dynamic square above the floor
4. call spaceStep and hashEach on a timer to let the square drop.

What I was expecting was for the square to land on the platform, maybe bounce and come to a stop. Instead, when the square lands, it slows down and drifts slowly through the platform, almost like it's under water.

Here's how I set up my objects:

the platform:

Code: Select all

    cpBody *body = cpBodyNew(INFINITY, INFINITY);
    body->p = cpv(0, -5);
    cpSpaceAddBody(space, body);
    
    
    cpVect verts[] = {
        cpv(-10, 2.5),
        cpv(10, 2.5),
        cpv(10, -2.5),
        cpv(-10, -2.5)
    };
    

    cpShape* rep = cpPolyShapeNew(body, 4, verts, cpvzero);
    rep->data = body;
    rep->e = 1.0;
    rep->u = 2.5;
        
    rep->collision_type = 0;
    cpSpaceAddStaticShape(space, rep);

The falling block:

Code: Select all

    cpBody *body = cpBodyNew(100, INFINITY);
    body->p = cpv(0, -0);
    cpSpaceAddBody(space, body);
    
    
    cpVect verts[] = {
        cpv(-.5, .5),
        cpv(.5, .5),
        cpv(.5, -.5),
        cpv(-.5, -.5)
    };
    

    cpShape* rep = cpPolyShapeNew(body, 4, verts, cpvzero);
    rep->data = body;
    rep->e = 1.0;
    rep->u = 2.5;
        
    rep->collision_type = 1;
    cpSpaceAddShape(space, rep);
If anyone knows why it would be acting this way the assist would be much appreciated!

Re: Dynamic objects floating through static ones

Posted: Sat Apr 16, 2011 3:22 pm
by slembcke
Some snippets from setting up your static object:

Code: Select all

    cpBody *body = cpBodyNew(INFINITY, INFINITY);
    ...
    cpSpaceAddBody(space, body);
    ...
    cpSpaceAddStaticShape(space, rep);
Adding a body to a space means that Chipmunk should simulate it. (fall under gravity and react to forces, etc) You don't want it to fall under gravity, and because it's infinite mass it won't react to forces or collisions anyway. The second part of the problem is that you are adding the shape attached to it as static. Static means that it doesn't move, so Chipmunk shouldn't ever bother updating the collision detection data.

Newer versions of Chipmunk have made this simpler and clearer:

Code: Select all

// Create a static body (read in the documentation what that means exactly)
cpBody *body = cpBodyNewStatic();
body->p = cpv(0, -5);

// Shortcut for making a box shape
cpShape *rep = cpBoxShapeNew(body, 20, 5);
//rep->data = body; // rep->body already has a pointer to the body
rep->e = 1.0;
rep->u = 2.5;
//rep->collision_type = 0; // it defaults to 0

// Shapes attached to static bodies are added as static bodies automatically
cpSpaceAddShape(space, rep);
Also, every space has a static body for it so you don't need to make one yourself. You can just attach your shapes to it using an offset.