Moveable Platform lagging

Hey,

in my game I want to have moveable platform that move from A to B and back again (endless). So I read the start and end position from an XML and the body pointers are stored in an array (start and end position are also stores in an array). So in init the bodies are created as kinematic and get the velocity by setLinearvelocity (which direction is calculated with start and end position). In my update I iterate over the body pointers of the platforms and look if the get out of the boundaries . If so I just reverse the velocity. here's one condition I check for platforms that start with moving from the left to the right:
else if (xDestination > xSource && ySource == yDestination) {
if ((currentPosition.x*PTM_RATIO > xDestination) ||
currentPosition.x*PTM_RATIO < xSource) {
body->SetLinearVelocity(-body->GetLinearVelocity());
}
}

It works fine in the beginning but after I've played some time the bodies hang at the boundaries (I think they reverse the velocity all the time) and sometimes they get back in line.
I thought my approach was pretty straight forward but somehow it doesn't work properly. Any Ideas what that might be?

I should mention that my positions in the XML are something like x=0.28381 and when I set the position I do it by (x*screenSize.width,y*screenSize.height) so that it's positioned equally on every device.

ANY HELP SO MUCH APPRECIATED!
I think you should change this:

Code: Select all

if ( (x > xDestination) || (x < xSource) )
    body->SetLinearVelocity( -body->GetLinearVelocity() );

to this:

Code: Select all

if ( x > xDestination )
    body->SetLinearVelocity( moveLeftVelocity );
else if ( x < xSource )
    body->SetLinearVelocity( moveRightVelocity );
hm okay but then I have to calculate the velocity for every direction first... do you think it solves the problem (of course I'll test it). I somehow think that it's a memory leak as the effect only appears after reloading the scene (which is restart the game in my situation). I mean I create alle the bodies, load the scene again and so creating all the bodies again... but should I maybe delete alle the bodies before I reload the scene? I didn't find an similar issue 🙁
Of course I think it solves the problem, that's why I suggested it 😀
It's because the velocity gets reversed all the time, exactly as you supposed. A memory leak just means you are wasting memory, it doesn't affect the program like this.
About calculating the velocity, don't you already have it...? It will be the value you originally passed to SetLinearVelocity.
THANKS for the amazing hint, it finally works !! 😀 🙂 😉

Original thread: https://box2d.org/forum/viewtopic.php?t=8657