This topic is now archived and is closed to further replies.

little problem with ai player direction

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey i have 2 states for my ai, AI_SEEKING and AI_HUNTING. currently im trying to have seeking to work, but ive run into a problem. the enemy does seek me, but only like half the time, like as if the degree is messed up or something. People who read opengl game programming might be familiar with this code, maybe they did it wrong or something: float dirToPlayer; CVector diff; // the vector from the ai to the player diff.x = position.x - player->position.x; diff.z = position.z - player->position.z; distFromPlayer = sqrt(diff.x*diff.x + diff.y*diff.y + diff.z*diff.z); diff.Normalize(); dirToPlayer = RAD2DEG(diff.Angle(CVector(0,0,-1))); so far so good right?? then in my AI_SEEKING case: case AI_SEEKING: { direction= dirToPlayer+90; etc.... I have to add 90 to the dir else the direction is NEVER right, not sure why. but still, Id like to find out why the direction is correct only half the time. I was thinking its because my direction goes over 360 sometimes(because of the +90) but doing if((dirToPlayer+90)>360) direction= (dirToPlayer+90)-360; else direction= dirToPlayer+90; didnt fix it. any help is appreciated thx!

Share this post

Link to post
Share on other sites
well.... acosf returns a value between 0 and pi, and it looks like your looking for a direction between 0 and 2pi.

so, theres no need to check if the value is over 360, as you can be assured it will never be over 180.

you have to find out what quadrant your facing to see whether you need to increase the angle (by means of 360 - theta) or not, if you want an angle between 0 and 360.

im not exactly sure how you''d go about doing this, as im just learning about this stuff aswell.

im gonna experiment a lil more.

Share this post

Link to post
Share on other sites
a bit of pseudo code.

CVector v1 = diff
CVector axis = axis(0, 0, -1)

float angle = acosf( DotProduct( v1, axis) )

if( v1.x < 0)
angle = TWOPI - angle;

Direction = RAD2DEG(angle)

this seems to be working for me

Share this post

Link to post
Share on other sites