LWJGL Kamerabug

florilu

Aktives Mitglied
Hi,

ich habe ein witziges, interessantes und nerviges Problem.

Meine Kamera, die ich jetzt mal endlich in meine Bibliothek gepackt habe, habe ich verbessert und kleiner vom Code her gemacht, schön und gut, aber mein Problem ist, wenn ich nach links gucken will und meine rotation (yaw) auf 0 ist das ich dann einfach nicht nach links gucken kann, habe ich aber mich rechts gedreht, dann kann ich mich nach links drehen, bis zu dem Punkt 0.

Sonst funktioniert alles. Ich wollte mir eine Funktion einbauen, die es mir ermöglicht wenn ich nach unten gucke das ich meine Kamera nicht um 360° drehen kann, sondern das ich noch auf den Boden schau, das gleiche nach oben, das funktioniert aber nur das nach links gucken nicht.

Der Code:
Camera:
Java:
package Methods.Cameras;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;


public class FPSCamera
{
    //3d vector to store the camera's position in
    public Vector3f    position    = null;
    //the rotation around the Y axis of the camera
    public float       yaw         = 0.0f;
    //the rotation around the X axis of the camera
    public float       pitch       = 0.0f;
    
    public static int mouseSpeed = 2;
	public static int walkingSpeed = 10;
	public static final int maxLookDown = -85;
	public static final int maxLookUp = 85;
    //Constructor that takes the starting x, y, z location of the camera
    public FPSCamera(float x, float y, float z)
    {
        //instantiate position Vector3f to the x y z params.
        position = new Vector3f(x, y, z);
    }
    public FPSCamera() { // This is made so the line FPCameraController app = new FPCameraController(); will work
		// TODO Auto-generated constructor stub
	}
	//increment the camera's current yaw rotation
    public void yaw(float amount)
    {
        //increment the yaw by the amount param
        yaw += amount;
    }

    //increment the camera's current yaw rotation
    public void pitch(float amount)
    {
        //increment the pitch by the amount param
        pitch += amount;
    }
    //moves the camera forward relitive to its current rotation (yaw)
    public void walkForward(float distance)
    {
    		position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
    		position.z += distance * (float)Math.cos(Math.toRadians(yaw));
    }

    //moves the camera backward relitive to its current rotation (yaw)
    public void walkBackwards(float distance)
    {    	
    		position.x += distance * (float)Math.sin(Math.toRadians(yaw));
    		position.z -= distance * (float)Math.cos(Math.toRadians(yaw));
    }

    //strafes the camera left relitive to its current rotation (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));
    }

    //strafes the camera right relitive to its current rotation (yaw)
    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));
    }
    
    public void moveUp(float distance){
    	position.y -= distance;
    }
    
    public void moveDown(float distance){
    	position.y += distance;
    }

    //translates and rotate the matrix so that it looks through the camera
    //this dose basic what gluLookAt() does
    public void lookThrough()
    {
        //roatate the pitch around the X axis
        GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
        //roatate the yaw around the Y axis
        GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
        
        if (Mouse.isGrabbed()) {
            float mouseDX = Mouse.getDX() * mouseSpeed * 0.16f;
            float mouseDY = Mouse.getDY() * mouseSpeed * 0.16f;
            if (yaw + mouseDX >= 360) {
                yaw = yaw + mouseDX - 360;
            } else if (yaw + mouseDX < 0) {
                yaw = 360 - yaw + mouseDX;
            } else {
                yaw += mouseDX;
            }
            if (pitch  - mouseDY >= maxLookDown && pitch  - mouseDY <= maxLookUp) {
                pitch  += -mouseDY;
            } else if (pitch  - mouseDY < maxLookDown) {
                pitch  = maxLookDown;
            } else if (pitch  - mouseDY > maxLookUp) {
                pitch  = maxLookUp;
            }
        }
        //translate to the position vector's location
        GL11.glTranslatef(position.x, position.y, position.z);
    }
}

Main:
Java:
import java.awt.event.KeyEvent;

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

import Methods.Methods;
import Methods.Cameras.FPSCamera;
import Methods.Cubes.DefaultCube;


public class Main 
{
	private static boolean gameRunning=true;
    private static int targetWidth = 800;
    private static int targetHeight = 600;
    
    public static boolean printFPS = false;
	private static int fps;
	private static int lastFPS;
	private static int lastFrame;
 
