简体   繁体   中英

LWJGL 3D Movement

I am trying to create a FPS camera using LWJGL, but my code does not seem to work. -_- It just creates a white block in front of the screen, player can not move or rotate. What is the problem?

public class LocalWindow 
{
public void Launch() 
{
    try 
    { 
        Display.setDisplayMode (new DisplayMode (800, 600));
        Display.setTitle("Historica");
        Display.create();
    } 
    catch (LWJGLException e) 
    {
            e.printStackTrace();
    }
}
public void Render()
{
    initMatrix();
    while (!Display.isCloseRequested())
            //&& !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
    {

        initControls();
        frameRefresh();
        initCamera();




        Block s = new Block(0, 0, -4);
        s.setBlock();






        Display.update();
    }
}
private void initCamera()
{
    Camera player = new Camera();
    player.setView();
}
private void frameRefresh()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
}
private void initControls()
{
    Mouse.setGrabbed(true);
    Controls controls = new Controls();
    controls.moveForward(0.1f);
    controls.moveBackward(0.1f);
    controls.strafeLeft(0.1f);
    controls.strafeRight(0.1f);
    controls.lookingAround(0.1f);
}
private void initMatrix()
{
    Matrix matrix = new Matrix();
    matrix.initMatrix();
}

}

The Camera class:

public class Camera {
public float x;
public float y;
public float z;
public float rx;
public float ry;
public float rz;
public float fov = 70;
public float aspect = (float)Display.getWidth()/(float)Display.getHeight();
public float near = 0.3f;
public float far = 1000;
public float dx;
public float dy;



public void setView(){
    glRotatef(rx,1,0,0);
    glRotatef(ry,0,1,0);
    glRotatef(rz,0,0,1);
    glTranslatef(x,y,z);
}
}

The Controls class:

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


public class Controls extends Camera {
boolean W = Keyboard.isKeyDown(Keyboard.KEY_W);
boolean S = Keyboard.isKeyDown(Keyboard.KEY_S);
boolean A = Keyboard.isKeyDown(Keyboard.KEY_A);
boolean D = Keyboard.isKeyDown(Keyboard.KEY_D);

public void moveForward(float amount)
{
    if(W)
    {
    x += amount * Math.cos(Math.toRadians(ry + 90));
    z += amount * Math.sin(Math.toRadians(ry + 90));
    }
}

public void moveBackward(float amount)
{
    if(S)
    {
    x -= amount * Math.cos(Math.toRadians(ry + 90));
    z -= amount * Math.sin(Math.toRadians(ry + 90));
    }
}
public void strafeLeft(float amount)
{
    if(A);
    {
    x -= amount * (float)Math.sin(Math.toRadians(ry-90));
    z += amount * (float)Math.cos(Math.toRadians(ry-90));
    }
}
public void strafeRight(float amount)
{
    if(D)
    {  
    x -= amount * (float)Math.sin(Math.toRadians(ry+90));
    z += amount * (float)Math.cos(Math.toRadians(ry+90));
    }
}
public void lookingAround(float amount)
{
    dx = Mouse.getDX();
    dy = Mouse.getDY();
    ry += dx * amount;
    rx -= dy * amount;
}
}

The Matrix class:

public class Matrix extends Camera{
public void initMatrix(){
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fov, aspect, near, far);
    glMatrixMode(GL_MODELVIEW); 
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
}

}

Class Block works entirely properly (It creates a 3D block on the given coordinates).

The reason why your camera doesn't move is that you create a new instance of player and controls in every frame. In addition to this, you change the members only in controls, but use the values in player to set the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM