Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] Limiting camera rotation?  (Read 4991 times)
0 Members and 1 Guest are viewing this topic.
Offline shadowstryker
« Posted 2016-04-09 19:55:11 »

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.

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


Normally this would be a very simple mechanic to implement, for example:
1  
2  
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.
Offline chrislo27
« Reply #1 - Posted 2016-04-09 20:09:33 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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;
   }

}
Offline shadowstryker
« Reply #2 - Posted 2016-04-10 18:06:51 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2016-04-11 06:04:49 »

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


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline shadowstryker
« Reply #4 - Posted 2016-04-12 17:50:10 »

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



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
   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
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (581 views)
2019-09-04 15:33:17

hadezbladez (5510 views)
2018-11-16 13:46:03

hadezbladez (2402 views)
2018-11-16 13:41:33

hadezbladez (5772 views)
2018-11-16 13:35:35

hadezbladez (1223 views)
2018-11-16 13:32:03

EgonOlsen (4661 views)
2018-06-10 19:43:48

EgonOlsen (5682 views)
2018-06-10 19:43:44

EgonOlsen (3198 views)
2018-06-10 19:43:20

DesertCoockie (4095 views)
2018-05-13 18:23:11

nelsongames (5115 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!