2017-10-06 5 views
0

Ich arbeite an einem 3D-Spiel in LWJGL, und ich möchte eine First-Person-Stil-Kamera mit Mausunterstützung hinzufügen. Ich habe derzeit einen, aber es ist nicht glatt und funktioniert überhaupt nicht sehr gut. Ich würde auch gerne das Fenster fokussieren, also ist die Maus beschäftigt und nicht über dem Fenster. Wie kann ich das umsetzen?Eine First Person Style Kamera für LWJGL (Mausunterstützung)

Hier ist meine aktuelle Kamera Umsetzung:

public class Camera { 

private Vector3f position = new Vector3f(0,5,0); 
private float pitch = 10; 
private float yaw; 
private float roll; 

private float speed = 0.2f; 

public Camera() { 

} 

public void move() { 

    yaw = - (Display.getWidth() - Mouse.getX()/4); 
    pitch = (Display.getHeight()/1000) - Mouse.getY(); 

    if (pitch >= 90) { 
     pitch = 90; 
    } 
    else if (pitch <= -90) { 
     pitch = -90; 
    } 

    if (Keyboard.isKeyDown(Keyboard.KEY_W)) { 

     position.z += -(float)Math.cos(Math.toRadians(yaw)) * speed; 
     position.x += (float)Math.sin(Math.toRadians(yaw)) * speed; 

    } 
    else if (Keyboard.isKeyDown(Keyboard.KEY_S)) { 

     position.z -= -(float)Math.cos(Math.toRadians(yaw)) * speed; 
     position.x -= (float)Math.sin(Math.toRadians(yaw)) * speed; 

    } 

    if (Keyboard.isKeyDown(Keyboard.KEY_D)) { 

     position.z += (float)Math.sin(Math.toRadians(yaw)) * speed; 
     position.x += (float)Math.cos(Math.toRadians(yaw)) * speed; 

    } 
    else if (Keyboard.isKeyDown(Keyboard.KEY_A)) { 

     position.z -= (float)Math.sin(Math.toRadians(yaw)) * speed; 
     position.x -= (float)Math.cos(Math.toRadians(yaw)) * speed; 

    } 

    if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) { 

     position.y -= speed; 

    } 

    if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { 

     position.y += speed; 

    } 

    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { 
     System.exit(0); 
    } 

} 

public Vector3f getPosition() { 
    return position; 
} 

public float getPitch() { 
    return pitch; 
} 

public float getYaw() { 
    return yaw; 
} 

public float getRoll() { 
    return roll; 
} 

} 

Antwort

0

Sie sind Integer-Division zu tun, wenn Gier- und Nick Berechnung, das ist schlecht, weil das Ergebnis wird auch eine ganze Zahl sein. Um dies zu ändern Sie müssen nur einen Wert ein Schwimmer wie diese machen:

yaw = - (Display.getWidth() - Mouse.getX()/4f); 
pitch = (Display.getHeight()/1000f) - Mouse.getY(); 

Jetzt wird das Ergebnis viel präziser sein.

Obwohl ich diesen Code verwenden reccomend Ihre Stampfen und Gieren zu berechnen:

float rotationSpeed = 1.0; //You probably need to change this factor 
yaw -= Mouse.getDX()/rotationSpeed; 
pitch += Mouse.getDY()/rotationSpeed; 

getDX/Y() gibt die Bewegung auf der x/y-Achse seit dem letzten Mal getDX/Y() aufgerufen wurde.

Wenn Sie sich auf das Fenster konzentrieren möchten, rufen Sie einfach Mouse.setGrabbed (true).