camera movement with cube - some weird bug thing

This commit is contained in:
2020-05-28 15:01:47 -04:00
parent 9a6b5ec048
commit 4b51802309
7 changed files with 240 additions and 29 deletions
+6
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;
}
+27
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);
+38 -5
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);
}
+12
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);
}
}
@@ -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();
}
}