Jump to content
  • Advertisement
Sign in to follow this  
Kirl

limmiting rotation range

This topic is 3320 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 a little 2d planet with multiple anti air guns around it's surface pointing straight out from the center (saved these rotations as their 'rest' position). I want to limmit their rotation freedom to 90 degrees (rest-45, rest+45), but I'm having trouble with the logic. Currently I'm doing something like this to limmit their rotation:
if(gun._rotation > gun.rest+45)
   gun._rotation = gun.rest+45;
else if(gun._rotation < gun.rest-45)
   gun._rotation = gun.rest-45;
The _rotation values range from -180 to 180. I guess the problems lie around the wrapping point (-180 == 180). It confuses me to no end. So I was wondering what's the proper way to deal with rotational ranges etc. I foresee more problems regarding this when I want to set certain ranges of the surface to be either land or sea and I need to determine if an angle is within the sea or land range.

Share this post


Link to post
Share on other sites
Advertisement
I would suggest expressing the problem in terms of direction vectors and relative angles rather than absolute angles.

Let's say that instead of storing the 'rest' angle of the turret, you store the forward direction vector corresponding to this orientation. At any given time, you also have the current forward vector of the turret.

You want to impose the constraint that the unsigned angle between these two vectors never exceeds a given threshold (45 degrees in your example). In pseudocode, it might look something like this:
float angle = signed_angle(rest_forward, forward);
if (abs(angle) > max_angle) {
forward = rotate_vector(rest_forward, sign(angle) * max_angle)
}
This should effectively limit the rotation range of the turret to two times the threshold value.

I skipped over some details, so feel free to ask if you have questions.

Share this post


Link to post
Share on other sites
I'm not familiar with the terms signed/unsigned angle, but I might recognise the math itself. Can you unpsuedofy your code a bit? In particular the bit how to get the vector from the gun.rest angle.

Please simplify where you can, I'm not very good with the material but I do have an excellent book for trig help and reference.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kirl
I'm not familiar with the terms signed/unsigned angle, but I might recognise the math itself. Can you unpsuedofy your code a bit? In particular the bit how to get the vector from the gun.rest angle.

Please simplify where you can, I'm not very good with the material but I do have an excellent book for trig help and reference.
To compute a forward vector from an angle (assuming the usual conventions for rotation in the xy plane):
forward.x = cos(angle);
forward.y = sin(angle);
To compute the signed angle between two 2-d vectors:
angle = atan2(perp_dot(v1, v2), dot(v1, v2));
Google or search the archives for 'perp dot' if you're not familiar with that operation.

If you need to store the angle as well as the forward direction vector, you'll probably need to convert the forward vector back to angle form after clamping it as described above, which can be done as follows:
angle = atan2(forward.y, forward.x);
You could also adapt the code I posted earlier to reset the angle directly rather than resetting the forward vector, although with this approach you'll have to deal with periodicity.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!