Mouse rotation

Started by Daslee, May 13, 2012, 11:28:28

Previous topic - Next topic

Daslee

Hello. Im new at LWJGL and now im trying to do something with 3D. Now i have cube object (without roof and ground :D) in my 3D world and i want to make rotation with mouse like in Counter Strike, but i worked around 2 hours and nothing happend. I was found this on google: http://www.lloydgoodall.com/tutorials/first-person-camera-control-with-lwjgl/ But even if i do not move my mouse it rotates all world really really fast, so hard to see my objects...

Here is my codes:
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.*;
import org.lwjgl.*;

public class ThreeDeeLWJGL{
	
	public ThreeDeeLWJGL(){
		FPCameraController camera = new FPCameraController(0, 0, 0);
		float dx = 0.0f;
	    float dy = 0.0f;
	    float dt = 0.0f;
	    float lastTime = 0.0f;
	    float time = 0.0f;
	    float mouseSensitivity = 0.05f;
	    float movementSpeed = 10.0f;
	    
		try{
			Display.setDisplayMode(new DisplayMode(1024, 768));
			Display.setTitle("Three Dee Demo");
			Display.create();
		}catch(LWJGLException e){
			e.printStackTrace();
		}
		glViewport(0, 0, Display.getWidth(), Display.getHeight());
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(45.0f, Display.getWidth() / Display.getHeight(), 1.0f, 1000.0f);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
		glClearDepth(1.0f);
		glDepthFunc(GL_LEQUAL);
		glEnable(GL_DEPTH_TEST);
		glShadeModel(GL_SMOOTH);
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
		
		while(!Display.isCloseRequested()){
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			
			time = Sys.getTime();
	        dt = (time - lastTime) / 1000.0f;
	        lastTime = time;
			
	        dx = Mouse.getDX();
	        dy = Mouse.getDY();
	        camera.yaw(dx * mouseSensitivity);
	        camera.pitch(dy * mouseSensitivity);
	        
			glBegin(GL_QUADS);
				//Front
				glColor3f(1.0f, 1.0f, 0.0f);
				glVertex3f(-15f, -15f, -100f);
				glVertex3f(15f, -15f, -100f);
				glVertex3f(15f, 15f, -100f);
				glVertex3f(-15f, 15f, -100f);
				//Left side
				glColor3f(0.0f, 1.0f, 1.0f);
				glVertex3f(-15f, -15f, -100f);
				glVertex3f(-15f, -15f, -150f);
				glVertex3f(-15f, 15f, -150f);
				glVertex3f(-15f, 15f, -100f);
				//Right side
				glColor3f(0.0f, 1.0f, 1.0f);
				glVertex3f(15f, -15f, -100f);
				glVertex3f(15f, -15f, -150f);
				glVertex3f(15f, 15f, -150f);
				glVertex3f(15f, 15f, -100f);
				//Back
				glColor3f(0.0f, 0.0f, 1.0f);
				glVertex3f(-15f, -15f, -150f);
				glVertex3f(15f, -15f, -150f);
				glVertex3f(15f, 15f, -150f);
				glVertex3f(-15f, 15f, -150f);
			glEnd();
			
			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);
	        }
	        
                glLoadIdentity();
                camera.lookThrough();

			Display.update();
			Display.sync(60);
		}
		Display.destroy();
		System.exit(0);
	}
	
	public static void main(String[] args) {
		new ThreeDeeLWJGL();
	}
}


package ThreeDee;

import org.lwjgl.util.vector.Vector3f;
import static org.lwjgl.opengl.GL11.*;

public class FPCameraController {
	private Vector3f position = null;
	private float yaw = 0.0f;
	private float pitch = 0.0f;
	
	public FPCameraController(float x, float y, float z){
		position = new Vector3f(x, y, z);
	}
	
	public void yaw(float amount){
		yaw += amount;
	}
	
	public void pitch(float amount){
		pitch += 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));
	}
	
	public void lookThrough(){
		glRotatef(pitch, 1.0f, 0.0f, 0.0f);
        glRotatef(yaw, 0.0f, 1.0f, 0.0f);
        glTranslatef(position.x, position.y, position.z);
	}
}


EDIT: I fixed it, but now, how i can make that mouse always stay in center?

matheus23

QuoteEDIT: I fixed it, but now, how i can make that mouse always stay in center?

Take a look at Mouse.setCursorPosition(x, y)

Call that AFTER calculating everything.
My github account and currently active project: https://github.com/matheus23/UniverseEngine

Daslee

It doesn't matter if i move mouse, or if i set mouse location with coding it anyway rotates all world. So this can't help i think :/

Daslee

No more suggestions?  ???

kappa

Grab the mouse with Mouse.setGrabbed(true) to stop it leaving the screen, second use Mouse.getDX() and Mouse.getDY() to get the amount the mouse has moved to update the camera.

Daslee

Quote from: kappa on May 17, 2012, 09:05:28
Grab the mouse with Mouse.setGrabbed(true) to stop it leaving the screen, second use Mouse.getDX() and Mouse.getDY() to get the amount the mouse has moved to update the camera.

Thanks. Mouse.setGrabbed this thing i needed! :)