Archived

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

funkykid

Is my time based movement right?

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


Link to post
Share on other sites
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


Link to 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


Link to post
Share on other sites