Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Troubles with the 3D Camera  (Read 1342 times)
0 Members and 1 Guest are viewing this topic.
Offline Bigabig

Junior Newbie





« Posted 2013-08-11 14:04:04 »

Hey Guys,
as I try to develop a little Voxel Engine to improve my 3D Skills I think this is the right place?

I started with some Cubes and Chunks and now I try to manage those Chunks... However, atm I have problems to set up a camera. I just want to fly around my Chunks and later implement a player, but for now I have trouble with the movement of the camera.

Here is my Camera.class:
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  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.util.vector.Vector3f;
import static org.lwjgl.opengl.GL11.*;

public class Camera {  
   //3d vector to store the camera's position in
    //the rotation around the Y axis of the camera
    //the rotation around the X axis of the camera
    public Vector3f position = null;
    private float yaw = 0.0f;
    private float pitch = 0.0f;
   private float movementSpeed = 5.0f;
   private float mouseSensitivity = 0.5f;
   
   private float dtt;
 
    /**
     * Creates a new Camera that takes the starting x, y, z location<br>
     * Call the update() method every frame to use the camera<br>
     * Do not forget to set the movementSpeed(default 10) and mouseSensitivity (default 0.5) by calling the
     * setMovementSpeed() and setMouseSensitivity() methods
     *
     * @param x x location
     * @param y y location
     * @param z z location
     */

    public Camera(float x, float y, float z)
    {
        position = new Vector3f(x, y, z);
    }

    /**
     * updates the camera<br>
     * CALL THIS <b>BEFORE</b> YOU RENDER YOUR SCENE!<br>
     * @param dt deltaTime for indie movement
     */

    public void update(float dt) {
       
       // ignore this :D
       dtt = dtt + dt;
       if(dtt >= 1) {
          System.out.println(position);
          dtt = 0;
       }
         
        //control camera yaw from x movement from the mouse
        yaw(-Mouse.getDX() * mouseSensitivity);
        //control camera pitch from y movement from the mouse
        pitch(Mouse.getDY() * mouseSensitivity);
       
        if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
           moveCamera(0, movementSpeed*dt);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
           moveCamera(180, movementSpeed*dt);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
           moveCamera(90, movementSpeed*dt);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
           moveCamera(270, movementSpeed*dt);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
            flyUp(movementSpeed*dt);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
            flyDown(movementSpeed*dt);
        }
        glLoadIdentity();
        lookThrough();
    }
   
    /**
     *    increment the camera's current yaw rotation
     * @param amount increment the yaw by the amount param
     */

    private void yaw(float amount) {
        yaw += amount;
        if(yaw > 359)
           yaw = yaw - 359;
        else if(yaw < 0)
           yaw = yaw + 360;
    }

    /**
     * increment the camera's current pitch rotation
     * @param amount increment the pitch by the amount
     */

    private void pitch(float amount) {
       pitch += amount;
       if(pitch > 90)
          pitch = 90;
       else if(pitch < -90)
          pitch = -90;
    }
   
    private void moveCamera(float direction, float distance) {
       position.x += Math.sin(Math.toRadians(yaw + direction)) * distance;
       position.z += Math.cos(Math.toRadians(yaw + direction)) * distance;
    }
   
    private void flyUp(float distance) {
       position.y += distance;
    }
   
    private void flyDown(float distance) {
       position.y -= distance;
    }

    /**
     * translates and rotate the matrix so that it looks through the camera
     */

    private void lookThrough() {
        //roatate the pitch around the X axis
        glRotatef(-pitch, 1.0f, 0.0f, 0.0f);
        //roatate the yaw around the Y axis
        glRotatef(-yaw, 0.0f, 1.0f, 0.0f);
        //translate to the position vector's location
        glTranslatef(-position.x, -position.y, -position.z);
    }
}

Maybe some of you can try out this class? There is something weird about the movement and I think thats because of positive and negative... Well I don't really got this Sad
I just wanted to write a camera based on this tutorial, but it seems not to work...
http://www.java-gaming.org/topics/creating-a-3d-camera/27906/view.html
 So 0 degrees should be moving forward, 180 degree backward, 270 right and 90 left.

Offline quew8

JGO Coder


Medals: 31



« Reply #1 - Posted 2013-08-11 14:58:59 »

Try translating to position before you rotate to face. If you rotate first then the translation is with respect to the new coordinate system defined by the rotation. (probably not what you want)
Offline Bigabig

Junior Newbie





« Reply #2 - Posted 2013-08-11 15:46:01 »

