trying to set the direction - Solved by if's

nord

17-07-2008 20:21:47

Hi.
I am trying to move my nxogre vehicle towards a destination point on the terrain. To do this, I have abstracted two functions for the vehicle as, AIvehicle->drive(Ogre::Real torquevalue) and AIvehicle->turn(bool direction) (turns the front wheels 1.5 degree on each frame).

I have got the current direction of the vehicle as :


//get orient. quaternion of the car
Quaternion orientation = AIvehicle->getBody()->getGlobalOrientation();

//consider only rotation relative to y axis(reduce the scene to 2D)
Ogre::Radian orientationRadian = orientation.getYaw(true);

//right now I know my car's orientation angle as radian

//create a unit vector that will convert the radian into a vector
Vector3 *currentDirection = new Vector3(Math::Sin(orientationRadian),0,Math::Cos(orientationRadian));

//prints the normalized direction_vector of the car
printf("CurrentDirection\nX : %f\nZ : %f\n",currentDirection->x,currentDirection->z);

nord

17-07-2008 20:24:55

everything works fine, that is when I look a direction with my car, I can see how much of the direction vector is projected on the x axis or z axis.

To move my car on a particular target point, first I create a vector from the position of my car to the destination_point, which is quite easy.


//get the destination position as a vector relative to origin (0,0,0).
Vector3 WPposition = target->getPosition();

nord

17-07-2008 20:25:14


//subtract AIvehicle's position from this vector to find the relative direction of the destination point
WPposition = WPposition - bora->getBody()->getGlobalPosition();

//right now WPposition's direction is exactly from car's position to destination. everything works fine until now...

//I want to closest angle between my direction vector and destination vector, so that I can understand which way to steer my car. To do this ....

//normalise the WPposition so that we can extract its angle.

WPposition.normalise();
//prints the normalized direction of the vector from car to wp
printf("the vector from car to wp\nX : %f\nY : %f\nZ : %f\n",WPposition.x,WPposition.y,WPposition.z);

//working fine ...

nord

17-07-2008 20:25:34

What I want to do is calculating the angle between my direction and target and steer accordingly.

As you have observed, I have succesfully converted a radian into a unit vector above (you may see on the first screenshot).

But when I try to convert a unit vector into radian I have encountered the problem on the second screenshot. I do this convertion with the following code.

Radian WPrad = Math::Asin(WPposition.x)

And since I cannot calculate the angle of WPposition vector correctly, I cannot decide which way to go.

I have told everything I did because I am not sure if it's the best way to do what I want to do. Thanks for sharing your time and reading all these =)



nord

17-07-2008 20:30:24

to make it more clear, My problem is simply this, I can convert a radian like in screenshot 1 into a unit vector.

But When I try to convert the unit vector back into radian, I get the degrees as in the second screenshot. Which is a little bit different though it should have been the same.

the problem is with the asin function. it should be generating angles between 0 to 180 and -180 to 0. but it generates 0 to 90 to 0, and 0 to -90 to 0.

I solved this manually by if statements. But can anyone tell me why asin behaves like this ?

betajaen

17-07-2008 22:49:45

I feel I should say something meaningful with a fix to your problem. But I tried to forget as much as possible about trigonometry in the previous semester of University.