From f6adf1f37d24046f7ad6600498cbfdfbf17d9b66 Mon Sep 17 00:00:00 2001 From: OusmBlueNinja <89956790+OusmBlueNinja@users.noreply.github.com> Date: Fri, 3 Jan 2025 09:58:58 -0600 Subject: [PATCH] Updated Camera Script --- assets/scripts/camera.lua | 144 +++++++++++++++++++++++++------------- imgui.ini | 8 +-- 2 files changed, 100 insertions(+), 52 deletions(-) diff --git a/assets/scripts/camera.lua b/assets/scripts/camera.lua index 6998862..ee571d1 100644 --- a/assets/scripts/camera.lua +++ b/assets/scripts/camera.lua @@ -1,72 +1,120 @@ local Engine = require("./assets/scripts/engine") local KeyCode = require("./assets/scripts/keycode") - local transform = nil -local new_rotation = 0 +local currentRotation = 0 -- Current Y-axis rotation in degrees -local new_rotationSpeed = 120 -local Move_Speec = 600 +local rotationSpeed = 120 -- Degrees per second +local moveSpeed = 600 -- Units per second function OnInit() local gameObject = Engine.GetGameObjectByTag("Camera") - transform = gameObject:GetComponent("Transform") - Engine.Expose("Position X", "nil") + if not gameObject then + Engine.Log("Error: Camera GameObject not found!") + return + end + transform = gameObject:GetComponent("Transform") + if not transform then + Engine.Log("Error: Transform component not found on Camera!") + return + end + + -- Initialize currentRotation based on the transform's current rotation + local rotation = transform:GetRotation() + currentRotation = rotation.y + + -- Expose initial position and rotation for debugging or UI purposes + Engine.Expose("Position X", rotation.x) + Engine.Expose("Position Y", rotation.y) + Engine.Expose("Rotation Y", currentRotation) Engine.Log("Init Done") end function OnUpdate(deltaTime) - local gameObject = Engine.GetGameObjectByTag("Camera") - transform = gameObject:GetComponent("Transform") - - local old_rotation = transform:GetRotation() - - - - local Position = transform:GetPosition() - - if Engine.KeyDown(KeyCode.W) then - Position.x = Position.x + (deltaTime * Move_Speec) - end - if Engine.KeyDown(KeyCode.S) then - Position.x = Position.x - (deltaTime * Move_Speec) - end - if Engine.KeyDown(KeyCode.D) then - Position.z = Position.z + (deltaTime * Move_Speec) - end - if Engine.KeyDown(KeyCode.A) then - Position.z = Position.z - (deltaTime * Move_Speec) + if not transform then + Engine.Log("Warning: Transform is not initialized.") + return end - transform:SetPosition(Position) - - Engine.Expose("Position X", Position.x) - Engine.Expose("Position Y", Position.y) - Engine.Expose("Rotation", new_rotation) - - - - - - local rotation = { - x = old_rotation.x, -- Preserving existing new_rotation on X-axis - y = new_rotation, -- Updated new_rotation on Y-axis for spinning - z = old_rotation.z -- Preserving existing new_rotation on Z-axis - } - -- Apply the new new_rotation to the Transform component - transform:SetRotation(rotation) + -- Retrieve current rotation and position + local rotation = transform:GetRotation() + local position = transform:GetPosition() + -- Update currentRotation based on input if Engine.KeyDown(KeyCode.Q) then - new_rotation = new_rotation - (deltaTime * new_rotationSpeed) - + currentRotation = currentRotation - (deltaTime * rotationSpeed) end if Engine.KeyDown(KeyCode.E) then - new_rotation = new_rotation + (deltaTime * new_rotationSpeed) + currentRotation = currentRotation + (deltaTime * rotationSpeed) + end + -- Normalize rotation to [0, 360) degrees + if currentRotation >= 360 then + currentRotation = currentRotation - 360 + elseif currentRotation < 0 then + currentRotation = currentRotation + 360 end - if new_rotation >= 360 then - new_rotation = 0 + + -- Apply the updated rotation to the Transform component + local newRotation = { + x = rotation.x, -- Preserving existing rotation on X-axis + y = currentRotation, -- Updated rotation on Y-axis + z = rotation.z -- Preserving existing rotation on Z-axis + } + transform:SetRotation(newRotation) + + -- Calculate movement direction based on currentRotation + local rotationRad = math.rad(currentRotation) + + -- Forward vector (positive Z direction) + local forward = { + x = math.cos(rotationRad), + z = math.sin(rotationRad) + } + + -- Right vector (positive X direction) + local right = { + x = math.sin(rotationRad), + z = -math.cos(rotationRad) + } + + -- Initialize movement deltas + local deltaX = 0 + local deltaZ = 0 + + -- Corrected Key-to-Direction Mapping: + -- W/S: Forward/Backward + -- A/D: Left/Right + if Engine.KeyDown(KeyCode.W) then + -- Move forward + deltaX = deltaX + forward.x * moveSpeed * deltaTime + deltaZ = deltaZ + forward.z * moveSpeed * deltaTime end + if Engine.KeyDown(KeyCode.S) then + -- Move backward + deltaX = deltaX - forward.x * moveSpeed * deltaTime + deltaZ = deltaZ - forward.z * moveSpeed * deltaTime + end + if Engine.KeyDown(KeyCode.A) then + -- Move right + deltaX = deltaX + right.x * moveSpeed * deltaTime + deltaZ = deltaZ + right.z * moveSpeed * deltaTime + end + if Engine.KeyDown(KeyCode.D) then + -- Move left + deltaX = deltaX - right.x * moveSpeed * deltaTime + deltaZ = deltaZ - right.z * moveSpeed * deltaTime + end + + -- Update position based on calculated deltas + position.x = position.x + deltaX + position.z = position.z + deltaZ + transform:SetPosition(position) + + -- Expose updated position and rotation for debugging or UI purposes + Engine.Expose("Position X", position.x) + Engine.Expose("Position Y", position.y) + Engine.Expose("Rotation Y", currentRotation) end diff --git a/imgui.ini b/imgui.ini index 271a1ba..a73f4d8 100644 --- a/imgui.ini +++ b/imgui.ini @@ -152,10 +152,10 @@ Collapsed=0 DockId=0x00000022,0 [Window][ Profiler] -Pos=345,28 -Size=1189,816 +Pos=345,846 +Size=1189,323 Collapsed=0 -DockId=0x0000001F,1 +DockId=0x00000025,1 [Table][0xE9E836E4,4] Column 0 Weight=1.2999 @@ -193,7 +193,7 @@ DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,51 Size= DockNode ID=0x00000017 Parent=0x0000000D SizeRef=1202,776 Split=Y Selected=0xDFF75B3F DockNode ID=0x0000001D Parent=0x00000017 SizeRef=518,720 Split=Y Selected=0x9A7B23B9 DockNode ID=0x0000001F Parent=0x0000001D SizeRef=549,359 CentralNode=1 Selected=0x9A7B23B9 - DockNode ID=0x00000025 Parent=0x0000001D SizeRef=549,323 Selected=0x1F29F1F5 + DockNode ID=0x00000025 Parent=0x0000001D SizeRef=549,323 Selected=0x7A66B86B DockNode ID=0x0000001E Parent=0x00000017 SizeRef=518,417 Selected=0xC74E1AEE DockNode ID=0x00000018 Parent=0x0000000D SizeRef=1202,364 Split=X Selected=0x1C0788A1 DockNode ID=0x00000019 Parent=0x00000018 SizeRef=601,364 Selected=0x1C0788A1