Problems with deceleration

Started by
0 comments, last by Katie 12 years, 8 months ago
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 icon_lol.gif

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));


The equations used are from basic physics.
Advertisement
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.

This topic is closed to new replies.

Advertisement