Box2D Forums

It is currently Sat May 18, 2013 8:27 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Jul 16, 2012 11:55 pm 
Offline

Joined: Mon Jul 16, 2012 11:39 pm
Posts: 3
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!


Top
 Profile  
 
PostPosted: Tue Jul 17, 2012 2:34 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
I think you should change this:
Code:
if ( (x > xDestination) || (x < xSource) )
    body->SetLinearVelocity( -body->GetLinearVelocity() );

to this:
Code:
if ( x > xDestination )
    body->SetLinearVelocity( moveLeftVelocity );
else if ( x < xSource )
    body->SetLinearVelocity( moveRightVelocity );


Top
 Profile  
 
PostPosted: Wed Jul 18, 2012 1:47 am 
Offline

Joined: Mon Jul 16, 2012 11:39 pm
Posts: 3
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 :(


Top
 Profile  
 
PostPosted: Wed Jul 18, 2012 2:30 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
Of course I think it solves the problem, that's why I suggested it :D
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.


Top
 Profile  
 
PostPosted: Wed Jul 18, 2012 5:52 am 
Offline

Joined: Mon Jul 16, 2012 11:39 pm
Posts: 3
THANKS for the amazing hint, it finally works !! :D :) ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group