LWJGL 3D运动

时间:2014-12-02 19:05:33

标签: java opengl 3d lwjgl

我正在尝试使用LWJGL创建一个FPS相机,但我的代码似乎不起作用。 -_- 它只是在屏幕前面创建一个白色块,玩家无法移动或旋转。 有什么问题?

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();
}

}

Camera类:

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);
}
}

Controls类:

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;
}
}

Matrix类:

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完全正常工作(它在给定坐标上创建一个3D块)。

1 个答案:

答案 0 :(得分:0)

您的相机不移动的原因是您在每个帧中创建了playercontrols的新实例。除此之外,您只更改控件中的成员,但使用播放器中的值来设置视图。