Cocos2d iphone - jitter scroll movement with Box2D

Discuss issues specific to using Box2D on the iPhone
betece
Posts: 5
Joined: Fri Oct 04, 2013 2:23 am

Cocos2d iphone - jitter scroll movement with Box2D

Postby betece » Sun Dec 14, 2014 8:57 am

Hi Box2D fourm,

i have a problem with jittery movement in my side scroll game. Jitter movement means that the hero jumps sometimes and then the scrolling jumps also (because it's relative to the hero position). I currently use cocos2d version 3.3 and the newest Box2D version.

I have a CCNode which follow the player node. The hero node is add to the follower:

Code: Select all

       [self addChild:_follower];
        [_follower addChild: _hero];


The hero is a class which is a wrapper for b2Body*. The class inherits from CCNode. Some of the properties are overridden to manage the physics Body

Code: Select all

    // Override the setters and getters to always reflect the body's properties.
    -(CGPoint)position
    {
        b2Vec2 pos  = _physicalBody->GetPosition();
       
        float x = pos.x * PTM_RATIO;
        float y = pos.y * PTM_RATIO;
       
        return ccp(x,y);
    }
   
    -(void)setPosition:(CGPoint)position
    {
        float angle = _physicalBody->GetAngle();
        _physicalBody->SetTransform( b2Vec2(position.x / PTM_RATIO, position.y / PTM_RATIO), angle );
    }


The way how i scroll is like this in my update method:

Code: Select all

    int32 velocityIterations = 8;
    int32 positionIterations = 4;
   
    _world->Step(delta, velocityIterations, positionIterations);
   
    _world->ClearForces();
   
    float offset = _hero.position.x;
    float offsetY = _hero.position.y;
   
    float scaleToCalculateWith = _follower.scale;
   
    float posX = (_winSize.width/2-offset*scaleToCalculateWith);
    float posY = (_winSize.height/2-offsetY*scaleToCalculateWith);
   
    _follower.position = ccp(posX, posY);


I have read lots of articles the past days about interpolation and stuff to prevent jitter in Box2D but i don't get it to work.

I have tried:

- Fixed time-step (read from this http://www.unagames.com/blog/daniele/20 ... tion-box2d)
- Extrapolation
- Interpolation
- put the update stuff in a fixedUpdate method

I have a tracked the time between the update methods and sometimes the differences is about 60-70 miliseconds and then it gets jitter. But i can't prevent these differences there also when i don't have any nodes or something in the project.

Because it's hard to write everthing what i did so far i create a example project.

--> https://www.dropbox.com/s/suw9pw2b5mvq8 ... e.zip?dl=0

I test this project on a iPhone 4. It's pretty close to my real app. I add a few CCNodes to the scene to simulate some content and to make the CPU usage a bit higher to see the jitter. I implement a prallax scrolling that you can better see the jitter.

I stuck in this a few weeks now so maybe someone know this issue and can help me.

Thank you in advance.

Return to “iPhone”



Who is online

Users browsing this forum: No registered users and 1 guest