Sign in to follow this  

Turning my enemies

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

I have two angles in the range of 0 to 2pi. One is the current angle of the enemy, and the other is the angle it needs to turn to. Based on the angles, I need to know weather a left or right turn is faster. I also need some way of telling when to stop turning, when the enemies angle has overshot the goal angle. I don't see any simple way of doing this, any ideas? Note: I pulled the angle of my enemy out by using atan on its look vector. When I want to turn to something, I use an x,y point on my terrain, and the x,y of the enemy. I then come up with an angle from that in which I use as my goal angle. Is this a good system? Thank you Dev578

Share this post


Link to post
Share on other sites
This problem can be expressed in a way that eliminates the 'which direction should I turn' problem. Instead of thinking of it terms of a current angle and a target angle, just find the relative rotation (signed angle) that will align the object with the target. You can find this angle as follows:
Vector2 diff = target - position;
float angle = atan2(perp_dot(forward,diff), dot(forward,diff));
The angle returned is an exact solution, as it represents both the direction you need to turn and how much.

If you don't want to 'snap' directly to the target, you can use the information in other ways. For example, you could just turn in the appropriate direction by a fixed amount, or by the magnitude of the angle, whichever is less, or you could interpolate to the goal orientation.

As for stopping rotation when aligned with the target, I would simply not change the object's orientation if the angle as computed above is below some small value.

Share this post


Link to post
Share on other sites
For that last problem (checking when to stop rotating), it would be ideal to stop rotating the object if the difference between the target angle and the current angle is less than the speed of rotation. If that's true, then set the current angle to the target angle, else keep rotating.

Share this post


Link to post
Share on other sites

This topic is 4102 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this