camera movement with cube - some weird bug thing

This commit is contained in:
EvilMuffinHa 2020-05-28 15:01:47 -04:00
parent 9a6b5ec048
commit 4b51802309
7 changed files with 240 additions and 29 deletions

View File

@ -3,4 +3,9 @@ abstract public class Game {
abstract public void loop() throws Exception;
abstract public void close() throws Exception;
public void run() throws Exception {
setup();
loop();
}
}

View File

@ -3,7 +3,7 @@ import org.hl.engine.io.Display;
import org.hl.engine.io.Input;
import org.hl.engine.math.lalg.Vector3f;
import org.hl.engine.math.lalg.Vector2f;
import org.hl.engine.objects.Camera;
import org.hl.engine.objects.FirstPersonCamera;
import org.hl.engine.objects.GameObject;
import org.lwjgl.glfw.GLFW;
@ -18,26 +18,90 @@ public class Test extends Game {
public Renderer renderer;
public Shader shader;
public Mesh mesh = new Mesh(new Vertex[] {
new Vertex(new Vector3f(-0.5F, 0.5F, 0.0F), new Vector3f(0, 0, 1.0F), new Vector2f(0, 0)),
new Vertex(new Vector3f(-0.5F, -0.5F, 0.0F), new Vector3f(0, 0, 1.0F), new Vector2f(0, 1)),
new Vertex(new Vector3f(0.5F, -0.5F, 0.0F), new Vector3f(1.0F, 0, 1.0F), new Vector2f(1, 1)),
new Vertex(new Vector3f(0.5F, 0.5F, 0.0F), new Vector3f(1.0F, 0, 1.0F), new Vector2f(1, 0)),
public Mesh plane = new Mesh(new Vertex[] {
new Vertex(new Vector3f(-20, 20, 0), new Vector2f(0, 0)),
new Vertex(new Vector3f(-20, -20, 0), new Vector2f(0, 1)),
new Vertex(new Vector3f(20, -20, 0), new Vector2f(1, 1)),
new Vertex(new Vector3f(20, 20, 0), new Vector2f(1, 0)),
}, new int[] {
0, 1, 2,
0, 2, 3
0, 1, 3,
3, 1, 2
}, new Material(new Texture("resources/textures/plane.png")));
public Mesh mesh = new Mesh(new Vertex[] {
//Back face
new Vertex(new Vector3f(-0.5f, 0.5f, -0.5f), new Vector2f(0.0f, 0.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, -0.5f), new Vector2f(0.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, -0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, -0.5f), new Vector2f(1.0f, 0.0f)),
//Front face
new Vertex(new Vector3f(-0.5f, 0.5f, 0.5f), new Vector2f(0.0f, 0.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, 0.5f), new Vector2f(0.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
//Right face
new Vertex(new Vector3f( 0.5f, 0.5f, -0.5f), new Vector2f(0.0f, 0.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, -0.5f), new Vector2f(0.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
//Left face
new Vertex(new Vector3f(-0.5f, 0.5f, -0.5f), new Vector2f(0.0f, 0.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, -0.5f), new Vector2f(0.0f, 1.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, 0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f(-0.5f, 0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
//Top face
new Vertex(new Vector3f(-0.5f, 0.5f, 0.5f), new Vector2f(0.0f, 0.0f)),
new Vertex(new Vector3f(-0.5f, 0.5f, -0.5f), new Vector2f(0.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, -0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
//Bottom face
new Vertex(new Vector3f(-0.5f, -0.5f, 0.5f), new Vector2f(0.0f, 0.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, -0.5f), new Vector2f(0.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, -0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
}, new int[] {
//Back face
0, 1, 3,
3, 1, 2,
//Front face
4, 5, 7,
7, 5, 6,
//Right face
8, 9, 11,
11, 9, 10,
//Left face
12, 13, 15,
15, 13, 14,
//Top face
16, 17, 19,
19, 17, 18,
//Bottom face
20, 21, 23,
23, 21, 22
}, new Material(new Texture("resources/textures/b.png")));
public boolean lockToggle = false;
public GameObject testObject = new GameObject(mesh, new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
public Camera camera = new Camera(new Vector3f(0, 0, 1), new Vector3f(0, 0, 0));
public GameObject testingPlane = new GameObject(plane, new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
public FirstPersonCamera camera = new FirstPersonCamera(new Vector3f(0, 0, 1), new Vector3f(0, 0, 0), 0.05f, 0.15f);
public void run() throws Exception {
setup();
i = new Input(display);
while (!(display.shouldClose()) && !i.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) {
while (!(display.shouldClose())) {
loop();
}
@ -51,18 +115,16 @@ public class Test extends Game {
//First updating
int frames = display.update();
display.setWindowName(display.getWindowName().substring(0, 4) + " (Frames : " + frames + ")");
float ms = 0.05f;
Vector3f cameraPos = camera.getPosition();
Vector3f cameraRot = camera.getRotation();
if (i.isKeyDown(GLFW.GLFW_KEY_A)) cameraPos = Vector3f.add(cameraPos, new Vector3f(-ms, 0, 0));
if (i.isKeyDown(GLFW.GLFW_KEY_D)) cameraPos = Vector3f.add(cameraPos, new Vector3f(ms, 0, 0));
if (i.isKeyDown(GLFW.GLFW_KEY_W)) cameraPos = Vector3f.add(cameraPos, new Vector3f(0, 0, -ms));
if (i.isKeyDown(GLFW.GLFW_KEY_S)) cameraPos = Vector3f.add(cameraPos, new Vector3f(0, 0, ms));
if (i.isKeyDown(GLFW.GLFW_KEY_SPACE)) cameraPos = Vector3f.add(cameraPos, new Vector3f(0, ms, 0));
if (i.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT)) cameraPos = Vector3f.add(cameraPos, new Vector3f(0, -ms, 0));
camera.setPosition(cameraPos);
camera.setRotation(cameraRot);
if (display.isLocked()) {
camera.update();
}
if (i.buttonPress(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
lockToggle = !lockToggle;
display.mouseState(lockToggle);
} else if (i.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) {
lockToggle = false;
display.mouseState(lockToggle);
}
i.reset();
@ -94,10 +156,18 @@ public class Test extends Game {
// Creating / displaying the mesh
mesh.create();
plane.create();
// Creating the shader
shader.create();
// Creating the input
i = new Input(display);
// Creating the camera
camera.create(i);
}
public void close() {

View File

@ -19,6 +19,12 @@ public class Vertex {
this.textureCoords = textureCoords;
}
public Vertex(Vector3f position, Vector2f textureCoords) {
this.position = position;
this.color = new Vector3f(1.0F, 1.0F, 1.0F);
this.textureCoords = textureCoords;
}
public Vector3f getPosition() {
return position;
}

View File

@ -31,6 +31,11 @@ public class Display {
private int savedWidth;
private int savedHeight;
private Matrix4f projection;
private boolean isLocked;
private double[] cursorX = new double[1];
private double[] cursorY = new double[1];
private double[] enabledCursorX = new double[1];
private double[] enabledCursorY = new double[1];
@ -95,6 +100,26 @@ public class Display {
}
}
public void mouseState(boolean lock) {
if (lock != isLocked) {
if (lock) {
glfwGetCursorPos(window, enabledCursorX, enabledCursorY);
glfwSetCursorPos(window, cursorX[0], cursorY[0]);
isLocked = lock;
glfwSetInputMode(window, GLFW_CURSOR, lock ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
} else {
glfwGetCursorPos(window, cursorX, cursorY);
isLocked = lock;
glfwSetInputMode(window, GLFW_CURSOR, lock ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
glfwSetCursorPos(window, enabledCursorX[0], enabledCursorY[0]);
}
}
}
public boolean isLocked() {
return isLocked;
}
// resized getter
public boolean isResized() {
@ -135,6 +160,8 @@ public class Display {
windowYPos[0] = (videoMode.height() - this.height ) / 2;
glfwSetWindowPos(window, windowXPos[0], windowYPos[0]);
glfwSetCursorPos(window, 0, 0);
// Graphics
glfwMakeContextCurrent(window);

View File

@ -47,7 +47,20 @@ public class Matrix4f {
return result;
}
public static Matrix4f rotate(float angle, Vector3f axis) {
public static Matrix4f flip(Matrix4f matrix) {
Matrix4f flippedMatrix = matrix;
for (int i = 0; i < SIZE; i ++) {
for (int j = 0; j < SIZE; j ++ ) {
flippedMatrix.set(i, j, matrix.get(j, i));
}
}
return flippedMatrix;
}
// This one doesn't work
/* public static Matrix4f rotate(float angle, Vector3f axis) {
float cos = (float)Math.cos(Math.toDegrees(angle));
float sin = (float)Math.sin(Math.toDegrees(angle));
@ -61,7 +74,27 @@ public class Matrix4f {
{z*x*C-y*sin , z*y*C + x*sin , cos + z*z*C , 0},
{0 , 0 , 0 , 1}
};
return new Matrix4f(rotArray);
} */
public static Matrix4f rotate(float angle, Vector3f axis) {
Matrix4f result = Matrix4f.identity();
float cos = (float) Math.cos(Math.toRadians(angle));
float sin = (float) Math.sin(Math.toRadians(angle));
float C = 1 - cos;
result.set(0, 0, cos + axis.getX() * axis.getX() * C);
result.set(0, 1, axis.getX() * axis.getY() * C - axis.getZ() * sin);
result.set(0, 2, axis.getX() * axis.getZ() * C + axis.getY() * sin);
result.set(1, 0, axis.getY() * axis.getX() * C + axis.getZ() * sin);
result.set(1, 1, cos + axis.getY() * axis.getY() * C);
result.set(1, 2, axis.getY() * axis.getZ() * C - axis.getX() * sin);
result.set(2, 0, axis.getZ() * axis.getX() * C - axis.getY() * sin);
result.set(2, 1, axis.getZ() * axis.getY() * C + axis.getX() * sin);
result.set(2, 2, cos + axis.getZ() * axis.getZ() * C);
return result;
}
public static Matrix4f scale(Vector3f scaleVec) {
@ -95,13 +128,13 @@ public class Matrix4f {
Vector3f negative = new Vector3f(-position.getX(), -position.getY(), -position.getZ());
Matrix4f translationMatrix = Matrix4f.translate(negative);
Matrix4f rotationXMatrix = Matrix4f.rotate(rotation.getX(), new Vector3f(1, 0, 0));
Matrix4f rotationYMatrix = Matrix4f.rotate(rotation.getY(), new Vector3f(0, 1, 0));
Matrix4f rotationZMatrix = Matrix4f.rotate(rotation.getZ(), new Vector3f(0, 0, 1));
Matrix4f rotationXMatrix = Matrix4f.rotate(-rotation.getX(), new Vector3f(1, 0, 0));
Matrix4f rotationYMatrix = Matrix4f.rotate(-rotation.getY(), new Vector3f(0, 1, 0));
Matrix4f rotationZMatrix = Matrix4f.rotate(-rotation.getZ(), new Vector3f(0, 0, 1));
Matrix4f rotMat = Matrix4f.multiply(rotationZMatrix, Matrix4f.multiply(rotationYMatrix, rotationXMatrix));
return Matrix4f.multiply(translationMatrix, rotMat);
return Matrix4f.multiply(rotMat, translationMatrix);
}

View File

@ -27,4 +27,16 @@ public class Camera {
public void setRotation(Vector3f rotation) {
this.rotation = rotation;
}
public void movePosition(float offsetX, float offsetY, float offsetZ) {
if(offsetZ != 0) {
position.setZ(position.getZ() + (float) Math.cos(Math.toRadians(rotation.getY())) * offsetZ);
position.setX(position.getX() + (float) Math.sin(Math.toRadians(rotation.getY())) * -offsetZ);
}
if(offsetX != 0) {
position.setX(position.getX() + (float) Math.cos(Math.toRadians(rotation.getY())) * offsetX) ;
position.setZ(position.getZ() + (float) Math.sin(Math.toRadians(rotation.getY())) * offsetX);
}
position.setY(position.getY() + offsetY);
}
}

View File

@ -0,0 +1,58 @@
package org.hl.engine.objects;
import org.hl.engine.io.Display;
import org.hl.engine.io.Input;
import org.hl.engine.math.lalg.Vector3f;
import org.lwjgl.glfw.GLFW;
import java.util.Vector;
public class FirstPersonCamera extends Camera {
private Input i;
private float moveSpeed;
private float sensitivity;
private double oldMouseX, oldMouseY = 0;
private double newMouseX, newMouseY;
public FirstPersonCamera(Vector3f position, Vector3f rotation, float moveSpeed, float sensitivity) {
super(position, rotation);
this.moveSpeed = moveSpeed;
this.sensitivity = sensitivity;
}
public void create(Input i) {
this.i = i;
}
public void update () {
newMouseX = i.getMouseX();
newMouseY = i.getMouseY();
Vector3f cameraPos = getPosition();
Vector3f cameraRot = getRotation();
float dx = (float) ((float)newMouseX - oldMouseX);
float dy = (float) ((float)newMouseY - oldMouseY);
oldMouseX = newMouseX;
oldMouseY = newMouseY;
cameraRot = Vector3f.add(cameraRot, new Vector3f(dy*sensitivity, dx*sensitivity, 0));
if (i.isKeyDown(GLFW.GLFW_KEY_A)) movePosition(-moveSpeed, 0, 0);
if (i.isKeyDown(GLFW.GLFW_KEY_D)) movePosition(moveSpeed, 0, 0);
if (i.isKeyDown(GLFW.GLFW_KEY_W)) movePosition(0, 0, -moveSpeed);
if (i.isKeyDown(GLFW.GLFW_KEY_S)) movePosition(0, 0, moveSpeed);
if (i.isKeyDown(GLFW.GLFW_KEY_SPACE)) movePosition(0, moveSpeed, 0);
if (i.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT)) movePosition(0, -moveSpeed, 0);
setRotation(cameraRot);
setPosition(cameraPos);
i.reset();
}
}