Box2D Forums

It is currently Sat May 18, 2013 6:52 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Mar 16, 2012 7:48 am 
Offline

Joined: Mon Jan 23, 2012 9:51 am
Posts: 15
Hello guys,

I am implementing a Kinematic moving platform in my game and got some problems regarding this. I read 2 parameters from a XML file:

Amplitude
Period

So, what I am doing right now is to set its speed as v = (Amplitude / (Period/2)) and then, after (Period/2) seconds, I revert the velocity of the object, so it goes back to its original position. As the values I get for the time step are not always the same (frame rate hiccups) and the sum of time steps not always coincides with one exact Period, my moving platform don't go back to the point it started its movement and it gets a variable Amplitude.

To solve this issue, I was thinking of placing some Sensors to detect where the platform should revert its movement, but bvefore I do that, I would like to know if you guys have any other suggestion.

Thanks


Top
 Profile  
 
PostPosted: Fri Mar 16, 2012 9:09 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
If your platform has a very predictable simple motion like that, perhaps you could use a function which gives you a position, eg. the sin function gives a nice smooth oscillating movement. Each frame you would increment a value by a fraction of the period and take the sin of it, multiplied by the amplitude. Uhh... easier to explain in code I think.
Code:
theta += 0.025; //period
b2Vec2 targetPos( 0, amplitude * sinf(theta) ); //where the body should be now
body->SetLinearVelocity( framesPerSecond * ( targetPos - body->GetPosition() ) ); //make the body move to the correct position in one time step
That would give you a body going only up/down, but you could alter the x component in the same way to make it move in a circle, oval, wobbly etc.
The reason for setting velocity rather than position is to get the expected interaction with other bodies by smooth movement, rather than a series of teleports as you would get by using SetTransform directly.
Because the position is tied to the framerate the body should not wander from its position like it can with the gradual error buildup with continuous velocity updates which rely on the position being correct in the previous frame.


Top
 Profile  
 
PostPosted: Fri Mar 16, 2012 9:45 am 
Offline

Joined: Mon Jan 23, 2012 9:51 am
Posts: 15
Thanks again for the tip. I will try it later and post the results =)


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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