From 4af24fbd63b65ab947d3f81a6fdb25dc159ae30f Mon Sep 17 00:00:00 2001 From: MaikelChan Date: Mon, 11 Sep 2023 03:57:06 +0200 Subject: [PATCH] Camera movement improvements with proper up and down movement. --- BanjoKazooieLevelEditor/Form1.cs | 36 ++-- BanjoKazooieLevelEditor/GLCamera.cs | 271 +++++++++++++------------ BanjoKazooieLevelEditor/ModelViewer.cs | 30 +-- 3 files changed, 174 insertions(+), 163 deletions(-) diff --git a/BanjoKazooieLevelEditor/Form1.cs b/BanjoKazooieLevelEditor/Form1.cs index f290fb4..66e6c17 100644 --- a/BanjoKazooieLevelEditor/Form1.cs +++ b/BanjoKazooieLevelEditor/Form1.cs @@ -69,6 +69,8 @@ public class Form1 : Form private bool zoomOut; private bool left; private bool right; + private bool moveUp; + private bool moveDown; private GLCamera BBCamera = new GLCamera(); private int newMouseX; private int newMouseY; @@ -756,9 +758,9 @@ private void locateEORFiles() private void camera() { bool flag = false; - if (this.zoomIn | this.zoomOut | this.left | this.right) + if (zoomIn | zoomOut | left | right | moveUp | moveDown) { - this.BBCamera.PanUpdate(this.zoomIn, this.zoomOut, this.left, this.right); + BBCamera.PanUpdate(zoomIn, zoomOut, left, right, moveUp, moveDown); flag = true; } Point mousePosition1 = Control.MousePosition; @@ -2295,6 +2297,8 @@ private void openSetupFile() this.BBCamera.Reset(); this.zoomIn = false; this.zoomOut = false; + moveUp = false; + moveDown = false; this.left = false; this.right = false; this.newMouseX = 0; @@ -3141,25 +3145,25 @@ private void LevelViewer_KeyDown(object sender, KeyEventArgs e) { case Keys.ShiftKey: case Keys.Shift: - this.multiselect = true; + multiselect = true; break; case Keys.A: - this.right = true; + right = true; break; case Keys.D: - this.left = true; + left = true; break; case Keys.E: - this.zoomOut = true; + moveUp = true; break; case Keys.Q: - this.zoomIn = true; + moveDown = true; break; case Keys.S: - this.zoomOut = true; + zoomOut = true; break; case Keys.W: - this.zoomIn = true; + zoomIn = true; break; } } @@ -3170,25 +3174,25 @@ private void LevelViewer_KeyUp(object sender, KeyEventArgs e) { case Keys.ShiftKey: case Keys.Shift: - this.multiselect = false; + multiselect = false; break; case Keys.A: - this.right = false; + right = false; break; case Keys.D: - this.left = false; + left = false; break; case Keys.E: - this.zoomOut = false; + moveUp = false; break; case Keys.Q: - this.zoomIn = false; + moveDown = false; break; case Keys.S: - this.zoomOut = false; + zoomOut = false; break; case Keys.W: - this.zoomIn = false; + zoomIn = false; break; } } diff --git a/BanjoKazooieLevelEditor/GLCamera.cs b/BanjoKazooieLevelEditor/GLCamera.cs index 3293f93..ea63b60 100644 --- a/BanjoKazooieLevelEditor/GLCamera.cs +++ b/BanjoKazooieLevelEditor/GLCamera.cs @@ -4,140 +4,145 @@ // MVID: 9E4E8A9F-6E2F-4B24-B56C-5C2BF24BF813 // Assembly location: F:\BanjosBackpack\BB.exe -using System; - namespace BanjoKazooieLevelEditor { - public class GLCamera - { - private Vector3 position = new Vector3(0.0f, 0.0f, -2161f); - private Vector3 rotation = new Vector3(0.0f, 0.0f, 0.0f); - private Vector3 up = new Vector3(0.0f, 1f, 0.0f); - private Matrix4 ryMatrix = Matrix4.I; - private Matrix4 rxMatrix = Matrix4.I; - private float movementSpeed = 10f; - - public float MovementSpeed - { - set => this.movementSpeed = value; - } - - public float X - { - set => this.position.x = value; - get => this.position.x; - } - - public float Y - { - set => this.position.y = value; - get => this.position.y; - } - - public float Z + public class GLCamera { - set => this.position.z = value; - get => this.position.z; + private Vector3 position = new Vector3(0.0f, 0.0f, -2161f); + private Vector3 rotation = new Vector3(0.0f, 0.0f, 0.0f); + private Matrix4 ryMatrix = Matrix4.I; + private Matrix4 rxMatrix = Matrix4.I; + private float movementSpeed = 10f; + + public float MovementSpeed + { + set => movementSpeed = value; + } + + public float X + { + set => position.x = value; + get => position.x; + } + + public float Y + { + set => position.y = value; + get => position.y; + } + + public float Z + { + set => position.z = value; + get => position.z; + } + + public float Yaw + { + set + { + rotation.y = value; + ryMatrix = Matrix4.GetRotationMatrixY(Core.ToRadians(rotation.y)); + } + get => rotation.y; + } + + public float Pitch + { + set + { + rotation.x = value; + if (rotation.x < -180.0f) + rotation.x = 360f + rotation.x; + if (rotation.x > 180.0f) + rotation.x = 360f - rotation.x; + rxMatrix = Matrix4.GetRotationMatrixX(Core.ToRadians(rotation.x)); + } + get => rotation.x; + } + + public float Roll + { + set => rotation.z = value; + get => rotation.z; + } + + public float GetXRotation() => rotation.x; + + public float GetYRotation() => rotation.y; + + public float GetZRotation() => rotation.z; + + public void Reset() + { + position = new Vector3(0.0f, 0.0f, -2161f); + rotation = new Vector3(0.0f, 0.0f, 0.0f); + } + + public void LookAt(Vector3 target) + { + rotation.x = 0.0f; + rotation.y = 0.0f; + rotation.z = 0.0f; + position.x = target.x * -1f; + position.y = target.y * -1.0f - 50.0f; + position.z = target.z * -1.0f - 500.0f; + ryMatrix = Matrix4.GetRotationMatrixY(Core.ToRadians(rotation.y)); + rxMatrix = Matrix4.GetRotationMatrixX(Core.ToRadians(rotation.x)); + } + + public void PanUpdate(bool forward, bool back, bool left, bool right, bool moveUp, bool moveDown) + { + Matrix4 m = Matrix4.GetRotationMatrix(Core.ToRadians(rotation.x), Core.ToRadians(rotation.y), Core.ToRadians(rotation.z)); + + Vector3 vector3 = new Vector3(); + if (forward) + { + vector3 += new Vector3(m.matrix[2, 0], m.matrix[2, 1], m.matrix[2, 2]); + } + + if (back) + { + vector3 += new Vector3(-m.matrix[2, 0], -m.matrix[2, 1], -m.matrix[2, 2]); + } + + if (left) + { + vector3 += new Vector3(-m.matrix[0, 0], -m.matrix[0, 1], -m.matrix[0, 2]); + } + + if (right) + { + vector3 += new Vector3(m.matrix[0, 0], m.matrix[0, 1], m.matrix[0, 2]); + } + + if (moveUp) + { + vector3 += new Vector3(0, -1, 0); + } + + if (moveDown) + { + vector3 += new Vector3(0, 1, 0); + } + + if (vector3 == Vector3.Zero) return; + + position += vector3.Normalize() * movementSpeed; + } + + public void MouseUpdate(int mouseDeltaX, int mouseDeltaY) + { + rotation.y += mouseDeltaX * 0.25f; + rotation.x += mouseDeltaY * 0.25f; + if (rotation.x <= -90.0f) + rotation.x = -90f; + if (rotation.x >= 90.0f) + rotation.x = 90f; + ryMatrix = Matrix4.GetRotationMatrixY(Core.ToRadians(rotation.y)); + rxMatrix = Matrix4.GetRotationMatrixX(Core.ToRadians(rotation.x)); + } + + public float[] GetWorldToViewMatrix() => (rxMatrix * ryMatrix * Matrix4.GetTranslationMatrix(position.x, position.y, position.z)).ToGLMatrix(); } - - public float Yaw - { - set - { - this.rotation.y = value; - this.ryMatrix = Matrix4.GetRotationMatrixY(Core.ToRadians((double) this.rotation.y)); - } - get => this.rotation.y; - } - - public float Pitch - { - set - { - this.rotation.x = value; - if ((double) this.rotation.x < -180.0) - this.rotation.x = 360f + this.rotation.x; - if ((double) this.rotation.x > 180.0) - this.rotation.x = 360f - this.rotation.x; - this.rxMatrix = Matrix4.GetRotationMatrixX(Core.ToRadians((double) this.rotation.x)); - } - get => this.rotation.x; - } - - public float Roll - { - set => this.rotation.z = value; - get => this.rotation.z; - } - - public float GetXRotation() => this.rotation.x; - - public float GetYRotation() => this.rotation.y; - - public float GetZRotation() => this.rotation.z; - - public void Reset() - { - this.position = new Vector3(0.0f, 0.0f, -2161f); - this.rotation = new Vector3(0.0f, 0.0f, 0.0f); - } - - public void LookAt(Vector3 target) - { - this.rotation.x = 0.0f; - this.rotation.y = 0.0f; - this.rotation.z = 0.0f; - this.position.x = target.x * -1f; - this.position.y = (float) ((double) target.y * -1.0 - 50.0); - this.position.z = (float) ((double) target.z * -1.0 - 500.0); - this.ryMatrix = Matrix4.GetRotationMatrixY(Core.ToRadians((double) this.rotation.y)); - this.rxMatrix = Matrix4.GetRotationMatrixX(Core.ToRadians((double) this.rotation.x)); - } - - public void PanUpdate(bool forward, bool back, bool left, bool right) - { - float movementSpeed = this.movementSpeed; - float num1 = (float) Math.Sin(Core.ToRadians((double) this.rotation.y)) * movementSpeed; - float num2 = (float) Math.Cos(Core.ToRadians((double) this.rotation.y)) * movementSpeed; - float num3 = (float) Math.Sin(Core.ToRadians((double) this.rotation.x)) * movementSpeed; - Vector3 vector3 = new Vector3(); - if (forward) - { - vector3.x -= num1; - vector3.z += num2; - vector3.y += num3; - } - if (back) - { - vector3.x += num1; - vector3.z -= num2; - vector3.y -= num3; - } - if (left) - { - vector3.x -= num2; - vector3.z -= num1; - } - if (right) - { - vector3.x += num2; - vector3.z += num1; - } - this.position += vector3; - } - - public void MouseUpdate(int mouseDeltaX, int mouseDeltaY) - { - this.rotation.y += (float) mouseDeltaX * 0.25f; - this.rotation.x += (float) mouseDeltaY * 0.25f; - if ((double) this.rotation.x <= -90.0) - this.rotation.x = -90f; - if ((double) this.rotation.x >= 90.0) - this.rotation.x = 90f; - this.ryMatrix = Matrix4.GetRotationMatrixY(Core.ToRadians((double) this.rotation.y)); - this.rxMatrix = Matrix4.GetRotationMatrixX(Core.ToRadians((double) this.rotation.x)); - } - - public float[] GetWorldToViewMatrix() => (this.rxMatrix * this.ryMatrix * Matrix4.GetTranslationMatrix(this.position.x, this.position.y, this.position.z)).ToGLMatrix(); - } -} +} \ No newline at end of file diff --git a/BanjoKazooieLevelEditor/ModelViewer.cs b/BanjoKazooieLevelEditor/ModelViewer.cs index 93dc5bf..8cddc4e 100644 --- a/BanjoKazooieLevelEditor/ModelViewer.cs +++ b/BanjoKazooieLevelEditor/ModelViewer.cs @@ -37,6 +37,8 @@ public class ModelViewer : Form private bool zoomOut; private bool left; private bool right; + private bool moveUp; + private bool moveDown; private uint vboVertexHandle; private uint vboColorHandle; private uint vboTexCoordHandle; @@ -290,9 +292,9 @@ private void readObjectsXML() private void camera() { bool flag = false; - if (this.zoomIn | this.zoomOut | this.left | this.right) + if (zoomIn | zoomOut | left | right | moveUp | moveDown) { - this.BBCamera.PanUpdate(this.zoomIn, this.zoomOut, this.left, this.right); + BBCamera.PanUpdate(zoomIn, zoomOut, left, right, moveUp, moveDown); flag = true; } Point mousePosition1 = Control.MousePosition; @@ -479,22 +481,22 @@ private void ModelViewer_KeyUp(object sender, KeyEventArgs e) switch (e.KeyCode) { case Keys.A: - this.right = false; + right = false; break; case Keys.D: - this.left = false; + left = false; break; case Keys.E: - this.zoomOut = false; + moveUp = false; break; case Keys.Q: - this.zoomIn = false; + moveDown = false; break; case Keys.S: - this.zoomOut = false; + zoomOut = false; break; case Keys.W: - this.zoomIn = false; + zoomIn = false; break; } } @@ -526,22 +528,22 @@ private void BKOpenGLC_KeyDown(object sender, KeyEventArgs e) switch (e.KeyCode) { case Keys.A: - this.right = true; + right = true; break; case Keys.D: - this.left = true; + left = true; break; case Keys.E: - this.zoomOut = true; + moveUp = true; break; case Keys.Q: - this.zoomIn = true; + moveDown = true; break; case Keys.S: - this.zoomOut = true; + zoomOut = true; break; case Keys.W: - this.zoomIn = true; + zoomIn = true; break; } }