Jump to content
• Advertisement

Archived

This topic is now archived and is closed to further replies.

Is my time based movement right?

This topic is 5230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys ,sorry if this is a little simple, but are these functions right? Im not sure i am using the time based movement right for these 2D objects. Do i need to use it more that i am, by this i mean multiply my forces by it aswell? My main function, called every frame for all objects.
CObj::UpdateObject()
{

CalcVelocity();
UpdatePosition();

ApplyFriction(); // applies friction or air resistance

if(m_Z >0)
{
ApplyGravity();

}

if(m_VelocityZ < 0)
{
HandleLanding();
}

}

CObj::CalcVelocity()
{

if(m_Angle == 0)
{
m_VelocityX =0;
}
else
{
m_VelocityX = (m_Velocity*cos(AngleToRadians(m_Angle-90)));
}
m_VelocityY = (m_Velocity*sin(AngleToRadians(m_Angle-90)));

}
////////////////////////

CObj::UpdatePosition()
{
//using x = v*t + 1/2*a*t^2

m_X += m_VelocityX* m_ElapsedTime;// + (0.5*(m_ElapsedTime*2));

m_Y += m_VelocityY* m_ElapsedTime;// + (0.5*(m_ElapsedTime*2));

m_Z += m_VelocityZ* m_ElapsedTime;// + (0.5*(m_ElapsedTime*2));

}
//////////////////////////

CObj::ApplyFriction()
{
if(m_Z == 0)
{
if (m_Velocity > m_Friction )
m_Velocity -= m_Friction; //* (m_Velocity/100) ;

else if (m_Velocity < -m_Friction)
m_Velocity += m_Friction; //* ((m_Velocity*(-1))/100);

else m_Velocity = 0;

}
else
{
if (m_Velocity > m_AirResistance ) // check direction

m_Velocity -= m_AirResistance;
else if (m_Velocity < -m_AirResistance)
m_Velocity += m_AirResistance;
else m_Velocity = 0;

}

}
///////////////////

CObj::ApplyGravity()
{

m_VelocityZ -= m_Gravity;

}
///////////////////////

CObj::HandleLanding()
{

if((m_Z <= 0)&&(m_Absorbtion != 1.0))
{
Bounce();
}
else if(m_Z <= 0)
{
m_Z = 0;
m_VelocityZ = 0;
m_Velocity = m_Velocity/2 ;

}

}
/////////////////////////////////////////////////

CObj::Bounce()
{

m_VelocityZ = ((m_VelocityZ*(-1)) * m_Absorbtion);
if (m_VelocityZ < 0.1)
{
m_VelocityZ =0.0;
m_Z =0;
}
else
{
if(m_VelocityZ > 1)
m_Z = 0;
m_Velocity = m_Velocity /2;
}
}


Thanks for any suggestions or improvements.

Share this post

Share on other sites
Advertisement
Didn''t need to post all of your code
Anyway, check this out. Scroll down and there''s a time based movement tutorial. Good luck.

Share this post

Share on other sites
Oh, well I see what your asking now. Update the position after all calculations have been done to the velocity vector such as forces and such, then update the position by time.

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
Rutin
26
2. 2
JoeJ
20
3. 3
4. 4
5. 5
• Advertisement

• 10
• 10
• 9
• 9
• 10
• Forum Statistics

• Total Topics
631751
• Total Posts
3002086
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!