little problem with ai player direction
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!
sure thing:
const float inline Angle(const CVector& normal) const
{
return acosf(*this % normal);
}
const float inline Angle(const CVector& normal) const
{
return acosf(*this % normal);
}
// dot product
const scalar_t operator%(const CVector &vec) const
{
return x*vec.x + y*vec.x + z*vec.z;
}
const scalar_t operator%(const CVector &vec) const
{
return x*vec.x + y*vec.x + z*vec.z;
}
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.
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
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);
float m[16];
glGetFloatv(GL_MODELVIEW_MATRIX, m);
all you have to do is check whether the x component of the direction vector is positive or negative. (see the pseudo code above)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement