Hey Guys,
as I try to develop a little Voxel Engine to improve my 3D Skills I think this is the right place?
I started with some Cubes and Chunks and now I try to manage those Chunks… However, atm I have problems to set up a camera. I just want to fly around my Chunks and later implement a player, but for now I have trouble with the movement of the camera.
Here is my Camera.class:
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.util.vector.Vector3f;
import static org.lwjgl.opengl.GL11.*;
public class Camera {
//3d vector to store the camera's position in
//the rotation around the Y axis of the camera
//the rotation around the X axis of the camera
public Vector3f position = null;
private float yaw = 0.0f;
private float pitch = 0.0f;
private float movementSpeed = 5.0f;
private float mouseSensitivity = 0.5f;
private float dtt;
/**
* Creates a new Camera that takes the starting x, y, z location
* Call the update() method every frame to use the camera
* Do not forget to set the movementSpeed(default 10) and mouseSensitivity (default 0.5) by calling the
* setMovementSpeed() and setMouseSensitivity() methods
*
* @param x x location
* @param y y location
* @param z z location
*/
public Camera(float x, float y, float z)
{
position = new Vector3f(x, y, z);
}
/**
* updates the camera
* CALL THIS <b>BEFORE</b> YOU RENDER YOUR SCENE!
* @param dt deltaTime for indie movement
*/
public void update(float dt) {
// ignore this :D
dtt = dtt + dt;
if(dtt >= 1) {
System.out.println(position);
dtt = 0;
}
//control camera yaw from x movement from the mouse
yaw(-Mouse.getDX() * mouseSensitivity);
//control camera pitch from y movement from the mouse
pitch(Mouse.getDY() * mouseSensitivity);
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
moveCamera(0, movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
moveCamera(180, movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
moveCamera(90, movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
moveCamera(270, movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
flyUp(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
flyDown(movementSpeed*dt);
}
glLoadIdentity();
lookThrough();
}
/**
* increment the camera's current yaw rotation
* @param amount increment the yaw by the amount param
*/
private void yaw(float amount) {
yaw += amount;
if(yaw > 359)
yaw = yaw - 359;
else if(yaw < 0)
yaw = yaw + 360;
}
/**
* increment the camera's current pitch rotation
* @param amount increment the pitch by the amount
*/
private void pitch(float amount) {
pitch += amount;
if(pitch > 90)
pitch = 90;
else if(pitch < -90)
pitch = -90;
}
private void moveCamera(float direction, float distance) {
position.x += Math.sin(Math.toRadians(yaw + direction)) * distance;
position.z += Math.cos(Math.toRadians(yaw + direction)) * distance;
}
private void flyUp(float distance) {
position.y += distance;
}
private void flyDown(float distance) {
position.y -= distance;
}
/**
* translates and rotate the matrix so that it looks through the camera
*/
private void lookThrough() {
//roatate the pitch around the X axis
glRotatef(-pitch, 1.0f, 0.0f, 0.0f);
//roatate the yaw around the Y axis
glRotatef(-yaw, 0.0f, 1.0f, 0.0f);
//translate to the position vector's location
glTranslatef(-position.x, -position.y, -position.z);
}
}
Maybe some of you can try out this class? There is something weird about the movement and I think thats because of positive and negative… Well I don’t really got this
I just wanted to write a camera based on this tutorial, but it seems not to work…
So 0 degrees should be moving forward, 180 degree backward, 270 right and 90 left.