0
\$\begingroup\$

I am quite new to LWJGL and OpenGL and I am trying to create an FPS camera. I had no problem with 2D stuff, so I decided to step things up and try and make some things in 3D. I have this 3D camera which I am working on, but for some reason my rotation doesn't seem to be working. The pitch and the yaw values are always 0.

Here is my code:

My3DGame.java

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.glu.GLU;

public class My3DGame {

private static My3DGame instance;

private final int DISPLAY_WIDTH = 840;
private final int DISPLAY_HEIGHT = 480;
private final String DISPLAY_TITLE = "My 3D Game";

private final int FPS_CAP = 60;

public My3DGame() {
    try {
        Display.setDisplayMode(new DisplayMode(DISPLAY_WIDTH, DISPLAY_HEIGHT));
        Display.setTitle(DISPLAY_TITLE);
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
    }

    init();
    initGL();

    while (!Display.isCloseRequested()) {
        render();
        update();
    }

    Display.destroy();
}

Camera camera;

private void init() {
    try {
        Mouse.create();
        Keyboard.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
    }

    camera = new Camera(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.15f, 2.4f);
    Mouse.setGrabbed(true);
}

private void initGL() {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    GLU.gluPerspective(45.0f, ((float) DISPLAY_WIDTH) / ((float) DISPLAY_HEIGHT), 3.2f, 2.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

private void update() {

    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) && Mouse.isGrabbed())
        Mouse.setGrabbed(false);

    if (Mouse.isButtonDown(0) && !Mouse.isGrabbed())
        Mouse.setGrabbed(true);

    if (!Mouse.isGrabbed())
        return;

    Mouse.setCursorPosition(DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2);

    camera.update();
}

private void render() {
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_QUADS);
    {
        glColor4f(3.2f, 23.1f, 0.2f, 1.0f);
        glVertex3f(100, 200, 1);
        glVertex3f(200, 200, 1);
        glVertex3f(200, 100, 1);
        glVertex3f(100, 100, 1);
    }
    glEnd();

    Display.update();
    Display.sync(FPS_CAP);
}

public static void main(String[] args) {
    instance = new My3DGame();
}

public static My3DGame getInstance() {
    return instance;
}

}

Camera.java

import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTranslatef;

import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.util.vector.Vector3f;

public class Camera {

private Vector3f position = null;
private float pitch = 0.0f;
private float yaw = 0.0f;

private float sensitivity = 0.0f;
private float moveSpeed = 0.0f;

public Camera(float x, float y, float z, float pitch, float yaw, float sensitivity, float moveSpeed) {
    position = new Vector3f(x, y, z);
    this.pitch = pitch;
    this.yaw = yaw;
    this.moveSpeed = moveSpeed;
}

public Camera() {

}

public void pitch(float amount) {
    pitch += amount;
}

public void yaw(float amount) {
    yaw += amount;
}

public void walkForward(float distance) {
    position.x -= distance * (float) Math.sin(Math.toRadians(yaw));
    position.z += distance * (float) Math.cos(Math.toRadians(yaw));
}

public void walkBackwards(float distance) {
    position.x += distance * (float) Math.sin(Math.toRadians(yaw));
    position.z -= distance * (float) Math.cos(Math.toRadians(yaw));
}

public void strafeLeft(float distance) {
    position.x -= distance * (float) Math.sin(Math.toRadians(yaw - 90));
    position.z += distance * (float) Math.cos(Math.toRadians(yaw - 90));
}

public void strafeRight(float distance) {
    position.x -= distance * (float) Math.sin(Math.toRadians(yaw + 90));
    position.z += distance * (float) Math.cos(Math.toRadians(yaw + 90));
}

float dx = 0.0f;
float dy = 0.0f;
float dt = 0.0f;
long lastTime = 0L;
long time = 0L;

public void update() {

    time = Sys.getTime();
    dt = (time - lastTime) / 1000.0f;
    lastTime = time;

    dx = Mouse.getDX();
    dy = Mouse.getDY();

    yaw(dx * sensitivity);
    pitch(dy * sensitivity);

    System.out.println("DT: " + dt);

    if (Keyboard.isKeyDown(Keyboard.KEY_W)) // Move forward
    {
        walkForward(moveSpeed * dt);
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_S)) // Move backwards
    {
        walkBackwards(moveSpeed * dt);
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_A)) // Strafe left
    {
        strafeLeft(moveSpeed * dt);
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_D)) // Strafe right
    {
        strafeRight(moveSpeed * dt);
    }

    System.out.println("Position: " + position.toString() + ", Pitch: " + pitch + ", Yaw: " + yaw);

    glLoadIdentity();

    glRotatef(pitch, 1.0f, 0.0f, 0.0f);
    glRotatef(yaw, 0.0f, 1.0f, 0.0f);
    glTranslatef(position.x, position.y, position.z);
}

}

\$\endgroup\$
  • \$\begingroup\$ make that edit an answer so we know you solved it (and how for future readers) \$\endgroup\$ – ratchet freak Jan 8 '14 at 9:58
  • \$\begingroup\$ Ok, I'll do that now, I have to wait a day before I can accept it as my answer though. \$\endgroup\$ – user35753 Jan 9 '14 at 2:21
0
\$\begingroup\$

I have fixed this now. I forgot to set the mouse sensitivity value in the constructor of Camera, which meant the sensitivity float was equal to 0.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy