Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  LWJGL Camera Pitch and Yaw  (Read 1316 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2014-03-16 17:25:45 »

Well I decided about a week ago to try (again) to learn LWJGL, and it's actually a very easy library now that I have more experience. It's a lot of fun to use. Once deciding to move onto 3D, which I'm more used to, I started following SHC's tutorials. They're very nice, thanks for them if you see this.

After following his tutorial for a camera class, I decided to add in a few of my own things primarily pitch, roll, and yaw in order to make a first person 'flying' type camera for the fun of it. I have almost everything working perfectly, but for some reason my pitch isn't being applied to my movement correctly. If I look up at a slight angle I fly up at that angle perfectly fine, however if I look almost straight up, it just flies at what seems to be a 45 degree angle.
However, funnily enough when I go down while looking straight up, it works perfectly. The video below will show that as well.

Here's what I'm doing to calculate the movement:
1  
2  
3  
4  
5  
   public void move(float amount, float direction) {
      position.z += amount * Math.sin(Math.toRadians(rotation.y + 90 * direction + yaw));
      position.x += amount * Math.cos(Math.toRadians(rotation.y + 90 * direction + yaw));
      position.y += amount * Math.sin(Math.toRadians(pitch));
   }


The Y part is the part where I'm pretty much guessing, so that's most likely what's wrong.
As well as my rotations:
1  
2  
3  
4  
5  
6  
7  
8  
9  
   public void apply() {
      view.setIdentity();

      Matrix4f.rotate((float) Math.toRadians(rotation.x + pitch), xAxis, view, view);
      Matrix4f.rotate((float) Math.toRadians(rotation.y + yaw), yAxis, view, view);
      Matrix4f.rotate((float) Math.toRadians(rotation.z), zAxis, view, view);

      Matrix4f.translate(position, view, view);
   }


I'm a little shaky on matrix math, so excuse me if it's an elementary problem.

Also, here's a video demonstrating the problem, note that when I do go straight up and down, I am pressing the Spacebar (up) and Control (down) buttons. I'm using WASD for movement.

<a href="http://www.youtube.com/v/BTJ9chsecoM?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/BTJ9chsecoM?version=3&amp;hl=en_US&amp;start=</a>

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline The Lion King
« Reply #1 - Posted 2014-03-16 18:12:36 »

if you want this done correctly, the x, y , and z axis change every time you rotate.

This is how i did it in my 3D engine. In psuedo code

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  
37  
38  
39  
40  
public class Object
{
         vector4 xAxis = (1, 0, 0, 0);
         vector4 yAxis = (0, 1, 0, 0);
         vector4 up     = (0, 1, 0, 0);
         vector3 zAxis = (0, 0, 1, 0);
         mat4 totalRotation = identity;
         
        public void move(Direction axis)
        {
               switch(axis)
               {
                       case X:
                             position += xAxis * speed;
                             break;
                             ...
               }
         }
       
         public void rotate(Direction axis, float amount)
         {
                  switch(axis)
                  {
                          case X:
                                Mat4 extraRot = createMat4(xAxis, amount);
                                yAxis = extraRot * yAxis;  
                                zAxis = extraRot * zAxis;
                                totalRot = extraRot * totalRot;
                                break;
                          case Y:  //if you are doing an fps camera use up axis
                                     //to form the matrix, else use yAxis
                                Mat4 extraRot = createMat4(up, amount);
                                xAxis = extraRot * xAxis;  
                                yAxis = extraRot * yAxis;
                                zAxis = extraRot * zAxis;
                                totalRot = extraRot * totalRot;
                                break;
                        }
                  }
         }

"You have to want it more than you want to breath, then you will be successful"
Offline Slyth2727
« Reply #2 - Posted 2014-03-16 20:20:52 »

Okay, I understand the movement part, however I'm not clear on the rotation. How would I create a matrix out of a vector and a float?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline The Lion King
« Reply #3 - Posted 2014-03-16 23:30:55 »

Look into using Quaterions or AngleAxis matrices.

heres a good explanation of angle- axis: and it has code/etc

http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm

"You have to want it more than you want to breath, then you will be successful"
Online SHC
« Reply #4 - Posted 2014-03-17 14:55:33 »

I use this function to move around.

1  
2  
3  
4  
5  
6  
7  
8  
/**
 * Move the camera with an amount in a direction.
 */

public void move(float amount, float direction)
{
    position.z += amount * Math.sin(Math.toRadians(rotation.y + 90 * direction));
    position.x += amount * Math.cos(Math.toRadians(rotation.y + 90 * direction));
}

And my apply method.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
/**
 * Apply the camera's transformations.
 */

public void apply()
{
    // Make the view matrix an identity.
   view.setIdentity();

    // Rotate the view
   Matrix4f.rotate((float) Math.toRadians(rotation.x), xAxis, view, view);
    Matrix4f.rotate((float) Math.toRadians(rotation.y), yAxis, view, view);
    Matrix4f.rotate((float) Math.toRadians(rotation.z), zAxis, view, view);

    // Move the camera
   Matrix4f.translate(position, view, view);
}

And to move the model,

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  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
/**
 * Update logic
 */

public void update(long elapsedTime)
{
    if (isKeyDown(KEY_ESCAPE))
        end();

    // Look up
   if (isKeyDown(KEY_UP))
        camera.addRotation(-1f, 0, 0);

    // Look down
   if (isKeyDown(KEY_DOWN))
        camera.addRotation(1f, 0, 0);

    // Turn left
   if (isKeyDown(KEY_LEFT))
        camera.addRotation(0, -1f, 0);

    // Turn right
   if (isKeyDown(KEY_RIGHT))
        camera.addRotation(0, 1f, 0);

    // Move front
   if (isKeyDown(KEY_W))
        camera.move(0.1f, 1);

    // Move back
   if (isKeyDown(KEY_S))
        camera.move(-0.1f, 1);

    // Strafe left
   if (isKeyDown(KEY_A))
        camera.move(0.1f, 0);

    // Strafe right
   if (isKeyDown(KEY_D))
        camera.move(-0.1f, 0);

    // Move Up
   if (isKeyDown(KEY_Z))
        camera.addPosition(0, 0.1f, 0);

    // Move Down
   if (isKeyDown(KEY_X))
        camera.addPosition(0, -0.1f, 0);
}

Offline Slyth2727
« Reply #5 - Posted 2014-03-18 21:48:56 »

I know, I've been following you're tutorials as I said in the above post Smiley
They're really great, thanks for taking the time to write them.

Anyways, I've solved my issue by using Quaternions as my rotation stuff, as The Lion King suggested.

Thanks guys

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
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!