From ea5619e2a0478d034a6c50d7e8d92f20dd9cee31 Mon Sep 17 00:00:00 2001 From: StratiX0 Date: Wed, 3 Apr 2024 12:15:53 +0200 Subject: [PATCH] ajout deplacement camera --- enginecustom/Positionclass.cpp | 60 +++++++++++++++++++++++++++ enginecustom/Positionclass.h | 3 ++ enginecustom/applicationclass.cpp | 17 ++++++-- enginecustom/inputclass.cpp | 67 +++++++++++++++++++++++++++++++ enginecustom/inputclass.h | 6 +++ 5 files changed, 150 insertions(+), 3 deletions(-) diff --git a/enginecustom/Positionclass.cpp b/enginecustom/Positionclass.cpp index abe8d1d..c378b94 100644 --- a/enginecustom/Positionclass.cpp +++ b/enginecustom/Positionclass.cpp @@ -5,6 +5,9 @@ PositionClass::PositionClass() m_frameTime = 0.0f; m_rotationY = 0.0f; m_rotationX = 0.0f; + m_positionX = 0.0f; + m_positionY = 0.0f; + m_positionZ = 0.0f; m_leftTurnSpeed = 0.0f; m_rightTurnSpeed = 0.0f; } @@ -32,6 +35,14 @@ void PositionClass::GetRotation(float& y, float& x) return; } +void PositionClass::GetPosition(float& x, float& y, float& z) +{ + x = m_positionX; + y = m_positionY; + z = m_positionZ; + return; +} + void PositionClass::TurnLeft(bool keydown) { // If the key is pressed increase the speed at which the camera turns left. If not slow down the turn speed. @@ -128,4 +139,53 @@ void PositionClass::TurnMouse(float deltaX, float deltaY) } return; +} + +void PositionClass::MoveCamera(bool forward, bool backward, bool left, bool right, bool up, bool down) +{ + float radians; + float speed; + + // Set the speed of the camera. + speed = 0.1f; + + // Convert degrees to radians. + radians = m_rotationY * 0.0174532925f; + + // Update the position. + if (forward) + { + m_positionX += sinf(radians) * speed; + m_positionZ += cosf(radians) * speed; + } + + if (backward) + { + m_positionX -= sinf(radians) * speed; + m_positionZ -= cosf(radians) * speed; + } + + if (left) + { + m_positionX -= cosf(radians) * speed; + m_positionZ += sinf(radians) * speed; + } + + if (right) + { + m_positionX += cosf(radians) * speed; + m_positionZ -= sinf(radians) * speed; + } + + if (up) + { + m_positionY += speed; + } + + if (down) + { + m_positionY -= speed; + } + + return; } \ No newline at end of file diff --git a/enginecustom/Positionclass.h b/enginecustom/Positionclass.h index a57799a..5048610 100644 --- a/enginecustom/Positionclass.h +++ b/enginecustom/Positionclass.h @@ -20,14 +20,17 @@ public: void SetFrameTime(float); void GetRotation(float&, float&); + void GetPosition(float&, float&, float&); void TurnLeft(bool); void TurnRight(bool); void TurnMouse(float, float); + void MoveCamera(bool, bool, bool, bool, bool, bool); private: float m_frameTime; float m_rotationY, m_rotationX; + float m_positionX, m_positionY, m_positionZ; float m_leftTurnSpeed, m_rightTurnSpeed, m_horizontalTurnSpeed, m_verticalTurnSpeed; }; diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 220a7c1..ce93324 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -573,8 +573,8 @@ void ApplicationClass::Shutdown() bool ApplicationClass::Frame(InputClass* Input) { int mouseX, mouseY, currentMouseX, currentMouseY; - bool result, mouseDown, keyDown; - float rotationY, rotationX; + bool result, mouseDown, keyDown, buttonQ, buttonD, buttonZ, buttonS, buttonA, buttonE; + float rotationY, rotationX, positionX, positionY, positionZ; float frameTime; @@ -625,7 +625,18 @@ bool ApplicationClass::Frame(InputClass* Input) // Get the current view point rotation. m_Position->GetRotation(rotationY, rotationX); - // Set the rotation of the camera. + // Check if the a(q), d, w(z), s, q(a), e have been pressed, if so move the camera accordingly. + buttonQ = Input->IsAPressed(); + buttonD = Input->IsDPressed(); + buttonZ = Input->IsWPressed(); + buttonS = Input->IsSPressed(); + buttonA = Input->IsQPressed(); + buttonE = Input->IsEPressed(); + m_Position->MoveCamera(buttonZ, buttonS, buttonQ, buttonD, buttonE, buttonA); + m_Position->GetPosition(positionX, positionY, positionZ); + + // Set the postion and rotation of the camera. + m_Camera->SetPosition(positionX, positionY, positionZ); m_Camera->SetRotation(rotationX, rotationY, 0.0f); m_Camera->Render(); diff --git a/enginecustom/inputclass.cpp b/enginecustom/inputclass.cpp index 5dd3f40..2a2c91f 100644 --- a/enginecustom/inputclass.cpp +++ b/enginecustom/inputclass.cpp @@ -276,6 +276,73 @@ bool InputClass::IsRightArrowPressed() return false; } +/////////////////////////////////////////////////// +// Les touches correspondent aux claviers QWERTY // +/////////////////////////////////////////////////// + +bool InputClass::IsAPressed() +{ + // Touche A sur QWERTY, Q sur AZERTY + if (m_keyboardState[DIK_A] & 0x80) + { + return true; + } + + return false; +} + +bool InputClass::IsDPressed() +{ + if (m_keyboardState[DIK_D] & 0x80) + { + return true; + } + + return false; +} + +bool InputClass::IsWPressed() +{ + // Touche W sur QWERTY, Z sur AZERTY + if (m_keyboardState[DIK_W] & 0x80) + { + return true; + } + + return false; +} + +bool InputClass::IsSPressed() +{ + if (m_keyboardState[DIK_S] & 0x80) + { + return true; + } + + return false; +} + +bool InputClass::IsQPressed() +{ + // Touche Q sur QWERTY, A sur AZERTY + if (m_keyboardState[DIK_Q] & 0x80) + { + return true; + } + + return false; +} + +bool InputClass::IsEPressed() +{ + if (m_keyboardState[DIK_E] & 0x80) + { + return true; + } + + return false; +} + void InputClass::GetMouseLocation(int& mouseX, int& mouseY) { mouseX = m_mouseX; diff --git a/enginecustom/inputclass.h b/enginecustom/inputclass.h index 0e80bb2..d559872 100644 --- a/enginecustom/inputclass.h +++ b/enginecustom/inputclass.h @@ -38,6 +38,12 @@ public: void KeyUp(unsigned int); bool IsLeftArrowPressed(); bool IsRightArrowPressed(); + bool IsAPressed(); + bool IsDPressed(); + bool IsWPressed(); + bool IsSPressed(); + bool IsQPressed(); + bool IsEPressed(); bool IsKeyDown(unsigned int);