[SOLVED] Limiting camera rotation?

I’m writing a gaming library right now, and I’ve accomplished so much yet somehow I can’t wrap my head around how to do this. Here’s the current method I plan on using to limit camera rotation.


public void limitXRotation(float minimum, float maximum) {}

Normally this would be a very simple mechanic to implement, for example:


if(rotx < minimum) rotx = minimum;
if(rotx > maximum) rotx = maximum;

Here comes my problem though, the library makes it so that the rotation value can only ever be a value between 0 and 360 (if the value becomes 361, it resets the value to 1). If I entered a value, for example, minimum = 20 and maximum = 120 there’d be no problem. But what if I want to limit the rotation between 280 and 80. This would be the most likely limitation on the rotation because this would be how I’d mimic a person looking up and down. I just can’t seem to grasp how I’d be able to accomplish this, because if the rotation value was 280, but the maximum was 80, the aforementioned code would set the rotation value back to 80.

I’m sorry if I’m not making much sense, I really don’t know how to word what I’m trying to accomplish properly.

Your range is 280-360, and then 0-80, which is an actual range of 0-160. You should be clamping your value from 0-160 first, then offsetting it by 280 (addition). Like you said, your library will use % 360 on the actual value to keep it in check, so even if you end up with 440 (which is 160 + 280), it’ll be clamped back to 80.


public void clampXRotation(float minimum, float maximum) {

   if (minimum > maximum) {
      float actualRange = (360 - minimum) + maximum;

      // move rotation to the actual range
      rotx += (360 - minimum);
      rotx %= 360;

      // now clamp between the actual range that has proper min/max values
      clampXRotation(0, actualRange);
      
      // move back to the old range with a larger minimum value
      rotx += minimum;
      rotx %= 360;
   } else {
      if (rotx < minimum) rotx = minimum;
      if (rotx > maximum) rotx = maximum;
   }

}

I had to modify it a lot to make it work with my code, and there were a few math errors, but with the basic concept I finally got it working. Thanks a ton mate!

Please share your solution so that others can build upon it too. :slight_smile:


	public float limitRotation(float currentRotation, float minimum, float maximum)
	{
		float finalRotation = currentRotation;

		
		if (minimum > maximum)
		{		
			float offsetRotation = currentRotation;
			float offsetMinimum;
			float offsetMaximum;
			float actualRange;
			
			actualRange = 360 - minimum + maximum;
			offsetMinimum =  minimum + (actualRange / 2);
			offsetMinimum %= 360;
			offsetMaximum =  maximum + (actualRange / 2);
			offsetMaximum %= 360;
			offsetRotation += 360 - minimum;
			offsetRotation %= 360;
			if(offsetRotation > minimum) offsetRotation -= 360;
			
			if(offsetRotation < offsetMinimum)
			{
				finalRotation = minimum;
			}
			if(offsetRotation > offsetMaximum) 
			{
				finalRotation = maximum;
			}
		}
		else
		{
			if (finalRotation < minimum) finalRotation = minimum;
			if (finalRotation > maximum) finalRotation = maximum;
			
		}

I’m nice enough to share :wink:

This probably isn’t the fastest way to do this, and I’ve got a little bit of optimization code here and there that I don’t feel like hunting down right now, but anyone who comes across this code just know it works, because it was a pain in the ass to do it haha