i have been to this site to try and get my car movement sorted out. http://www.helixsoft.nl/articles/circle/sincos.htm
I have been having issues with it just moving the car in a circle because of the sin and cos that I have used I think I have done it correctly although the site does use fixed point number and I want to use floating point.
Here is my code
if(myEngine->KeyHeld(Key_W))
{
length -= carSpeedIncrement;
}
if(myEngine->KeyHeld(Key_S))
{
length += carSpeedIncrement;
}
if(myEngine->KeyHeld(Key_A))
{
angle -= 0.01f;
}
if(myEngine->KeyHeld(Key_D))
{
angle += 0.01f;
}
carVolocityX = length * (sin(angle));
carVolocityZ = length * (cos(angle));
carPositionX += carVolocityX;
carPositionZ += carVolocityZ;
car[0]->MoveX((carPositionX * sin(angle)) * frameTime);
car[0]->MoveZ((carPositionZ * cos(angle)) * frameTime);
I am open to new ideas on how to do this movement but it has to use vectors. Can anyoune see where I am going wrong with this.
Any help is appreciated.
Based on what you've said about MoveX and MoveZ, I think the problem is you're trying to pass an absolute position to a function which is expecting a velocity. Try
car[0]->MoveX(carVolocityX * frameTime);
car[0]->MoveZ(carVolocityZ * frameTime);
Why are you applying sin and cos both while calculating the velocity vector and while calculating a new position for your car?
Your code looks like you're trying to drive the car using the keyboard. If that's the case, try this
car[0]->MoveX((carPositionX) * frameTime);
car[0]->MoveZ((carPositionZ) * frameTime);
Note that I find it more clear to apply frameTime during the actual calculation of the distance and would suggest this edit:
carPositionX += carVolocityX * frameTime;
carPositionZ += carVolocityZ * frameTime;
car[0]->MoveX((carPositionX));
car[0]->MoveZ((carPositionZ));
If instead you just want to move it around in a circle, and not allow 'A' and 'D' to affect the angle,
car[0]->MoveX((carPositionX * sin(angularVelocity * totalTime)));
car[0]->MoveZ((carPositionZ * cos(angularVelocity * totalTime)));
You can use keys to adjust the (new) variable angularVelocity, or just assign a constant to see it working. The variable totalTime is the total time since the simulation began.
angle
for steering to look natural - Eric J. 2012-04-05 20:41