LWJGL
June 20, 2013, 04:52:56 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL is now using GitHub
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: Mouse rotation  (Read 1428 times)
Daslee
Regular nerd
**
Posts: 80



« on: May 13, 2012, 04:28:28 »

Hello. Im new at LWJGL and now im trying to do something with 3D. Now i have cube object (without roof and ground Cheesy) 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:
Code:
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();
}
}

Code:
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?
Logged

Sorry for my bad English. :/
matheus23
Regular nerd
**
Posts: 62


« Reply #1 on: May 13, 2012, 06:59:13 »

Quote
EDIT: 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.
Logged

My github account and currently active project: https://github.com/matheus23/UniverseEngine
Daslee
Regular nerd
**
Posts: 80



« Reply #2 on: May 14, 2012, 07:35:41 »

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 :/
Logged

Sorry for my bad English. :/
Daslee
Regular nerd
**
Posts: 80



« Reply #3 on: May 17, 2012, 01:59:07 »

No more suggestions?  Huh
Logged

Sorry for my bad English. :/
kappa
Administrator
Nerdus Imperius
*****
Posts: 1117



« Reply #4 on: May 17, 2012, 02: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.
Logged
Daslee
Regular nerd
**
Posts: 80



« Reply #5 on: May 17, 2012, 13:03:03 »

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

Sorry for my bad English. :/
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!