fully mobile around a cube
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 17 Dec 2021 04:41:48 +0000 (22:41 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 17 Dec 2021 04:41:48 +0000 (22:41 -0600)
src/main.onyx

index 463dce4d3f465c24a7466cbbeba8c9d67b54f69c..29c658e2adc07407ce918c95d6f516cfade7f84a 100644 (file)
@@ -215,8 +215,9 @@ Vector3 :: struct {
     }
 }
 
-#operator + macro (v1, v2: Vector3) => Vector3.{ v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };
-#operator - macro (v1, v2: Vector3) => Vector3.{ v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };
+#operator + macro (v1, v2: Vector3)    => Vector3.{ v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };
+#operator - macro (v1, v2: Vector3)    => Vector3.{ v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };
+#operator * macro (v: Vector3, s: f32) => Vector3.{ v.x * s,     v.y * s,     v.z * s };
 
 update_world_matrix :: (position: Vector3, facing: Vector3, up: Vector3) {
     forward := Vector3.norm(facing); //center - position);
@@ -239,6 +240,7 @@ update_world_matrix :: (position: Vector3, facing: Vector3, up: Vector3) {
 
 theta := 0.0f; // y-axis
 phi := 0.0f; // x-axis
+pos := Vector3.{ 5, 5, 5 };
 
 update :: (dt: f32) {
     #persist t := 0.0f;
@@ -261,7 +263,22 @@ update :: (dt: f32) {
         math.cos(theta) * math.cos(phi)
     };
 
-    update_world_matrix(.{ 5, 5, 5 }, facing, .{ 0, 1, 0 });
+    up := Vector3.{0,1,0};
+
+    if glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS {
+        pos += Vector3.norm(facing) * 3 * dt;
+    }
+    if glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS {
+        pos -= Vector3.norm(facing) * 3 * dt;
+    }
+    if glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS {
+        pos += Vector3.norm(Vector3.cross(facing, up)) * 3 * dt;
+    }
+    if glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS {
+        pos -= Vector3.norm(Vector3.cross(facing, up)) * 3 * dt;
+    }
+
+    update_world_matrix(pos, facing, up);
 }
 
 draw :: () {