Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] control from player input #126

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/GameBall/logic/player_input.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/GameBall/logic/player_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
};

Expand Down
93 changes: 52 additions & 41 deletions src/GameBall/logic/units/regular_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,52 +39,63 @@ 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();
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};
// }
// }
// }

sphere.velocity *= std::pow(0.5f, delta_time);
sphere.angular_velocity *= std::pow(0.2f, delta_time);
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 (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);
sphere.angular_velocity +=
moving_direction * angular_acceleration * delta_time;
}

if (input.brake) {
sphere.angular_velocity/=1.5;
}
if(input.copy&&!copy_flag&&!last_copy_flag)
{
world_->CreateUnit<Logic::Units::RegularBall>(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.8f, delta_time);
sphere.angular_velocity *= std::pow(0.5f, delta_time);

position_ = sphere.position;
velocity_ = sphere.velocity;
Expand Down