Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Camera that rotate and move on local axes (Convert local to global.)  (Read 511 times)
0 Members and 1 Guest are viewing this topic.
Offline AikoCraft

Junior Newbie





« Posted 2013-02-19 16:05:55 »

Hi all !

This is my problem, I need to convert local axis translating/rotating to global axis. (I won't use matrix, so please don't ask me to use them. I will use them only if there is no other way to solve my problem.)

(X axis = right/left) (Y axis = up/down) (Z axis = front/back)
On this picture we can see that the Camera rotated on its Local Z axis, it's normal that now the Y and X global/local axis aren't parallel and the Z global/local axis are.

 


So right now, we can assume that if the camera goes up its y position will increase but also its x position.

To do this mathematically in the game I did this :

-Create an up Vector.
    (Which store for each global axes the percentage that make them the axis nearest from the y Local)

    this code show how the up vector is computed. (Don't search errors here, I'm (almost) sure that it doesn't come from here)
   
1  
2  
3  
4  
5  
6  
public void getUp() {
    up.x = -Math.sin(Math.toRadians(zRot)) * Math.cos(Math.toRadians(yRot));
    up.y = Math.cos(Math.toRadians(xRot)) * Math.cos(Math.toRadians(zRot));      
    up.z = -Math.sin(Math.toRadians(xRot)) * Math.cos(Math.toRadians(yRot));
}
   



-Compute camera movement using up vector:


    In this code you can see that I just took the previous code and integrate to it the up vector.
    (I'm almost sure that there is a problem here)
   
   
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  
    public void moveForward() {               
    // This code was used before, it was used for a freefly camera wich can't rotate its local z axis
   //      pos.x += speed * Math.sin(Math.toRadians(yRot)) * Math.cos(Math.toRadians(xRot));
   //      pos.y += speed * Math.sin(Math.toRadians(xRot)) * Math.cos(Math.toRadians(zRot));
   //      pos.z += speed * Math.cos(Math.toRadians(yRot)) * Math.cos(Math.toRadians(xRot));
 
        pos.x += speed *
        (
        ( (up.x * Math.sin(Math.toRadians(xRot))) + (up.y * Math.sin(Math.toRadians(yRot))) + (up.z * Math.sin(Math.toRadians(zRot))) )
        *
        ( (up.y * Math.cos(Math.toRadians(xRot))) + (up.z * Math.cos(Math.toRadians(yRot))) + (up.x *   Math.cos(Math.toRadians(zRot))) )
        );
     
       pos.y += speed *
        (
        ( (up.y * Math.sin(Math.toRadians(xRot))) + (up.z * Math.sin(Math.toRadians(yRot))) + (up.x * Math.sin(Math.toRadians(zRot))) )
        *
        ( (up.z * Math.cos(Math.toRadians(xRot))) + (up.x * Math.cos(Math.toRadians(yRot))) + (up.y * Math.cos(Math.toRadians(zRot))) )
        );
     
        pos.z += speed *
        (
        ( (up.x * Math.cos(Math.toRadians(xRot))) + (up.y * Math.cos(Math.toRadians(yRot))) + (up.z * Math.cos(Math.toRadians(zRot))) )
        *
        ( (up.y * Math.cos(Math.toRadians(xRot))) + (up.z * Math.cos(Math.toRadians(yRot))) + (up.x * Math.cos(Math.toRadians(zRot))) )
        );
    }
   



-Compute camera rotations using up vector:

    The code speak better than me : (if you don't totally understand what i'm doing ask me ^^ !)
   
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   public void turnLocalX(float dist) {
      xGlobalRot += dist * up.y;
      yGlobalRot += dist * up.z;
      zGlobalRot += dist * up.x;
   }
   
   public void turnLocalY(float dist) {
      xGlobalRot += dist * up.x;
      yGlobalRot += dist * up.y;
      zGlobalRot += dist * up.z;        
   }
       // I still not did the turnLocalZ But it's the same principle.
   


But even after all of this, it doesn't work  Huh !
Do someone have an idea of where it comes from ?

Thank you for reading !

Sorry if I poorly explained what I mean, my english is not perfect ask me if there is something weird !
Also I am young and the only advanced math we saw at school is cos() and sin().. Emo Fortunately all my other math knowledge is from the Internet. So if you answer me with 'advanced' math, please explain a bit what you're posting !  Grin
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (35 views)
2014-09-24 16:13:29

Pippogeek (28 views)
2014-09-24 16:12:22

Pippogeek (17 views)
2014-09-24 16:12:06

Grunnt (40 views)
2014-09-23 14:38:19

radar3301 (23 views)
2014-09-21 23:33:17

BurntPizza (59 views)
2014-09-21 02:42:18

BurntPizza (29 views)
2014-09-21 01:30:30

moogie (34 views)
2014-09-21 00:26:15

UprightPath (47 views)
2014-09-20 20:14:06

BurntPizza (51 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59: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!