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

Go To StackoverFlow.com

0

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.

2012-04-05 18:44
by bobthemac
What exactly is not working. I'm not sure what your question is - Eric J. 2012-04-05 18:46
@bobthemac try using integers instead of floa - Owais Qureshi 2012-04-05 18:48
@EricJ. the car is just circling round a centre point when I press the button to turn. I want to to work like a cars movement - bobthemac 2012-04-05 18:56
Hey why the downvoting , +1 for question suppor - Owais Qureshi 2012-04-05 18:56
@dotNetSoldier I get the same result if I use integers ad floats - bobthemac 2012-04-05 19:22
It might be helpful to know exactly what MoveX and MoveZ do, are they just setting the car's position? or adding to a position - MerickOWA 2012-04-05 19:31
MoveX and MoveZ are game engine functions that move the car by a given amount each frame. the engine I have to use it the TL engin - bobthemac 2012-04-05 19:35
Your verbal descriptions aren't very clear. Try screenshots if you can't describe it in words. (e.g. you complain that the car goes in a circle around a point -- but that's what cars do in real life when you turn the steering wheel and hold it in a fixed position - Hurkyl 2012-04-05 21:33
@Hurkyl sorry about my descriptions it's one of the things that I find most difficult. I might upload some videos to youtube in future so people can see what is going on - bobthemac 2012-04-05 21:41


1

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);
2012-04-05 20:40
by MerickOWA
Thanks Got it to work now thanks for the help everyon - bobthemac 2012-04-05 21:39


0

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.

2012-04-05 18:48
by Eric J.
This doesn't work either still just circles round a point - bobthemac 2012-04-05 18:58
I am aiming to get car movement that looks natural I don't think I was clear with what I said. I am trying to follow what was done for car movement at the link I posted in the question - bobthemac 2012-04-05 19:04
What does not look natural presently (other than that the car always faces the same direction) - Eric J. 2012-04-05 19:29
The steering moves doesn't steer the car at all and the movement goes from side to side instead of forward and back - bobthemac 2012-04-05 19:33
How are you representing the car? As a dot on the screen or as an image/icon/sprite of some kind? If you're using a sprite like in the article, you will ALSO need to rotate the sprite by angle for steering to look natural - Eric J. 2012-04-05 20:41
The car is a 3D model I can easily do the rotation on the model to make it look like it is turning the corner - bobthemac 2012-04-05 21:31
Ads