fixing update cameras

This commit is contained in:
EvilMuffinHa 2020-05-30 11:19:02 -04:00
parent 68720e1d1f
commit 11f3160846
4 changed files with 104 additions and 24 deletions

View File

@ -21,9 +21,9 @@ public class Test implements Game {
public Renderer renderer; public Renderer renderer;
public Shader shader; public Shader shader;
public Mesh dragon = ModelLoader.loadModel("resources/models/dragon.obj", "resources/textures/b.png"); public boolean lockToggle = false;
public Mesh cube = new Mesh(new Vertex[] { public GameObject cubeObject = new GameObject(new Mesh(new Vertex[] {
//Back face //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, 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(0.0f, 1.0f)),
@ -83,19 +83,15 @@ public class Test implements Game {
//Bottom face //Bottom face
20, 21, 23, 20, 21, 23,
23, 21, 22 23, 21, 22
}, new Material(new Texture("resources/textures/b.png")), "texture"); }, new Material(new Texture("resources/textures/b.png")), "texture"), new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
public boolean lockToggle = false;
public GameObject cubeObject = new GameObject(cube, new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
public GameObject planeObject = new GameObject("resources/objects/plane.mesh", new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)); public GameObject planeObject = new GameObject("resources/objects/plane.mesh", new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
public GameObject dragonObject = new GameObject(dragon, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)); public GameObject dragonObject = new GameObject("resources/models/dragon.obj", "resources/textures/b.png", new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
// public ThirdPersonCamera camera = new ThirdPersonCamera(new Vector3f(0, 0, 5), new Vector3f(0, 0, 0), cubeObject, 0.5f, 5, 0.1f, 20f, false, true, true); public ThirdPersonCamera camera = new ThirdPersonCamera(new Vector3f(0, 0, 5), new Vector3f(0, 0, 0), cubeObject, 0.5f, 5, 0.1f, 12f, true, true, true);
public FirstPersonCamera camera = new FirstPersonCamera(new Vector3f(0, 0, 5), new Vector3f(0, 0, 0), 0.5f, 0.15f); // public FirstPersonCamera camera = new FirstPersonCamera(new Vector3f(0, 0, 5), new Vector3f(0, 0, 0), 0.1f, 0.15f);
public Test() throws Exception { public Test() throws Exception {
} }
@ -118,7 +114,7 @@ public class Test implements Game {
int frames = display.update(); int frames = display.update();
display.setWindowName(display.getWindowName().substring(0, 4) + " (Frames : " + frames + ")"); display.setWindowName(display.getWindowName().substring(0, 4) + " (Frames : " + frames + ")");
if (display.isLocked()) { if (display.isLocked()) {
camera.update(); camera.standardKeybindUpdate();
} }
if (i.buttonPress(GLFW.GLFW_MOUSE_BUTTON_LEFT)) { if (i.buttonPress(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
if (!lockToggle) { if (!lockToggle) {

View File

@ -25,7 +25,7 @@ public class FirstPersonCamera extends Camera {
this.i = i; this.i = i;
} }
public void update () throws Exception { public void standardKeybindUpdate () throws Exception {
newMouseX = i.getMouseX(); newMouseX = i.getMouseX();
newMouseY = i.getMouseY(); newMouseY = i.getMouseY();
@ -48,13 +48,49 @@ public class FirstPersonCamera extends Camera {
if (i.isKeyDown(GLFW.GLFW_KEY_S)) 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_SPACE)) movePosition(0, moveSpeed, 0);
if (i.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT)) movePosition(0, -moveSpeed, 0); if (i.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT)) movePosition(0, -moveSpeed, 0);
if (i.keyPress(GLFW.GLFW_KEY_R)) moveSpeed = 4*moveSpeed; if (i.keyPress(GLFW.GLFW_KEY_R)) moveSpeed = 3*moveSpeed;
if (i.keyReleased(GLFW.GLFW_KEY_R)) moveSpeed = moveSpeed / 4; if (i.keyReleased(GLFW.GLFW_KEY_R)) moveSpeed = moveSpeed / 3;
setRotation(cameraRot); setRotation(cameraRot);
setPosition(cameraPos); setPosition(cameraPos);
i.reset(); i.reset();
}
public void moveForward() {
movePosition(0, 0, -moveSpeed);
}
public void moveBackward() {
movePosition(0, 0, moveSpeed);
}
public void moveLeft() {
movePosition(-moveSpeed, 0, 0);
}
public void moveRight() {
movePosition(moveSpeed, 0, 0);
}
public void moveUp() {
movePosition(0, moveSpeed, 0);
}
public void moveDown() {
movePosition(0, -moveSpeed, 0);
}
public float getMoveSpeed() {
return moveSpeed;
}
public void setMoveSpeed(float moveSpeed) {
this.moveSpeed = moveSpeed;
}
public void rotateCamera(float dx, float dy) throws Exception {
Vector3f cameraRot = getRotation();
cameraRot = Vector3f.add(cameraRot, new Vector3f(dy*sensitivity, dx*sensitivity, 0));
setRotation(cameraRot);
} }
} }

View File

@ -1,9 +1,6 @@
package org.hl.engine.objects; package org.hl.engine.objects;
import org.hl.engine.graphics.Material; import org.hl.engine.graphics.*;
import org.hl.engine.graphics.Mesh;
import org.hl.engine.graphics.Texture;
import org.hl.engine.graphics.Vertex;
import org.hl.engine.math.lalg.Vector2f; import org.hl.engine.math.lalg.Vector2f;
import org.hl.engine.math.lalg.Vector3f; import org.hl.engine.math.lalg.Vector3f;
import org.hl.engine.objects.yloaders.YMesh; import org.hl.engine.objects.yloaders.YMesh;
@ -32,10 +29,10 @@ public class GameObject {
Yaml yaml = new Yaml(); Yaml yaml = new Yaml();
FileInputStream inputStream = new FileInputStream(meshFileName); FileInputStream inputStream = new FileInputStream(meshFileName);
YMesh yMesh = yaml.loadAs(inputStream, YMesh.class); YMesh yMesh = yaml.loadAs(inputStream, YMesh.class);
Integer[] cull = yMesh.getCull().toArray(new Integer[yMesh.getCull().size()]); Integer[] cull = yMesh.getCull().toArray(new Integer[0]);
String type = yMesh.getType(); String type = yMesh.getType();
String texture = yMesh.getTexture(); String texture = yMesh.getTexture();
YPoint[] vertices = yMesh.getVertices().toArray(new YPoint[yMesh.getVertices().size()]); YPoint[] vertices = yMesh.getVertices().toArray(new YPoint[0]);
Vertex[] meshFormat = new Vertex[vertices.length]; Vertex[] meshFormat = new Vertex[vertices.length];
for (YPoint vertex : vertices) { for (YPoint vertex : vertices) {
@ -69,6 +66,12 @@ public class GameObject {
this.rotation = rotation; this.rotation = rotation;
} }
public GameObject(String meshFileName, String texturePath, Vector3f position, Vector3f rotation, Vector3f scale) throws Exception {
this.mesh = ModelLoader.loadModel(meshFileName, texturePath);
this.position = position;
this.scale = scale;
this.rotation = rotation;
}
public void create() { public void create() {

View File

@ -43,9 +43,7 @@ public class ThirdPersonCamera extends Camera {
} }
public void update() throws Exception { public void standardKeybindUpdate() throws Exception {
near = 0.1f;
newMouseX = i.getMouseX(); newMouseX = i.getMouseX();
newMouseY = i.getMouseY(); newMouseY = i.getMouseY();
@ -63,9 +61,13 @@ public class ThirdPersonCamera extends Camera {
if (i.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_RIGHT) && zoomEnabled) { if (i.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_RIGHT) && zoomEnabled) {
if (distance > 0) { if (distance > 0) {
distance += dy * sensitivity; distance += dy * sensitivity;
} else { }
else {
distance = near; distance = near;
} }
if (distance > far) {
distance = far;
}
} }
} else if (clickToMove) { } else if (clickToMove) {
@ -79,6 +81,9 @@ public class ThirdPersonCamera extends Camera {
} else { } else {
distance = near; distance = near;
} }
if (distance > far) {
distance = far;
}
} }
} else if (clickToZoom) { } else if (clickToZoom) {
@ -92,6 +97,9 @@ public class ThirdPersonCamera extends Camera {
} else { } else {
distance = near; distance = near;
} }
if (distance > far) {
distance = far;
}
} }
} else { } else {
@ -104,6 +112,9 @@ public class ThirdPersonCamera extends Camera {
} else { } else {
distance = near; distance = near;
} }
if (distance > far) {
distance = far;
}
} }
} }
@ -123,6 +134,40 @@ public class ThirdPersonCamera extends Camera {
i.reset(); i.reset();
}
public void movePosition (float dy, float dx) throws Exception {
vertAngle -= dy * sensitivity;
horizAngle += dx * sensitivity;
float horizDistance = (float) (distance * Math.cos(Math.toRadians(vertAngle)));
float vertDistance = (float) (distance * Math.sin(Math.toRadians(vertAngle)));
float xOffset = (float) (horizDistance * Math.sin(Math.toRadians(-horizAngle)));
float zOffset = (float) (horizDistance * Math.cos(Math.toRadians(-horizAngle)));
setPosition(new Vector3f(object.getPosition().getX() + xOffset, object.getPosition().getY() - vertDistance, object.getPosition().getZ() + zOffset));
setRotation(new Vector3f(-vertAngle, horizAngle,0));
}
public void zoom (float dy) throws Exception {
if (zoomEnabled) {
if (distance > 0) {
distance += dy * sensitivity;
} else {
distance = near;
}
if (distance > far) {
distance = far;
}
}
float horizDistance = (float) (distance * Math.cos(Math.toRadians(vertAngle)));
float vertDistance = (float) (distance * Math.sin(Math.toRadians(vertAngle)));
float xOffset = (float) (horizDistance * Math.sin(Math.toRadians(-horizAngle)));
float zOffset = (float) (horizDistance * Math.cos(Math.toRadians(-horizAngle)));
setPosition(new Vector3f(object.getPosition().getX() + xOffset, object.getPosition().getY() - vertDistance, object.getPosition().getZ() + zOffset));
setRotation(new Vector3f(-vertAngle, horizAngle,0));
} }
} }