# AI character's rotation (facing direction)

This topic is 4996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My AI characters have a position vector, and a endposition vector, and they travel slowly to the endposition vector. Dynamically, the endposition may change, and this is all working fine. However, I am not sure how to have the character rotate to be facing the direction of the endposition vector. The character has a float rotation value, and for the player's character this works fine, as when they press 'a' or 'd' the endposition vector is rotated around the player character's position, and then I work out the rotation value by an amount I found through trial and error. However, because the AI characters's endposition isn't rotated at all (it simply stops being 20.0, 10.0, 10.0 and becomes 25.0, 10.0, 7.0, or whatever the new target position is) I don't know how to rotate their model to be facing their final position. Could anyone help?

##### Share on other sites
this->desired_angle = (atan2(this->yv*1200,this->xv*1200)/PI)*180+270;

keep track of how far your object has rotated and compare it to desired_angle.

this->yv and this->xv = is the difference between the start and end points divded by the eta which in this case is 1200

##### Share on other sites
I am sorry if I sound dumb, but I don't really follow that Traiger.

I am sure it's right, but I don't think I understand it. I don't suppose you or anyone else could explain how that or any other solution works?

##### Share on other sites
Sorry my method probably wouldn't work anyway it's written for 2d

##### Share on other sites
So you want endposition to rotate around position or what. How does your AI work? How does it effect endposition & position vectors?

##### Share on other sites
Leggy,

the issue is, you are changing the angle between the vector course the object is on and where you want him to be.

So, you have:

current position
current view
end position(this is the part that changes)

you need to figure out:

Azimuth and Elevation angles to adjust current view by to point at the new end position.

Azimuth is the direction on the X,Z plane so all of the math to get this angle will only involve x and z coords.

To figure this, we use the law of cosines: c^2 = a^2+b^2 - 2ab* cos(C) where a,b,c are sidesof a triangle and C is the angle opposite side c.

So:
a = magnitude(old endposition - current position)
b = magnitude(new endposition - current position)
c = magnitude(new endposition - old endposition)

C will then equal CSC((a^2 + b^2 - c^2)/2ab));

C then, is the angle to rotate your view around the Y axis to point in the new direction.

If your endpoint also changed elevation then you apply the same process except that you use the y and z coords and then rotate around the x axis.

I hope this helps get you started.

##### Share on other sites
That brings back memories from my old math class, Bullfrog.

I am at work now, but I'll try it out when I get home and let you know how it goes.

I can calculate magnitude, is CSC a function from math.h, or do I need to create that myself?

edit: And Darkwing, no I can rotate endposition just fine, what I need to rotate is the physical 3d model representing my AI character so that it is actually facing the same direction as the endposition vector.

##### Share on other sites
if you need only to rotate your model to face movement direction, you could use this function (I use it in my engine and works great)

float Angle(CVector3 a, CVector3 b)
{
CVector3 V = b - a;
return (atan2(V.x,V.z)*180/PI -90);

}

where "a" is the start position and "b" is the destination.

let me know if it works also for you :)

P.S.
if it doesn't work, or gives crappy results, try to change that "-90"

##### Share on other sites
Sorry leggy,

CSC is the term math classes always use. The math.h function is acosf().

As for:
Quote:
 if you need only to rotate your model to face movement direction, you could use this function (I use it in my engine and works great)float Angle(CVector3 a, CVector3 b) {CVector3 V = b - a;return (atan2(V.x,V.z)*180/PI -90);}where "a" is the start position and "b" is the destination.let me know if it works also for you :)P.S.if it doesn't work, or gives crappy results, try to change that "-90"

The only reason that you would have to play around with the -90 part s that you arent necessarily dealing with right triangles. If it gets you close enough though, that's great cause it will execute faster.

Have a good one...

[Edited by - bu11frog on November 12, 2004 10:33:14 AM]

##### Share on other sites
Bullfrog and Beserk, just want to say thanks.

I got it working using Beserk's method, and it's working great now.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002235
×