little problem with ai player direction

Started by
8 comments, last by Alexandre Brien 21 years, 8 months ago
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!
In Construction : http://www.gdev.org
Advertisement
could you please post your CVector::Angle() code.
sure thing:

const float inline Angle(const CVector& normal) const
{
return acosf(*this % normal);
}
In Construction : http://www.gdev.org
what has the modulo operator been overloaded with?
// dot product
const scalar_t operator%(const CVector &vec) const
{
return x*vec.x + y*vec.x + z*vec.z;
}
In Construction : http://www.gdev.org
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.
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
to find out which quadrant i am facing, i guess i can use this:

float m[16];
glGetFloatv(GL_MODELVIEW_MATRIX, m);
In Construction : http://www.gdev.org
all you have to do is check whether the x component of the direction vector is positive or negative. (see the pseudo code above)
lol, this works perfectly. You are a math god =P

thx a million man
In Construction : http://www.gdev.org

This topic is closed to new replies.

Advertisement