    private void init(){
        //load textures here and other things
    }
 
    private float xrot=0.1f;
    private float yrot=0.1f;
    private float zrot=0.1f;
    
    /** The texture that’s been loaded */
    
    
    private static void initDisplay(boolean fullscreen){
 
        DisplayMode chosenMode = null;
 
        try {
                DisplayMode[] modes = Display.getAvailableDisplayModes();
 
                for (int i=0;i<modes.length;i++) {
                    if ((modes[i].getWidth() == targetWidth) && (modes[i].getHeight() == targetHeight)) {
                        chosenMode = modes[i];
                        break;
                    }
                }
            } catch (LWJGLException e) {
        Sys.alert("Error", "Unable to determine display modes.");
        System.exit(0);
        }
 
        // at this point if we have no mode there was no appropriate, let the user know
    // and give up
        if (chosenMode == null) {
            Sys.alert("Error", "Unable to find appropriate display mode.");
            System.exit(0);
        }
 
        try {
            Display.setDisplayMode(chosenMode);
            Display.setFullscreen(fullscreen);
            Display.setTitle("3D Game Engine");
            Display.create();
 
        }
        catch (LWJGLException e) {
            Sys.alert("Error","Unable to create display.");
            System.exit(0);
        }
 
}
 
    private static boolean initGL(){
    	 GL11.glMatrixMode(GL11.GL_PROJECTION);
         GL11.glLoadIdentity();
  
//         Calculate the aspect ratio of the window
         GLU.gluPerspective(45.0f,((float)targetWidth)/((float)targetHeight),0.1f,100.0f);
         GL11.glMatrixMode(GL11.GL_MODELVIEW);
         GL11.glLoadIdentity();
  
         GL11.glEnable(GL11.GL_TEXTURE_2D);                                    // Enable Texture Mapping ( NEW )
         GL11.glShadeModel(GL11.GL_SMOOTH);
         GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
         GL11.glClearDepth(1.0f);
         GL11.glEnable(GL11.GL_DEPTH_TEST);
         GL11.glDepthFunc(GL11.GL_LEQUAL);
         GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
        return true;
    }
        public boolean isKeyPressed(int keyCode) {
                // apparently, someone at decided not to use standard
 
                // keycode, so we have to map them over:
 
                switch(keyCode) {
                case KeyEvent.VK_SHIFT:
                	keyCode = Keyboard.KEY_LSHIFT;
                	break;
                }
 
                return org.lwjgl.input.Keyboard.isKeyDown(keyCode);
        }
 
    private void run(){
      FPSCamera camera = new FPSCamera(0, 0, 0);
 
            float dx        = 0.0f;
            float dy        = 0.0f;
            float dt        = 0.0f; //length of frame
            float lastTime  = 0.0f; // when the last frame was
            float time      = 0.0f;
 
            float mouseSensitivity = 0.15f;
            float movementSpeed = 10.0f; //move 10 units per second
 
            //hide the mouse
            Mouse.setGrabbed(true);
        while(gameRunning){
            update();
            render();
            Display.update();
            Display.setTitle("X: "+camera.position.x+" Y: "+camera.position.y+" Z: "+camera.position.z);
            
            String jpg = "jpg";
            String png = "png";
            String txt = "txt";
            
                time = Sys.getTime();
               
                //here is your movement speed, which can be changed to anything
                dt = 0.0005f;
               
                lastTime = time;
 
 
                //distance in mouse movement from the last getDX() call.
                dx = Mouse.getDX();
                //distance in mouse movement from the last getDY() call.
                dy = Mouse.getDY();
 
                //control camera yaw from x movement from the mouse
                camera.yaw(dx * mouseSensitivity);
                //control camera pitch from y movement from the mouse
                camera.pitch(-dy * mouseSensitivity);
 
 
                //when passing in the distrance to move
                //we times the movementSpeed with dt this is a time scale
                //so if its a slow frame u move more then a fast frame
                //so on a slow computer you move just as fast as on a fast computer
               
                //OVER HERE! What do I do to make the boolean canWalk actually work the right way?
                
                //If Statements for Camera Controls
                if(Keyboard.isKeyDown(Keyboard.KEY_W)){
                	camera.walkForward(movementSpeed * dt); 
                }if(Keyboard.isKeyDown(Keyboard.KEY_S)){
                	camera.walkBackwards(movementSpeed * dt);
                }if(Keyboard.isKeyDown(Keyboard.KEY_A)){
                	camera.strafeLeft(movementSpeed * dt);
                }if(Keyboard.isKeyDown(Keyboard.KEY_D)){
                	camera.strafeRight(movementSpeed * dt); 
                }if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
                	camera.moveUp(movementSpeed * dt);
                }if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){
                	camera.moveDown(movementSpeed * dt);
                }if(Keyboard.isKeyDown(Keyboard.KEY_P)){
                	if(Mouse.isGrabbed()){
                		Mouse.setGrabbed(false);
                	}else{
                		Mouse.setGrabbed(true);
                	}
                }if(Keyboard.isKeyDown(Keyboard.KEY_F1)){
                	Methods.screenShot();
                }if(Keyboard.isKeyDown(Keyboard.KEY_F)){
                	if(printFPS == true){
                		printFPS = false;
                	}else{
                		printFPS = true;
                	}
                }
 
