From 5e5d44b3924261d33214cea8ae5ccff6ac431277 Mon Sep 17 00:00:00 2001 From: team109 Date: Mon, 22 Jan 2024 14:06:22 +0800 Subject: [PATCH 1/2] [feat] control from player input --- src/GameBall/logic/units/regular_ball.cpp | 74 +++++++++++------------ 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index af1cc95..1e1bb31 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); From df9b510be8b1b0e6b33a60b0b8c2af3af7f1f13c Mon Sep 17 00:00:00 2001 From: team109 Date: Mon, 22 Jan 2024 23:39:29 +0800 Subject: [PATCH 2/2] add features --- src/GameBall/logic/player_input.cpp | 7 +++++-- src/GameBall/logic/player_input.h | 4 ++++ src/GameBall/logic/units/regular_ball.cpp | 21 ++++++++++++++++----- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/GameBall/logic/player_input.cpp b/src/GameBall/logic/player_input.cpp index c002a91..2e08b55 100644 --- a/src/GameBall/logic/player_input.cpp +++ b/src/GameBall/logic/player_input.cpp @@ -1,18 +1,21 @@ #include "GameBall/logic/player_input.h" #include "GameBall/core/game_ball.h" - namespace GameBall::Logic { PlayerInputController::PlayerInputController(GameBall *app) : app_(app) { } - PlayerInput PlayerInputController::GetInput() { auto window = app_->Window(); + if(glfwGetKey(window,GLFW_KEY_Q)==GLFW_PRESS)exit(0); 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_.brake = (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS); + input_.copy=(glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS); + input_.freecamera = (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS); + input_.jump=(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS); + input_.rotate=(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS); auto camera_controller = app_->CameraController(); auto pitch_yaw = camera_controller->GetPitchYaw(); auto pitch = pitch_yaw.x; diff --git a/src/GameBall/logic/player_input.h b/src/GameBall/logic/player_input.h index 98b60c0..d25667f 100644 --- a/src/GameBall/logic/player_input.h +++ b/src/GameBall/logic/player_input.h @@ -12,6 +12,10 @@ struct PlayerInput { bool move_left{false}; bool move_right{false}; bool brake{false}; + bool copy{false}; + bool freecamera{false}; + bool jump{false}; + bool rotate{false}; glm::vec3 orientation{0.0f, 0.0f, 1.0f}; }; diff --git a/src/GameBall/logic/units/regular_ball.cpp b/src/GameBall/logic/units/regular_ball.cpp index 1e1bb31..a98f1b2 100644 --- a/src/GameBall/logic/units/regular_ball.cpp +++ b/src/GameBall/logic/units/regular_ball.cpp @@ -39,7 +39,7 @@ SYNC_ACTOR_FUNC(RegularBall) { actor->SetMotion(position_, velocity_, orientation_, augular_momentum_); actor->SetMomentOfInertia(sphere.inertia[0][0]); } - +static bool copy_flag,last_copy_flag; void RegularBall::UpdateTick() { float delta_time = world_->TickDeltaT(); auto physics_world = world_->PhysicsWorld(); @@ -70,6 +70,10 @@ void RegularBall::UpdateTick() { if (input.move_right) { moving_direction += forward; } + if (input.rotate) + { + moving_direction += glm::vec3{0.0f,0.1f,0.0f}; + } if (glm::length(moving_direction) > 0.0f) { moving_direction = glm::normalize(moving_direction); @@ -78,13 +82,20 @@ void RegularBall::UpdateTick() { } if (input.brake) { - sphere.angular_velocity = glm::vec3{0.0f}; + sphere.angular_velocity/=1.5; } + if(input.copy&&!copy_flag&&!last_copy_flag) + { + world_->CreateUnit(0,sphere.position,sphere.radius,sphere.mass); + } + last_copy_flag=copy_flag; + copy_flag=input.copy; + if(input.jump)sphere.velocity+=glm::vec3{0.0f,2.0f,0.0f}; + else sphere.velocity+=glm::vec3{0.0f,-0.6f,0.0f}; } } - - sphere.velocity *= std::pow(0.5f, delta_time); - sphere.angular_velocity *= std::pow(0.2f, delta_time); + sphere.velocity *= std::pow(0.8f, delta_time); + sphere.angular_velocity *= std::pow(0.5f, delta_time); position_ = sphere.position; velocity_ = sphere.velocity;