well, thanks for the reply, but jeah, thats not rly what I want Cheesy
This sounds correct for me:
1  
2  
3  
4  
void moveCamera(float direction) { // direction is in degrees
   pos.x += Math.sin(Math.toRadians(yaw + direction));
   pos.z += Math.cos(Math.toRadians(yaw + direction));
}

so lets say I look forward and move forward. yaw = 0, direction = 0
pos.x = 0;
pos.z = 1 * SPEED
This is correct math. But in fact it feels wrong, because pressing W for moving forward will make you moving backwards.! And thats what I don't understand.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline drabla

Junior Duke


Medals: 5



« Reply #3 - Posted 2013-08-11 15:51:08 »

Your forward is not fixed.

You give the movement direction as a fixed angle (0, 90, 180, ...) - this will result in moving along the z & x axis.

You have to calculate your direction every time you turn your camera.
Offline Bigabig

Junior Newbie





« Reply #4 - Posted 2013-08-11 15:57:48 »

right, my forward is not fixed.
and i don't give the movement direction as a fixed angle. I already calculate my direction every time i turn the camera.
and i think every movement will result in moving along the z & x axis Cheesy
I am just looking for the error in my code, why W = moving backwards, S = moving forward and maybe a little math explanation.
Offline quew8

JGO Coder


Medals: 31



« Reply #5 - Posted 2013-08-11 16:04:53 »

OK, I see. OpenGL uses a right handed coordinate system, which means that unless you do something funky, positive z is pointing out of the screen towards you. You can try it with your right hand - put thumb, 1st and 2nd finger (or 1st, 2nd, 3rd if you're a pianist) at 90 degrees to each other. Then put it against the screen with thumb pointing right (x axis), 1st finger pointing up (y axis) and see where your second finger (z axis) points.

This is one of the things that sets OpenGL apart from most other libraries like DirectX which tend to use left handed system by default.
Offline Bigabig

Junior Newbie





« Reply #6 - Posted 2013-08-11 16:14:59 »

OK, I see. OpenGL uses a right handed coordinate system, which means that unless you do something funky, positive z is pointing out of the screen towards you. You can try it with your right hand - put thumb, 1st and 2nd finger (or 1st, 2nd, 3rd if you're a pianist) at 90 degrees to each other. Then put it against the screen with thumb pointing right (x axis), 1st finger pointing up (y axis) and see where your second finger (z axis) points.

This is one of the things that sets OpenGL apart from most other libraries like DirectX which tend to use left handed system by default.
ahhh ok Cheesy moving in the positiv z direction looks like moving backwards. thank you Cheesy
looking forward and moving forward. yaw = 0, direction = 0
pos.z = cos(0 + 0)
pos.z = 1!!! so this should make the camera moving into the screen, but the opposite is true. I got it! Cheesy
Offline davedes
« Reply #7 - Posted 2013-08-11 16:23:55 »

This is one of the things that sets OpenGL apart from most other libraries like DirectX which tend to use left handed system by default.
Just a note... Modern OpenGL doesn't use one or the other. In the programmable pipeline, you need to construct your own projection and modelview matrices.

This is different from the fixed-function pipeline (glTranslate, glRotate, glOrtho, etc), which became deprecated five years ago.

Offline Bigabig

Junior Newbie





« Reply #8 - Posted 2013-08-11 16:31:33 »

This is one of the things that sets OpenGL apart from most other libraries like DirectX which tend to use left handed system by default.
Just a note... Modern OpenGL doesn't use one or the other. In the programmable pipeline, you need to construct your own projection and modelview matrices.

This is different from the fixed-function pipeline (glTranslate, glRotate, glOrtho, etc), which became deprecated five years ago.
Modern OpenGL? so OpenGL > 3?
"You need to construct your own projection and modelview matrices" sounds really hard... is this very complicated? I am asking, because I just started using OpenGL so I might swap to the "modernGL" Cheesy
Is there anything you can recommend to read or to look up?
Offline quew8

JGO Coder


Medals: 31



« Reply #9 - Posted 2013-08-11 17:06:57 »

Its not massively complicated. The documentation for OpenGL actually shows how it constructs the matrices in all of the glTranslate, glRotate etc functions. The LWJGL util.vector.Matrix4f class provides all you need to perform operations on matrices. You could probably do this with very little understanding of the transformation matrices, I wouldn't recommend it but you could.

So a couple of links:

http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/
Still the best tutorial for beginners I've ever found. (IMO)

http://www.opengl.org/sdk/docs/man2/
The OpenGL docs to see how "they" do it. Make sure you view this in a browser with support for proper mathematical notation. (Chrome and (I think) IE don't out of the box for example) Firefox works without any extra plugins but I couldn't say for any other browsers.
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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!