                //set the modelview matrix back to the identity
                GL11.glLoadIdentity();
                
                //updateFPS();
                //look through the camera before you draw anything
                camera.lookThrough();
                //you would draw your scene here.
 
                //draw the buffer to the screen
                //Display.update();
            //}
 
            // finally check if the user has requested that the display be
            // shutdown
            if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
                    gameRunning = false;
                    Display.destroy();
                    System.exit(0);
                    }
        }
    }
 
    private void update(){
        xrot+=0.1f;
        yrot+=0.1f;
        zrot+=0.1f;
    }
 
    private void render(){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
        //GL11.glLoadIdentity();
        
        //Drawing Method
        DefaultCube.draw();
    }
 
    public static void main(String[] args)
{
        Main main = new Main();
        initDisplay(false);
                initGL();
                main.init();
                main.run();
        }
}

Das Programm, damit ihr das Problem selbst testen könnt: (.exe Datei)
3DTests.exe

MfG:
Florilu
 
Zuletzt bearbeitet:

Marco13

Top Contributor
Also, ich hab' diese EXE einfach mal downgeloaded und gestartet, und jetzt blinkt dauernd das Licht an meiner Webcam und das CD-Laufwerk geht dauernd auf und zu :bahnhof:

:joke:

In der lookThrough begrenzt du das yaw ja auch auf positive Werte. Statt der ganzen if's ein
yaw = (yaw + mouseDX) % 360;
reicht schon
 

florilu

