Rotating Objects: move at strange angle
I am trying to move a Mesh of a car about, everything works perfectly except when i try and turn it though an angle of about 45deg then the movement path doesnt match the direct of the mesh.
Code:
car.X = (float)(Math.Cos((double)car.DegToRad(car.Rotation - 90.0f))*50.0f) + car.X;
Its only happens when the cars Rotation is inbetween -0.45 and 0.45, other that it acts weirdly.
Re: Rotating Objects: move at strange angle
Look's like the formula's wrong:
car.X = (float)(Math.Cos((double)car.DegToRad(car.Rotation - 90.0f))*50.0f) + car.X;
Here's the right way to do it.
Car.X = Car.X + Cos((Pi * Degree)/180) * Rotation_Speed
Car.Y = Car.Y + Sin((Pi * Degree)/180) * Rotation_Speed
So try this:
car.X += (float)(Math.Cos((double)car.DegToRad(car.Rotation))) * 50.0f;
car.Y += (float)(Math.Sin((double)car.DegToRad(car.Rotation))) * 50.0f;
Notice now that (* 50.0f) is outside the parenthesis, and also notice I added Y rotation.
Re: Rotating Objects: move at strange angle
This is an exactly of moving forward at whatever angle.
m_VectorCircle is an array size of 360.
This is the ideal way to do it. This is the mathematically correct way to move in 3D. If you change upAngle to anything but 0 you will start to angle into the air....
Also, the cos/sin time can get as high as 500ms I believe...So it is best to make a lookup table.
PHP Code:
//----------------
//Generate Vector Circles -- Fills the cos/sin lookup table
//----------------
void MoveableObject::GenerateVectorCircle()
{
float Theta=0;
float PI = 3.14159265358979f;
float ToRad = PI / 180;
for (int i = 0;i < 360;i++)
{
Theta = i * ToRad;
m_VectorCircle[i].u = cosf(Theta);
m_VectorCircle[i].v = sinf(Theta);
}
}
//Within the unit movement function
if (m_Actions.MoveNorth)
{
m_Pos.x = m_Pos.x + (m_VectorCircle[m_Angle].u * m_Velocity) * (m_VectorCircle[m_UpAngle].u);
m_Pos.y = m_Pos.y + (m_VectorCircle[m_UpAngle].v * m_Velocity);
m_Pos.z = m_Pos.z + (m_VectorCircle[m_Angle].v * m_Velocity) * (m_VectorCircle[m_UpAngle].u);
}