ai rotation

Started by
25 comments, last by dantheman1337 14 years, 1 month ago
Quote:Original post by dantheman1337
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.


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
Advertisement
oh lol. sorry the other post by rethan confused me. yeah i have the mentality of a middle schooler
Quote:Original post by dantheman1337
oh 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
could you also show me an example? just to make sure im doing it properly please
Quote:Original post by dantheman1337
could 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
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
and true. brb. ill get my code
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
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
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

This topic is closed to new replies.

Advertisement