From ea97dc3b50eb0ab07faefa3dde8dd2cb95c71bcf Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 4 Mar 2023 15:17:29 +0800 Subject: [PATCH] Fixed Managed vector3 forward & back --- Assets/Scenes/SS_Playground.shade | 38 +++------- Assets/Scripts/Tests/MovementTest.cs | 80 +++++++++++++++++++++ Assets/Scripts/Tests/MovementTest.cs.shmeta | 3 + SHADE_Managed/src/Math/Vector3.hxx | 4 +- 4 files changed, 93 insertions(+), 32 deletions(-) create mode 100644 Assets/Scripts/Tests/MovementTest.cs create mode 100644 Assets/Scripts/Tests/MovementTest.cs.shmeta diff --git a/Assets/Scenes/SS_Playground.shade b/Assets/Scenes/SS_Playground.shade index 9acf4216..7aae078f 100644 --- a/Assets/Scenes/SS_Playground.shade +++ b/Assets/Scenes/SS_Playground.shade @@ -45,7 +45,7 @@ Components: Transform Component: Translate: {x: 0.141888797, y: 5, z: 0} - Rotate: {x: -0, y: 0, z: 0.490080774} + Rotate: {x: -0, y: 0, z: 0} Scale: {x: 0.999999702, y: 0.999999702, z: 1} IsActive: true RigidBody Component: @@ -68,9 +68,9 @@ Collider Component: Colliders: - Is Trigger: false - Collision Tag: 1 - Type: Sphere - Radius: 1 + Collision Tag: 0 + Type: Box + Half Extents: {x: 1, y: 1, z: 1} Friction: 0.400000006 Bounciness: 0 Density: 1 @@ -78,30 +78,8 @@ Rotation Offset: {x: 0, y: 0, z: 0} IsActive: true Scripts: - - Type: PhysicsTestObj + - Type: MovementTest Enabled: true - forceAmount: 50 - torqueAmount: 25 -- EID: 2 - Name: Default - IsActive: true - NumberOfChildren: 0 - Components: - Transform Component: - Translate: {x: 0.34412086, y: 3.23541069, z: 0} - Rotate: {x: 0, y: 0, z: 0.490080804} - Scale: {x: 0.999999702, y: 0.999999702, z: 1} - IsActive: true - Collider Component: - Colliders: - - Is Trigger: false - Collision Tag: 0 - Type: Sphere - Radius: 1 - Friction: 0.400000006 - Bounciness: 0 - Density: 1 - Position Offset: {x: 0, y: 0, z: 0} - Rotation Offset: {x: 0, y: 0, z: 0} - IsActive: false - Scripts: ~ \ No newline at end of file + forceAmount: 25 + torqueAmount: 50 + rayDistance: 1 \ No newline at end of file diff --git a/Assets/Scripts/Tests/MovementTest.cs b/Assets/Scripts/Tests/MovementTest.cs new file mode 100644 index 00000000..c1c18ff4 --- /dev/null +++ b/Assets/Scripts/Tests/MovementTest.cs @@ -0,0 +1,80 @@ +using SHADE; +using System; +using System.Collections.Generic; +using static Item; + + +public class MovementTest : Script +{ + public Transform tf { get; set; } + public RigidBody body { get; set; } + + // Movement input booleans + internal bool move = false; + internal bool[] rotate = new bool[2]; + + internal Vector3[] rotateVec = new Vector3[2] + { + Vector3.Up, + Vector3.Down + }; + + internal Input.KeyCode[] rotateInputKeys = new Input.KeyCode[2] + { + Input.KeyCode.J, + Input.KeyCode.L + }; + + public float forceAmount = 50.0f; + public float torqueAmount = 100.0f; + public float rayDistance = 1.0f; + + protected override void awake() + { + tf = GetComponent(); + body = GetComponent(); + + for (int i = 0; i < 2; ++i) + rotate[i] = false; + } + + protected override void update() + { + if (Input.GetKey(Input.KeyCode.W)) + move = true; + + for (int i = 0; i < 2; ++i) + { + if (Input.GetKeyDown(rotateInputKeys[i])) + rotate[i] = true; + } + + Vector3 dirNor = tf.Forward; + List rayList1 = Physics.ColliderRaycast(GameObject, new Ray(Vector3.Zero, Vector3.RotateY(dirNor, SHADE.Math.DegreesToRadians(22.5f))), rayDistance, false, (ushort)65535); + List rayList2 = Physics.ColliderRaycast(GameObject, new Ray(Vector3.Zero, Vector3.RotateY(dirNor, SHADE.Math.DegreesToRadians(-22.5f))), rayDistance, false, (ushort)65535); + List rayList3 = Physics.ColliderRaycast(GameObject, new Ray(Vector3.Zero, dirNor), rayDistance, false, (ushort)65535); + } + + protected override void fixedUpdate() + { + if (move) + { + // Apply force in the direction the box is facing + // AKA Transform's forward + body.AddForce(tf.Forward * forceAmount); + Debug.Log($"Forward: <{tf.Forward.x}, {tf.Forward.y}, {tf.Forward.z}"); + move = false; + } + + for (int i = 0; i < 2; ++i) + { + bool shouldRotate = rotate[i]; + + if (shouldRotate) + { + body.AddTorque(rotateVec[i] * torqueAmount); + rotate[i] = false; + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Tests/MovementTest.cs.shmeta b/Assets/Scripts/Tests/MovementTest.cs.shmeta new file mode 100644 index 00000000..a9942abe --- /dev/null +++ b/Assets/Scripts/Tests/MovementTest.cs.shmeta @@ -0,0 +1,3 @@ +Name: MovementTest +ID: 164554656 +Type: 9 diff --git a/SHADE_Managed/src/Math/Vector3.hxx b/SHADE_Managed/src/Math/Vector3.hxx index 31a373ba..e3a921ad 100644 --- a/SHADE_Managed/src/Math/Vector3.hxx +++ b/SHADE_Managed/src/Math/Vector3.hxx @@ -45,7 +45,7 @@ namespace SHADE /// /// Shorthand for writing Vector3(0, 0, -1). /// - static initonly Vector3 Back = Vector3(0.0f, 0.0f, -1.0f); + static initonly Vector3 Back = Vector3(0.0f, 0.0f, 1.0f); /// /// Shorthand for writing Vector3(0, -1, 0). /// @@ -53,7 +53,7 @@ namespace SHADE /// /// Shorthand for writing Vector3(0, 0, 1). /// - static initonly Vector3 Forward = Vector3(0.0f, 0.0f, 1.0f); + static initonly Vector3 Forward = Vector3(0.0f, 0.0f, -1.0f); /// /// Shorthand for writing Vector3(-1, 0, 0). ///