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] fix endless falling down with reset and quit, add jump and add balls #62

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
8 changes: 7 additions & 1 deletion src/GameBall/core/game_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ void GameBall::OnInit() {
auto enemy_player = world->CreatePlayer();
auto primary_unit = world->CreateUnit<Logic::Units::RegularBall>(
primary_player->PlayerId(), glm::vec3{0.0f, 1.0f, 0.0f}, 1.0f, 1.0f);
auto enemy_unit = world->CreateUnit<Logic::Units::RegularBall>(
auto enemy_unit_1 = world->CreateUnit<Logic::Units::RegularBall>(
enemy_player->PlayerId(), glm::vec3{-5.0f, 1.0f, 0.0f}, 1.0f, 1.0f);
auto enemy_unit_2 = world->CreateUnit<Logic::Units::RegularBall>(
enemy_player->PlayerId(), glm::vec3{5.0f, 1.0f, 0.0f}, 1.0f, 1.0f);
auto enemy_unit_3 = world->CreateUnit<Logic::Units::RegularBall>(
enemy_player->PlayerId(), glm::vec3{0.0f, 1.0f, 5.0f}, 1.0f, 1.0f);
auto enemy_unit_4 = world->CreateUnit<Logic::Units::RegularBall>(
enemy_player->PlayerId(), glm::vec3{0.0f, 1.0f, -5.0f}, 1.0f, 1.0f);
auto primary_obstacle = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{0.0f, -10.0f, 0.0f}, std::numeric_limits<float>::infinity(),
false, 20.0f);
Expand Down
3 changes: 3 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_.jump = (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS);
input_.quit = (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS);
input_.reset = (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS);
auto camera_controller = app_->CameraController();
auto pitch_yaw = camera_controller->GetPitchYaw();
auto pitch = pitch_yaw.x;
Expand Down
3 changes: 3 additions & 0 deletions src/GameBall/logic/player_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ struct PlayerInput {
bool move_left{false};
bool move_right{false};
bool brake{false};
bool jump{false};
bool quit{false};
bool reset{false};
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 @@ -17,6 +17,7 @@ RegularBall::RegularBall(World *world,
sphere_id_ = physics_world->CreateSphere();
auto &sphere = physics_world->GetSphere(sphere_id_);
sphere.position = position_;
sphere.ini_position = position_;
sphere.SetRadiusMass(radius_, mass_);
sphere.orientation = orientation_;
sphere.velocity = velocity_;
Expand Down Expand Up @@ -45,43 +46,56 @@ 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 (input.jump) {
if (sphere.position.y <= 1 && sphere.position.y >= 0) {
sphere.velocity += glm::vec3{0.0f, 7.0f, 0.0f};
}
}
if (input.quit) {
exit(0);
}
if (input.reset) {
sphere.position = sphere.ini_position + glm::vec3{0.0f, 5.0f, 0.0f};
sphere.velocity = glm::vec3{0.0f, 0.0f, 0.0f};
sphere.angular_velocity = glm::vec3{0.0f, 0.0f, 0.0f};
sphere.orientation = glm::mat3{1.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 = glm::vec3{0.0f};
}
}
}

sphere.velocity *= std::pow(0.5f, delta_time);
sphere.angular_velocity *= std::pow(0.2f, delta_time);
Expand Down
7 changes: 7 additions & 0 deletions src/GameX/physics/rigid_body.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#include "GameX/physics/rigid_body.h"


namespace GameX::Physics {

void RigidBody::Update(float delta_time) {
position += velocity * delta_time;
if (position.y < -10.0) {
position = ini_position + glm::vec3{0.0f, 5.0f, 0.0f};
velocity = glm::vec3{0.0f, 0.0f, 0.0f};
angular_velocity = glm::vec3{0.0f, 0.0f, 0.0f};
orientation = glm::mat3{1.0f};
}
orientation = Base::Rotate(angular_velocity * delta_time) * orientation;
}

Expand Down
1 change: 1 addition & 0 deletions src/GameX/physics/rigid_body.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct RigidBody {
glm::mat3 inertia{1.0f}; // inertia tensor without orientation
glm::mat3 inertia_inv{1.0f}; // inverse inertia tensor without orientation
glm::vec3 position{0.0f};
glm::vec3 ini_position{0.0f};
glm::vec3 velocity{0.0f};
glm::vec3 angular_velocity{0.0f};
glm::mat3 orientation{1.0f};
Expand Down
Loading