Sign in to follow this  
lonewolff

Problems with deceleration

Recommended Posts

Hi Guys,

I am in the process of making an Ogre based Lunar Lander style game.

Everything works fine except when I press the 'Up arrow' the object immediately accelerates upwards as opposed to the desired downward deceleration of the object (eventually having it go back up again).

If anyone could help out as to why this is happening, it would be great. I have spent a few days on this and am starting to tear my hair out [img]http://www.ogre3d.org/forums/images/smilies/icon_lol.gif[/img]

[code] if(KEY_DOWN(VK_UP)&&GetFocus()==(graphics->getWindowHandle()))
{
if(bKeyHeld==false)
{
StartCounter();
u=v;
}

s1=(0.5*(a1*(t*t)));
v1=u*u+2*a1*s1;
v1=sqrt(v1);
bKeyHeld=true;
}
if(KEY_UP(VK_UP)&&GetFocus()==(graphics->getWindowHandle())&&bKeyHeld==true)
{
bKeyHeld=false;
StartCounter();
u=v;
}

t=GetCounter()/1000;

s=(0.5*(a*(t*t)));
v=u*u+2*a*s;
v=sqrt(v);

if(a<0)
v=-v;

vt=v+v1;

graphics->assetTranslate(1,Vector3(0,vt/3000,0));
[/code]

The equations used are from basic physics.

Share this post


Link to post
Share on other sites
I see what you're trying to do here, but this isn't usually the way this is done for objects which can alter their physics. (it's the sort of thing you might do for particles or bullets which aren't subject to changing accelerations).


For player controlled objects, what one normally does is the keypress changes a flag (call it "thrust") and sets it true or false.

In the main loop, you ask "how long since my last update" using a counter.

If your "thrust" flag is set, then you apply a=f/m where f is your thrust force, m = mass. Then you do v.up += a. Then you do posn += v

v and posn are both xyz vectors which you hang onto between frames.


It's a discrete simulation rather than a continuous one.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this