Jump to content
  • Advertisement
Sign in to follow this  
howie_007

having the bots face the player

This topic is 4409 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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Hellsp4wn
float 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!