Public Group

# having the bots face the player

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

## Recommended Posts

There must be something here I don't understand. What I want is an angle that is from -360 to 360. float angle = ((cos((bx * px) + (bz * pz)) * 180) / 3.141592654); The above bx, bz, px, pz are all normalized vectors. The angle is always positive and doesn't go above 60.0. Why is this? Bah!

##### Share on other sites
Why do you want an angle between -360 and 360? Shouldn't that be 0 and 360

##### Share on other sites
float angle = arccos(bx * px) * 180.0f / PI;

Would this perhaps be more correct (assuming bx * px is the dot-product)?

EDIT:
I might have gotten this wrong. What exactly is bx and px?

##### Share on other sites
First of al you need to use the "acos()" function if you want to know the angle.
And the acos() function will return a value ranging from 0radians to 3.141592654radians.
This you can convert into degrees by multiplying by 180/3.141592654

##### Share on other sites
Quote:
 Original post by Hellsp4wnfloat angle = arccos(bx * px) * 180.0f / PI;Would this perhaps be more correct (assuming bx * px is the dot-product)?

This is definitely a simpler way. However, you'll also need to add a cross product between the desired vector and the current forward vector to distinguish between clockwise and counter-clockwise. The above will always return a positive result.

There's a way to do angle between using some other trig functions. the problem with arccos is that you can crash it when your arguments are out of bounds due to floating point imprecision. I don't remember the other technique: it's something i re-learn about once a year and then promptly forget. =)

[EDIT: also as an aside, it's better to store all your angles in radians since that's the "native" angle format. Typically you do a one time conversion at load time between designer tuned angles in degress and your radian format. Otherwise you waste lots of cycles continually doing degree->radian conversions]

-me

##### Share on other sites
<< Why do you want an angle between -360 and 360? Shouldn't that be 0 and 360 >>

Good point. That would work too.

<< Would this perhaps be more correct (assuming bx * px is the dot-product)? >>

The equasion (bx * px) + (bz * pz) is the dot product. bx & bz is the normalized vector of the player point to the bot point. px & pz is the normalized vector of the bot point and the direction point the bot is facing.

acos seems to do the same thing as cos only returning a different sign.

##### Share on other sites
I have a feeling my data may be a little off.

##### Share on other sites
Quote:
 Original post by howie_007<< Why do you want an angle between -360 and 360? Shouldn't that be 0 and 360 >>Good point. That would work too.<< Would this perhaps be more correct (assuming bx * px is the dot-product)? >>The equasion (bx * px) + (bz * pz) is the dot product. bx & bz is the normalized vector of the player point to the bot point. px & pz is the normalized vector of the bot point and the direction point the bot is facing.acos seems to do the same thing as cos only returning a different sign.

If acos is the Arccos function, it should return the inverse of cos, so if you did y = arccos( cos(pi) ), the result would be y = pi.

##### Share on other sites
I want to thank everyone for their help. Problem solved and it works better then I thought it would!

It was a combination of how I was using my data and the use of acos.

Thanks!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634067
• Total Posts
3015323
×