Page 1 of 1

Strange starting place behaviour

Posted: Wed Apr 24, 2013 4:48 pm
by jamesl22
Hi,

I am trying to implement chipmunk into my tile based game. I originally tried Box2D but found that the character got stuck on block edges. Currently, when you enter the game, the character starts off at a strange location of (49347, 32395) instead of (385,254). I define my bodies like this:

Code: Select all

void physics_addStaticBodies()
{
    for(int i = 0; i < 256; i++)
    {
        for(int i2 = 0; i2 < 256; i2++)
        {
            if(tile[i][i2].visible && tile[i][i2].shape == 1)
            {
                tile[i][i2].body = cpSpaceAddBody(world, cpBodyNew(INFINITY, INFINITY));
                cpBodySetPos(tile[i][i2].body, cpv(tile[i][i2].x, tile[i][i2].y));
                cpShape *Shape = cpSpaceAddShape(world, cpBoxShapeNew(tile[i][i2].body, blocksize, blocksize));
                cpShapeSetFriction(Shape, 0.7);
            }
        }
    }
}

void physics_addDynamicBodies()
{
    for(int i = 0; i < characters; i++)
    {
        if(character[i].visible)
        {
            character[i].body = cpSpaceAddBody(world, cpBodyNew(1.0f, INFINITY));
            cpBodySetPos(character[i].body, cpv(character[i].x, character[i].y));
            cpShape *Shape = cpSpaceAddShape(world, cpBoxShapeNew(character[i].body, character[i].width, character[i].height));
            cpShapeSetFriction(Shape, 0.7);
        }
    }
}
What could be the problem?

Thanks,

James

Re: Strange starting place behaviour

Posted: Wed Apr 24, 2013 5:15 pm
by slembcke
I'm not sure, but I suspect there are other issues happening here:
1) You don't want to create a body for each tile. It's just a waste of memory. Just create a single static body using cpBodyNewStatic() and offset the tiles using the more flexible cpBoxShapeNew2() to create their collision shapes.
2) It's a *very* bad idea to add infinite mass bodies to your simulation. As mention in #1, what you really want is to use a static body.

Try making those changes first. I'm sort of wondering if you are starting your player inside a wall and having it get pushed out somewhere else.

Re: Strange starting place behaviour

Posted: Thu Apr 25, 2013 1:39 pm
by jamesl22
Thanks for the advice. I have tried what you suggested but the same behavior occurs. I now define static bodies as follows:

Code: Select all

void physics_addStaticBodies()
{
    staticBodies = cpBodyNewStatic();
    for(int i = 0; i < 256; i++)
    {
        for(int i2 = 0; i2 < 256; i2++)
        {
            if(tile[i][i2].visible && tile[i][i2].shape == 1)
            {
                tile[i][i2].body = cpSpaceAddShape(world, cpBoxShapeNew2(staticBodies, cpBBNew(tile[i][i2].x, tile[i][i2].y, tile[i][i2].x + blocksize, tile[i][i2].y + blocksize)));
                cpShapeSetFriction(tile[i][i2].body, 0.7);
            }
        }
    }
}
EDIT: If I divide the characters position by its width and height respectively I can get the character to be in the correct place for one frame. After this it starts flying off exponentially even when I do not define any static bodies.

Re: Strange starting place behaviour

Posted: Thu Apr 25, 2013 2:52 pm
by slembcke
How are you creating and moving the player? It sounds like there is something else wrong here other than how you create your tile shapes. If you have to divide it's position by it's width and height it sounds like there is some math screwed up somewhere when you are are initializing things.

Re: Strange starting place behaviour

Posted: Thu Apr 25, 2013 3:05 pm
by jamesl22
The player is created in a "level_load" function which calls the physics initialization functions after. The character is stored as a exact pixel value in the level file, in this case (385.528, 254.087). I found that if I set the position manually after initialization in a place like (10,10), rather than appearing at that pixel value chipmunk set the character position at (10 * character[0].width, 10 * character[0].height).

As for movement, the strange coordinates appear without me even defining any physics related movement code.

EDIT: NM, I still had my Box2D pixels to meters conversion code in place :lol:. It appears the positioning bug is now resolved. Thanks for the advice.

Re: Strange starting place behaviour

Posted: Thu Apr 25, 2013 3:34 pm
by slembcke
Ah, that would do it. I was figuring that something sort of scaling was happening.

Re: Strange starting place behaviour

Posted: Thu Apr 25, 2013 3:48 pm
by jamesl22
Yeah, bit silly of me. Sometimes I miss the simplest things. Just a quick question about cpBB what does it mean when it defines "left, bottom, top, right" values as single floats? How does that work and how would I define a box using such a bounding box?

Re: Strange starting place behaviour

Posted: Thu Apr 25, 2013 4:52 pm
by slembcke
Perhaps more clear this way: left, bottom, right, top -> min x, min y, max x, max y