From fa2e64e5864c41ae3a7930d24b968a6fc28645d7 Mon Sep 17 00:00:00 2001 From: Sherlockk <3100897298@qq.com> Date: Fri, 12 Jan 2024 17:50:26 +0800 Subject: [PATCH 1/7] accomplish input control --- src/GameBall/logic/player_input.cpp | 8 +-- src/GameBall/logic/units/regular_ball.cpp | 76 +++++++++++------------ 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/GameBall/logic/player_input.cpp b/src/GameBall/logic/player_input.cpp index c002a91..3add4dc 100644 --- a/src/GameBall/logic/player_input.cpp +++ b/src/GameBall/logic/player_input.cpp @@ -8,10 +8,10 @@ PlayerInputController::PlayerInputController(GameBall *app) : app_(app) { PlayerInput PlayerInputController::GetInput() { auto window = app_->Window(); - input_.move_forward = (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS); - input_.move_backward = (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS); - input_.move_left = (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS); - input_.move_right = (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS); + input_.move_forward = (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS); + input_.move_backward = (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS); + input_.move_left = (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS); + input_.move_right = (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS); input_.brake = (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS); auto camera_controller = app_->CameraController(); auto pitch_yaw = camera_controller->GetPitchYaw(); diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index af1cc95..926eef9 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -45,43 +45,43 @@ void RegularBall::UpdateTick() { auto physics_world = world_->PhysicsWorld(); auto &sphere = physics_world->GetSphere(sphere_id_); - // auto owner = world_->GetPlayer(player_id_); - // if (owner) { - // if (UnitId() == owner->PrimaryUnitId()) { - // auto input = owner->TakePlayerInput(); - // - // glm::vec3 forward = glm::normalize(glm::vec3{input.orientation}); - // glm::vec3 right = - // glm::normalize(glm::cross(forward, glm::vec3{0.0f, 1.0f, 0.0f})); - // - // glm::vec3 moving_direction{}; - // - // float angular_acceleration = glm::radians(2880.0f); - // - // if (input.move_forward) { - // moving_direction -= right; - // } - // if (input.move_backward) { - // moving_direction += right; - // } - // if (input.move_left) { - // moving_direction -= forward; - // } - // if (input.move_right) { - // moving_direction += forward; - // } - // - // if (glm::length(moving_direction) > 0.0f) { - // moving_direction = glm::normalize(moving_direction); - // sphere.angular_velocity += - // moving_direction * angular_acceleration * delta_time; - // } - // - // if (input.brake) { - // sphere.angular_velocity = glm::vec3{0.0f}; - // } - // } - // } + auto owner = world_->GetPlayer(player_id_); + if (owner) { + if (UnitId() == owner->PrimaryUnitId()) { + auto input = owner->TakePlayerInput(); + + glm::vec3 forward = glm::normalize(glm::vec3{input.orientation}); + glm::vec3 right = + glm::normalize(glm::cross(forward, glm::vec3{0.0f, 1.0f, 0.0f})); + + glm::vec3 moving_direction{}; + + float angular_acceleration = glm::radians(2880.0f); + + if (input.move_forward) { + moving_direction -= right; + } + if (input.move_backward) { + moving_direction += right; + } + if (input.move_left) { + moving_direction -= forward; + } + if (input.move_right) { + moving_direction += forward; + } + + if (glm::length(moving_direction) > 0.0f) { + moving_direction = glm::normalize(moving_direction); + sphere.angular_velocity += + moving_direction * angular_acceleration * delta_time; + } + + if (input.brake) { + sphere.angular_velocity = glm::vec3{0.0f}; + } + } + } sphere.velocity *= std::pow(0.5f, delta_time); sphere.angular_velocity *= std::pow(0.2f, delta_time); @@ -144,4 +144,4 @@ glm::vec3 RegularBall::AngularMomentum() const { return augular_momentum_; } -} // namespace GameBall::Logic::Units +} namespace GameBall::Logic::Units From 9b39e03c0a9a246bc4b50de83d27f72e304c34f9 Mon Sep 17 00:00:00 2001 From: Sherlockk <3100897298@qq.com> Date: Fri, 12 Jan 2024 18:08:34 +0800 Subject: [PATCH 2/7] nothing changed --- src/GameBall/logic/player_input.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/GameBall/logic/player_input.cpp b/src/GameBall/logic/player_input.cpp index 3add4dc..9067c6f 100644 --- a/src/GameBall/logic/player_input.cpp +++ b/src/GameBall/logic/player_input.cpp @@ -8,10 +8,10 @@ PlayerInputController::PlayerInputController(GameBall *app) : app_(app) { PlayerInput PlayerInputController::GetInput() { auto window = app_->Window(); - input_.move_forward = (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS); - input_.move_backward = (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS); - input_.move_left = (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS); - input_.move_right = (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS); + input_.move_forward = (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) | (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS); + input_.move_backward = (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) | (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS); + input_.move_left = (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) | (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS); + input_.move_right = (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) | (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS); input_.brake = (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS); auto camera_controller = app_->CameraController(); auto pitch_yaw = camera_controller->GetPitchYaw(); From c43597cc160bf786514cb5fd5d926fcefddcc74a Mon Sep 17 00:00:00 2001 From: Sherlockk <3100897298@qq.com> Date: Fri, 12 Jan 2024 18:10:02 +0800 Subject: [PATCH 3/7] nothing changed again --- src/GameBall/logic/units/regular_ball.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index 926eef9..d803d58 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -144,4 +144,4 @@ glm::vec3 RegularBall::AngularMomentum() const { return augular_momentum_; } -} namespace GameBall::Logic::Units +} //namespace GameBall::Logic::Units From 12cc7fc8a95581ad994e30bacc2f743ca30efb58 Mon Sep 17 00:00:00 2001 From: Sherlockk-23 <145881280+Sherlockk-23@users.noreply.github.com> Date: Fri, 19 Jan 2024 19:57:58 +0800 Subject: [PATCH 4/7] Update camera_third_person.cpp --- src/GameBall/core/camera_third_person.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/GameBall/core/camera_third_person.cpp b/src/GameBall/core/camera_third_person.cpp index 3175bca..f37b7e6 100644 --- a/src/GameBall/core/camera_third_person.cpp +++ b/src/GameBall/core/camera_third_person.cpp @@ -99,4 +99,8 @@ void CameraControllerThirdPerson::CursorMove(float x, float y) { dst_pitch_ += y * 0.1f; dst_pitch_ = glm::clamp(dst_pitch_, -89.0f, 89.0f); } + +void CameraControllerThirdPerson::CursorScroll(float x, float y) { + dst_distance_ += y * 0.5f; +} } // namespace GameBall From 72d259489ab65c31f66c08011c11709271679720 Mon Sep 17 00:00:00 2001 From: Sherlockk-23 <145881280+Sherlockk-23@users.noreply.github.com> Date: Fri, 19 Jan 2024 19:58:36 +0800 Subject: [PATCH 5/7] Update camera_third_person.h --- src/GameBall/core/camera_third_person.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/GameBall/core/camera_third_person.h b/src/GameBall/core/camera_third_person.h index 463242f..45f4e54 100644 --- a/src/GameBall/core/camera_third_person.h +++ b/src/GameBall/core/camera_third_person.h @@ -33,6 +33,8 @@ class CameraControllerThirdPerson { void CursorMove(float x, float y); + void CursorScroll(float x, float y); + private: GameX::Graphics::PCamera camera_; float dst_distance_{10.0f}; From 16b24b54a084d9f93581eebf3d592c0e148f2cdd Mon Sep 17 00:00:00 2001 From: Sherlockk-23 <145881280+Sherlockk-23@users.noreply.github.com> Date: Fri, 19 Jan 2024 19:58:51 +0800 Subject: [PATCH 6/7] Update game_ball.cpp --- src/GameBall/core/game_ball.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/GameBall/core/game_ball.cpp b/src/GameBall/core/game_ball.cpp index a47b79e..cce4bb5 100644 --- a/src/GameBall/core/game_ball.cpp +++ b/src/GameBall/core/game_ball.cpp @@ -150,4 +150,13 @@ void GameBall::CursorPosCallback(double xpos, double ypos) { ignore_next_mouse_move_ = false; } +void GameBall::ScrollCallback(double xoffset, double yoffset) { + + if (!ignore_next_mouse_move_) { + camera_controller_->CursorScroll(xoffset, yoffset); + } + + ignore_next_mouse_move_ = false; +} + } // namespace GameBall From 57a582c3d42b28ff1f47052539018bbf4738bafa Mon Sep 17 00:00:00 2001 From: Sherlockk-23 <145881280+Sherlockk-23@users.noreply.github.com> Date: Fri, 19 Jan 2024 19:59:02 +0800 Subject: [PATCH 7/7] Update game_ball.h --- src/GameBall/core/game_ball.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/GameBall/core/game_ball.h b/src/GameBall/core/game_ball.h index 3da4bd6..eb02e78 100644 --- a/src/GameBall/core/game_ball.h +++ b/src/GameBall/core/game_ball.h @@ -47,6 +47,8 @@ class GameBall : public GameX::Base::Application { void CursorPosCallback(double xpos, double ypos) override; + void ScrollCallback(double xoffset, double yoffset) override; + CameraControllerThirdPerson *CameraController() { return camera_controller_.get(); }