Sign in to follow this  
PSD

Rotation problem for driving game

Recommended Posts

PSD    122
I have a problem with rotating the vehicle to face the correct direction. I'm currently calculating the angle by taking a dot product of the foward vector and the last foward vector and dividing it by the product of their magnitudes, then take the cos inverse to get the angle. This does not work very well, the car rotates too slowly, its not keeping up with the direction its goin in. I would appreciate some help on this, I've been tryin 2 find a solution and hav come across quaternions but i'm not sure thats what i hav to use and havent got a clue how to use them. I also came across eulers angles but not sure how to implement it. Below is the code where i calculate the angle: Code:
if (keys[VK_RIGHT]) {
      
      Vector3d turn(-vehicle->getDirection().z/25,0,vehicle->getDirection().x/25);
  
    Vector3d lastFoward (vehicle->getFoward().x, vehicle->getFoward().y, vehicle->getFoward().z);
     vehicle->changeFoward(turn);
      //camera.Rotate_Position(CAMERATURN);
      
      double dotprod = lastFoward.dotProduct(vehicle->getFoward());
      double mags = lastFoward.magnitude()*vehicle->getFoward().magnitude();
      GLfloat ang = (GLfloat) acos(dotprod/mags);
      carturn-=(ang*20);   
      camturn+=2.0f;
   }
   if (keys[VK_LEFT]) {
      Vector3d turn(vehicle->getDirection().z/25,0,-vehicle->getDirection().x/25);
    
    Vector3d lastFoward (vehicle->getFoward().x, vehicle->getFoward().y, vehicle->getFoward().z);
     vehicle->changeFoward(turn);
     
      //camera.Rotate_Position(-CAMERATURN);
      
      double dotprod = lastFoward.dotProduct(vehicle->getFoward());
      double mags = lastFoward.magnitude()*vehicle->getFoward().magnitude();
      GLfloat ang = (GLfloat) acos(dotprod/mags);
      carturn+=(ang*20);   
      camturn-=2.0f;
   }

[Edited by - PSD on April 22, 2006 12:05:04 PM]

Share this post


Link to post
Share on other sites
jyk    2094
Cross-posting is a no-no here, so I'd delete your post in the other forum and leave this one (as it's a math-y question). Meanwhile, I'll take a look at your code and try to post something useful in a few minutes...

Share this post


Link to post
Share on other sites
jyk    2094
I'm not sure if there's enough information there to give a definite answer. For example, I'm not sure what the difference is between the 'foward' and 'direction' vectors of the car, or what the function changeForward() does.

Other than that though, some things look a little suspicious - it looks like you're 'going around the block to get next door', as it were, in terms of the car's rotation and direction vectors.

In any case, it looks like basically a 2D problem, so quaternions, Euler angles, or what have you probably aren't necessary. I imagine some simple trig and vector algebra will suffice. For further input though you'll probably have to give more details.

A couple of notes. Make sure you're converting to radians if necessary, and also, if both vectors are unit length, it's not necessary to divide by the magnitudes. Keep in mind that using acos() returns the unsigned angle between the vectors; using atan2() will fix that (and also avoid possible numerical issues that can arise with acos()).

Share this post


Link to post
Share on other sites
PSD    122
ok well changefoward jus adds the turn vector to the current foward vector then normalises it. This changes the direction the car goes but the car doesnt face that direction. What two parameters would i use for atan2? because it asks for 2 doubles. The direction vector is the direction the car is moving in and the foward vector is the orientation of the car. I know these 2 are the same thing but i was gettin problems when i jus had 1 its because wen i reversed the car it got complicated so i brought in another 1.
Thanx for your help.

Share this post


Link to post
Share on other sites
PSD    122
Well i think i got it working, didn't realise the glRotatef takes the angle in degrees, been driving me mad cos i was converting everything to radians!!! Anyway its ok, not very accurate but will do for now. If anyone knows about any good car physics resources please let me know it will be very helpful.
I'm trying to get the camera to rotate correctly now, if I get stuck will post a reply.
Thanx for your help........

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this