2
votes

I have a box2d world with the view locked into landscapeLeft. I'm trying to move a body by tilting the phone left or right, which is currently working. I have problems when the level is started with the phone tilted in landscapeRight. under this condition the gravity is no longer being applied, and my body which should be moving stands still. I believe this is a friction issue (even though both my body to move and platform to move on are both 0 friction), because when I set the restitution of the platform my body is resting on to 1.0 (so my body is now bounding a little in place) the gravity is applied correctly always. This is not the box2d issue where bodies get stuck on corners of tiles as I only have 1 platform for my body to move on, and it does not move no matter where it is on the platform. here is my acceleration code.

   - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate (UIAcceleration*)acceleration
{
    if (acceleration.x < 0) {
        b2Vec2 gravity(-acceleration.y * 15, -8);
        world->SetGravity(gravity);
    }
}

here is my init code

// Define the gravity vector.
    b2Vec2 gravity;
    gravity.Set(0.0f, -10.0f);

    // Do we want to let bodies sleep?
    // This will speed up the physics simulation
    bool doSleep = true;

    // Construct a world object, which will hold and simulate the rigid bodies.
    world = new b2World(gravity, doSleep);

    world->SetContinuousPhysics(true);
1
What are you initializing gravity to when you create b2World()? - Bovinedragon
Is the body asleep? Changing gravity won't wake it up, you would need to SetAwake(true). - iforce2d
The body was asleep! SetAwake(true) fixed my problem, use this to answer my question and i'll accept it. here is some info about sleeping bodies for other users. When Box2D determines that a body (or group of bodies) has come to rest, the body enters a sleep state which has very little CPU overhead. If a body is awake and collides with a sleeping body, then the sleeping body wakes up. Bodies will also wake up if a joint or contact attached to them is destroyed. You can also wake a body manually. - FierceMonkey

1 Answers

0
votes

Look at answer in this post - Accelerometer based movement in Cocos2D game

Just use cocos2D sprite for movement and use value of sprite position to box2D body.