ai rotation

Started by
25 comments, last by dantheman1337 14 years, 1 month ago
okay, i've been having this one problem. im making a 3-d game called awakening. it's first person. can ayone help me with this one problem? i need to make an object roatate to face my position. how is this possible? i'm using opengl.
Advertisement
Some more info would help.

1 - If the object can rotate 100% freely -- e.g., it's a hovering ball -- you probably want to do the shortest rotation. Keyphrase: "quaternion SLERP" (you can also view it just as a particular axis-angle rotation, and interpolating the angle. Much info on this in the forums/online.)

2 - If the object is constrained to rotate in certain realistic ways -- say, it's a gun turret, or a humanoid character -- then you probably just want to interpolate Euler angles.

3 - If the problem is really 2d (e.g., you want an NPC or bot who cannot look up or down to simply turn and face the player), then you should just forget about the third dimension, in which case the solution will probably come to you quickly.
alright, well im noobish at programming. well, im actually somewhat good. cuz ive been programming over a year. but i need a way for a character to look at my character. the character only needs to rotate on one axis to face my characters position. if you are familiar with opengl, i would have to rotate on the y axis.



think of mario 64. the king bomb only rotates along the y axis to face mario. how do i do that?
If the terms vector, normalize, dot product and cross product are unfamiliar to you, google around. It is a domain of mathematics called "Linear Algebra" that you definitely need to learn

A basic algorithm that won't require quaternions or matrices (both of which you also need to learn, but let's start basic first):

dot product to find the smallest angle between the current normalized look vector and the normalized aiToEnemy vector.
cross product of the same 2 vectors and use the value of z (up or down) to figure out if you should rotate left or right

if you want it to turn smoothly over several frames then (dt is time since last frame which hopefully you're already passing through your update/tick functions)
angleToRotateThisFrame = min( max_angle_per_second*dt, totalAngle );

I would also establish a "close enough" angle so that you don't get jittering due to floating point imprecision.

-me
thanks lol. ill need to do some studying. good thing im going to take courses lol.
okay. ive been researching and im kinda confused. i know how to normalize vectors and all. but lets say that the enemy's x, y, and z are equal to 0. that would mean that the dot product is 0.


so how exactly can i solve this. sorry. im not really smart about this topic.
No. You're dot producting the look vector (the enemy's facing vector) with the vector that points from the enemy to the target. Neither should ever be able to be zero length unless the enemy is standing directly on top of it's target.

A look vector is not the position. Usually you define something like (1,0,0) as the default look vector. to find a units look vector you transform the default vector by it's orientation matrix; or if we're staying away from matrices, then rotate that vector by it's current euler angle orientation

-me
You want to "do math" on two vectors:

- The "look" vector of the enemy, which doesn't care about the enemy's actual position. (What rotation are you using to orient this object for drawing etc, you want to use something that has to do with that for this vector and not the position.)

- The (target) vector made from the position of the player and the position of the enemy (basically, what the look vector of the enemy should be after turning.)

Once you have these two vectors, you can use the dot product to get the cos of the angle between them which will help you figure out how much to rotate the enemy so that the look vector matches the second (target) vector.
thanks that clears it up a lot!
wait. i dont care how much the enemy needs to rotate. i just want to know how to find the look vector. i want to know what angle it needs to face to be looking at the player.

This topic is closed to new replies.

Advertisement