Skip to content
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
4 changes: 4 additions & 0 deletions src/GameBall/logic/player_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ PlayerInput PlayerInputController::GetInput() {
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_.accelerate = (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS);
input_.flip_gravity = (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS);

auto camera_controller = app_->CameraController();
auto pitch_yaw = camera_controller->GetPitchYaw();
auto pitch = pitch_yaw.x;
Expand All @@ -21,6 +24,7 @@ PlayerInput PlayerInputController::GetInput() {
-glm::cos(glm::radians(yaw))};
auto result = input_;
input_ = {};
input_.flip_gravity = result.flip_gravity;
return result;
}

Expand Down
2 changes: 2 additions & 0 deletions src/GameBall/logic/player_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct PlayerInput {
bool move_left{false};
bool move_right{false};
bool brake{false};
bool accelerate{false};
bool flip_gravity{0};
glm::vec3 orientation{0.0f, 0.0f, 1.0f};
};

Expand Down
88 changes: 51 additions & 37 deletions src/GameBall/logic/units/regular_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,57 @@ 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;
}

float acceleration_multiplier = 1;
if (input.accelerate) {
acceleration_multiplier = 1.5;
}

if (glm::length(moving_direction) > 0.0f) {
moving_direction = glm::normalize(moving_direction);
sphere.angular_velocity +=
acceleration_multiplier * moving_direction * angular_acceleration * delta_time;
}

if (input.brake) {
sphere.angular_velocity = glm::vec3{0.0f};
}

if (input.flip_gravity) {
if (sphere.flip_gravity_lapsed >= delta_time * 8) {
sphere.gravity = sphere.gravity * float(-1);
}
sphere.flip_gravity_lapsed = 0;
}

sphere.flip_gravity_lapsed += delta_time;
}
}

sphere.velocity *= std::pow(0.5f, delta_time);
sphere.angular_velocity *= std::pow(0.2f, delta_time);
Expand Down
2 changes: 2 additions & 0 deletions src/GameX/physics/rigid_body.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ struct RigidBody {
glm::vec3 gravity{0.0f, -9.8f, 0.0f};
float friction{0.0f};
float elasticity{0.0f};

float flip_gravity_lapsed{0.0f};
};
} // namespace GameX::Physics