Search code examples
c++game-physicstrigonometry

car doesn't move correctly in my game c++


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.


Solution

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