Aktives Mitglied
Hey danke, hat geklappt :D Ich habe schon so viel probiert, viele andere Methoden, blablabla, und dieses einfache Stückchen ist die Lösung, währ nie drauf gekommen :)
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
coolian lwjgl glfw window zeigt nur grau an Spiele- und Multimedia-Programmierung 0
coolian slick lwjgl text darstellen mit UnicodeFont funktoniert nicht? Spiele- und Multimedia-Programmierung 11
F OpenGL (LWJGL) Shader Programmierung GLSL Spiele- und Multimedia-Programmierung 2
Meeresgott LWJGL 3 Problem mit einer Texture Spiele- und Multimedia-Programmierung 4
V LWJGL GUI Spiele- und Multimedia-Programmierung 1
V GUI in LWJGL 2 erstellen Spiele- und Multimedia-Programmierung 6
C GLSL Shaderprogrammierung in LWJGL 3 Spiele- und Multimedia-Programmierung 12
G Low Poly 3D LWJGL Shader Problem Spiele- und Multimedia-Programmierung 4
B LWJGL OpenGL SIGSEGV auf Linux (Verzweiflung :/) Spiele- und Multimedia-Programmierung 8
G LWJGL .obj .mtl loader Spiele- und Multimedia-Programmierung 3
G 2D animationen LWJGL Spiele- und Multimedia-Programmierung 0
pcfreak9000 "Allgemeine" Performance verbessern (LWJGL 2) Spiele- und Multimedia-Programmierung 2
G LWJGL Rendert nicht Spiele- und Multimedia-Programmierung 3
G lwjgl verwendung Spiele- und Multimedia-Programmierung 6
R [LWJGL] Skeletal Animation Spiele- und Multimedia-Programmierung 5
E LWJGL glGenVertexArrays() erzeugt doppelte IDs Spiele- und Multimedia-Programmierung 3
G Java 2D Spiel mit LWJGL verbinden Spiele- und Multimedia-Programmierung 1
Streeber Problem mit Transparenz/TextDrawing in LWJGL/Slick2d (OpenGL) Spiele- und Multimedia-Programmierung 1
K No Lwjgl Spiele- und Multimedia-Programmierung 2
T LWJGL 2.9.2: Seltsamer Effekt beim Rendern (VertexShader Problem?) Spiele- und Multimedia-Programmierung 3
T LWJGL: Terrain-Texturen / 2D-Array in Shader? Spiele- und Multimedia-Programmierung 2
S 2D-Render Probleme LWJGL 2 (Java) Spiele- und Multimedia-Programmierung 1
P java lwjgl Game Spiele- und Multimedia-Programmierung 0
T [LWJGL] Textur / File wieder freigeben Spiele- und Multimedia-Programmierung 4
F [LWJGL] Skeletal Animation 3D Spiele- und Multimedia-Programmierung 1
C Generelle Hilfe zur lwjgl Spiele- und Multimedia-Programmierung 0
D LWJGL gluLookAt "Umschauen" Problem Spiele- und Multimedia-Programmierung 0
D Problem mit Würfelanimierung in LWJGL Spiele- und Multimedia-Programmierung 7
RalleYTN LWJGL Vignette Spiele- und Multimedia-Programmierung 2
E LWJGL Switchen zwischen gluOrtho und gluPerspective Spiele- und Multimedia-Programmierung 0
RalleYTN LWJGL Rotation Spiele- und Multimedia-Programmierung 1
C LWJGL Color Picking Textures deaktivieren Spiele- und Multimedia-Programmierung 0
K FBO Framebuffer object [LWJGL] 2D tutorial gesucht Spiele- und Multimedia-Programmierung 2
K [LWJGL] 2D Tunneler Hintergrund Spiele- und Multimedia-Programmierung 7
S LWJGL 3d-spieleentwicklung Spiele- und Multimedia-Programmierung 3
H LWJGL-Renderfail Spiele- und Multimedia-Programmierung 1
Seikuassi LWJGL - Texturen flackern Spiele- und Multimedia-Programmierung 2
Androbin LWJGL - Kollisions-Bug (Fallen) Spiele- und Multimedia-Programmierung 14
K Schiessen in 2D (LWJGL) Spiele- und Multimedia-Programmierung 2
S LWJGL Kamera Problem - Alles verzerrt Spiele- und Multimedia-Programmierung 4
U Kann nur ein Objekt mit LWJGL rendern Spiele- und Multimedia-Programmierung 2
X LWJGL | Parent.isDisplayable() must be true | wie kann man das zu true machen? Spiele- und Multimedia-Programmierung 0
X [LWJGL] Binden von Texturen per PNG File und Texture Sheet Spiele- und Multimedia-Programmierung 1
X LWJGL - Anklick baren Button erstellen aber wie? Spiele- und Multimedia-Programmierung 6
U Quadrate anklicken LWJGL Spiele- und Multimedia-Programmierung 3
B LWJGL / OPENGL Kriege Depth-Test nicht hin :( Spiele- und Multimedia-Programmierung 0
B LWJGL Manche Seiten werden transparent angezeigt Spiele- und Multimedia-Programmierung 2
T LWJGL VBO's funktionieren nicht, geben aber auch keinen Fehler Spiele- und Multimedia-Programmierung 0
U Komische fragmente bei LWJGL Spiele- und Multimedia-Programmierung 6
B LWJGL StackOverFlow Problem nach 30sekunden. (Pong) Spiele- und Multimedia-Programmierung 2
Q LWJGL - Alpha-Probleme Spiele- und Multimedia-Programmierung 2
S [LWJGL] Zweimal selbe Textur trotz unterschiedlicher IDs Spiele- und Multimedia-Programmierung 3
O LWJGL AWTGLCanvas Tiefe auf 1 beschränkt Spiele- und Multimedia-Programmierung 5
Seikuassi LWJGL-Problem Spiele- und Multimedia-Programmierung 2
S [LWJGL] schwarzer Bildschrim beim rendern von .obj Model Spiele- und Multimedia-Programmierung 2
S [lwjgl] Renderbug bei mehreren Objekten Spiele- und Multimedia-Programmierung 2
R LWJGL: OpenGL Fehler - weitere Informationen auslesen möglich? Spiele- und Multimedia-Programmierung 2
S LWJGL Kamera Koordinaten invertiert. Spiele- und Multimedia-Programmierung 2
M LWJGL Text rendern Spiele- und Multimedia-Programmierung 3
B LWJGL Mauskoordinaten Spiele- und Multimedia-Programmierung 1
J LWJGL Update Schleife (Snake) Spiele- und Multimedia-Programmierung 6
B LWJGL Display.update() ist langsam Spiele- und Multimedia-Programmierung 5
R LWJGL: Performance glBegin, drawList, ... Spiele- und Multimedia-Programmierung 16
R LWJGL: Object Loader -> .obj, .c4d, ... laden Spiele- und Multimedia-Programmierung 3
R LWJGL: Textur -> unsichtbare Stellen, wie erzeugen? Spiele- und Multimedia-Programmierung 4
A LwJGL - Animation Stockt Spiele- und Multimedia-Programmierung 5
R [lwjgl] Cursor -> versetzt Zeichnen / Bild ist umgedreht Spiele- und Multimedia-Programmierung 2
R LWJGL: 3D Picking Spiele- und Multimedia-Programmierung 4
F LWJGL: Textur ändern mit GL11.readPixels Spiele- und Multimedia-Programmierung 5
F LWJGL: Licht und GL_LINES funktioniert nicht Spiele- und Multimedia-Programmierung 6
A [LWJGL] BMP Textur wird nicht richtig dargestellt Spiele- und Multimedia-Programmierung 8
S LWJGL Rechteck wird nicht gezeichnet Spiele- und Multimedia-Programmierung 6
F LWJGL: Is undefined? Spiele- und Multimedia-Programmierung 7
F LWJGL Problem mit Erstellen eines Objekts und der Kamera Spiele- und Multimedia-Programmierung 5
F LWJGL Dreidimensionaler Würfel Spiele- und Multimedia-Programmierung 15
P LWJGL oder OpenGL (C++) Spiele- und Multimedia-Programmierung 7
P "Tiefe" in Objekten - LWJGL Spiele- und Multimedia-Programmierung 12
T LWJGL 3D Objekt Collision: Wie? Spiele- und Multimedia-Programmierung 11
S LWJGL Kamera Frage Spiele- und Multimedia-Programmierung 2
V Komischer Fehler in LWJGL Spiele- und Multimedia-Programmierung 18
Z lwjgl oder jogl nutzen Spiele- und Multimedia-Programmierung 9
Y LWJGL Hintergrund Spiele- und Multimedia-Programmierung 7
Creylon [LWJGL] Textur wird falsch angezeigt Spiele- und Multimedia-Programmierung 12
Creylon [LWJGL] Spiel Exportieren Spiele- und Multimedia-Programmierung 2
Creylon [LWJGL] 2D Sprite Rotieren/Drehen Spiele- und Multimedia-Programmierung 6
CookieSoft LWJGL Ubuntu 12.04 Fehler Spiele- und Multimedia-Programmierung 7
E [LWJGL] Karusell, mehrere Objekte drehen sich um einen Mittelpunkt Spiele- und Multimedia-Programmierung 31
F lwjgl - Skysphere Spiele- und Multimedia-Programmierung 3
CookieSoft Slick und LWJGL Texture lag Spiele- und Multimedia-Programmierung 13
U OpenGl 1.1 (LWJGL GL11.*) und weiter? Spiele- und Multimedia-Programmierung 7
0 Grafikfehler LWJGL Spiele- und Multimedia-Programmierung 2
A LWJGL 3D Objekte Kollision Spiele- und Multimedia-Programmierung 3
Luk10 (LWJGL) Aufwendiges Animieren von Texturen Spiele- und Multimedia-Programmierung 16
S (LWJGL) VertexBufferObjects Spiele- und Multimedia-Programmierung 20
T LWJGL Grafik meines Projektes läuft nicht korrekt auf meinem iMac Spiele- und Multimedia-Programmierung 19
B LWJGL/OpenGL rendert manche Objekte nicht Spiele- und Multimedia-Programmierung 6
H LWJGL: Fragen zum Verständnis Spiele- und Multimedia-Programmierung 7
T LWJGL Gui erstellen Spiele- und Multimedia-Programmierung 7
Kenan89 lwjgl Exception Spiele- und Multimedia-Programmierung 3
Z Anfängerfrage: Text anzeigen mit LWJGL Spiele- und Multimedia-Programmierung 2

Ähnliche Java Themen

Neue Themen


Oben