# ai rotation

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

## Recommended Posts

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.

##### Share on other sites

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.

##### Share on other sites
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?

##### Share on other sites
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

##### Share on other sites
thanks lol. ill need to do some studying. good thing im going to take courses lol.

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
thanks that clears it up a lot!

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by dantheman1337wait. 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.

That's what we're telling you how to figure out. You calculate the delta angle from the current facing angle. You then add that to the current facing angle and you get what you're looking for

I suppose you could do the same calculation with the toTarget vector and whatever you define as your default look vector. It's nicer to do relative though because then you can do nice smooth motion over several frames

-me

##### Share on other sites
oh lol. sorry the other post by rethan confused me. yeah i have the mentality of a middle schooler

##### Share on other sites
Quote:
 Original post by dantheman1337oh lol. sorry the other post by rethan confused me. yeah i have the mentality of a middle schooler

Well, Rethan really just re-phrased my answer with more math detail.

Short story: you really need to go learn what vectors are and what the various basic operations like dot product, cross product, etc mean. The fundamental problem you're having is a lack of math knowledge. [smile]

-me

##### Share on other sites
could you also show me an example? just to make sure im doing it properly please

##### Share on other sites
Quote:
 Original post by dantheman1337could you also show me an example? just to make sure im doing it properly please

Why don't you show us what you have and we can help you correct it. You won't learn anything by just copy/pasting our code

-me

##### Share on other sites
oh yeah i am lol. im not in highschool lol. im in accelerated algebra lol. so i havn't really gotten to this subject yet

##### Share on other sites
and true. brb. ill get my code

##### Share on other sites
heres what i have. i think its inproper:

float angle;
angle = (((e_ang_x * (px - ex)) + ((e_ang_y * (py - ey)) + ((e_ang_z * (pz - ez));

please note that the e_ang_ is the enemies angles in an array. p. is the player coordinate and e. is the enemy coords

##### Share on other sites
ok.

I would first suggest that you forget about 3D until you understand the math. I would make this game as a top-down 2D shooter to start. You'll need way less linear algebra to make it work, and it will force you to learn some anyway.

but here's the deal in 3D, and it's not as trivial as it seems on the face of things. I'm also sure i've omitted some steps or gotten a couple things a little wrong... been a long time since i've implemented this stuff from scratch.

For simplicity, make it so that no units in your game can ever have roll, only pitch and yaw. Also clamp pitch so that they can never look more than just less than 90 degrees up or down. This should save you from having to deal with gimbal lock.

Math tools you will need:
Know what a vector is
Know how to take dot products and cross products of 2 vectors
know how to normalize a vector
Know what a matrix is
Know how to construct a rotation matrix from euler angles
Know how to multiply a matrix by a vector

All the vector stuff is pretty trivial. Matrices are more complicated but you can get away with just looking up implementations.

1) Find the current Enemy look vector:
Matrix rotMatrix = rotMatrixEuler( e_ang_x, e_ang_y, e_ang_z );vec3 lookVector = rotMatrix * defaultLookVector;lookVector.normalize();

2) Find the Desired facing vector
vec3 desiredVector = target.position - enemy.position;

3) Calculate delta yaw
//zero out z b/c we're just interested in yawvec3 lookNoZ( lookVector.x, lookVector.y, 0 );vec3 desiredNoZ( desiredVector.x, desiredVector.y, 0 );lookNoZ.normalize();desiredNoZ.normalize()float deltaYaw = dotProduct( lookNoZ, desiredNoZ );

4) calculate delta pitch
//create a rotation matrix using the new total yaw and the old pitch//post-multiply that matrix by the default facing vector//this will create a new vector that is facing the enemy in the XY-plane//but which is angled at the current pitch//take the dot product of that vector and the desiredVector to find an angle//this angle will be your delta pitch

fun...

-me

##### Share on other sites
Other benefits of 2D:

- you don't need 3D models
- you don't need to learn how to do 3D animation and rigged skeletal animation
- lighting is way easier
- collision is way easier

I mean, don't get me wrong. My first game ever was a 3D shooter with an engine written from scratch. It's a fantastic learning experience. It just took 2 years to make and I never did get around to doing good animation or lighting [smile]

-me

##### Share on other sites
2 things. im programming on a mac so i cant have the vec3 library. im already making the game and i have a lot achieved so im nor going to trash it and make a new game

edit-

sorry i forgot a third thing. thanks for all the help! i appreciate it because teaching myself to program is a pain so its nice to have a forum devoted to it as well as generous and patient people like yourself :P

edit-

haha is there anyway to do this process without vectors? like im willing to do something complex if necessary. i just need to know the process

##### Share on other sites
Quote:
 Original post by dantheman13372 things. im programming on a max so i cant have the vec3 library. im already making the game and i have a lot achieved so im nor going to trash it and make a new game

Fair enough

Anyway, none of what I posted is library calls. It's all something you have to write yourself. I should have disclaimed that at the top. Basically, I'm saying you need to:

a) learn the math
b) implement a math library (doesn't need to be a strict library obviously, just a collection of header and .cpp files or whatever)
c) use your math library to implement more or less what I posted

Again, what I posted is pseudo-code, it's not meant to compile. You'll have to learn the math & make it work

-me

##### Share on other sites
thanks for the info lol. i got slightly confused about that. i knew it shouldn't compile its just the vec3 and e.t.c. and um, like i asked before, is there away to do this with no matrixes and vectors?

i would appreciate any help. but im willing to take time and learn lol.

i had a method earlier where the ai could only look at 8 angles and it didnt look good at all.

##### Share on other sites
Quote:
 Original post by dantheman1337and um, like i asked before, is there away to do this with no matrixes and vectors?

If you're doing a 3D game you absolutely need to understand vectors and matrices. It is by far the easiest way to manage these problems. Without it you will not be able to get help from anyone and you will most likely be unable to solve the problems at hand.

You will need it for: animation, lighting, AI, weapons, particle effects... basically everything you will do in 3D programming.

Programming is math, get used to learning it.

-me

##### Share on other sites
oh. w ell then i got to get studying. i appreciate all your help :)