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

[homework]Update regular_ball.cpp #55

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Binary file added BANG.mp3
Binary file not shown.
74 changes: 37 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,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);
Expand Down
7 changes: 4 additions & 3 deletions src/GameX/physics/collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bool DetectCollision(const Sphere &sphere,
return true;
}

bool SolveCollision(RigidBody &body1,
uint64_t SolveCollision(RigidBody &body1,
RigidBody &body2,
const Collision &collision) {
// std::cout << "Solve Collision..." << std::endl;
Expand Down Expand Up @@ -103,8 +103,9 @@ bool SolveCollision(RigidBody &body1,
body2.velocity + glm::cross(body2.angular_velocity, r2) - body1.velocity -
glm::cross(body1.angular_velocity, r1);
float velocity_along_normal = glm::dot(relative_velocity, collision.normal);
uint64_t addscore=10000*velocity_along_normal;
if (velocity_along_normal > -0.0001f) {
return false;
return 0;
}

glm::mat3 inverse_inertia1 =
Expand Down Expand Up @@ -165,7 +166,7 @@ bool SolveCollision(RigidBody &body1,
inverse_inertia2 * glm::cross(r2, friction_impulse);
}

return true;
return addscore;
}

} // namespace GameX::Physics
2 changes: 1 addition & 1 deletion src/GameX/physics/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bool DetectCollision(const Sphere &sphere,
const Cube &cube,
Collision &collision);

bool SolveCollision(RigidBody &body1,
uint64_t SolveCollision(RigidBody &body1,
RigidBody &body2,
const Collision &collision);

Expand Down
9 changes: 7 additions & 2 deletions src/GameX/physics/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ void World::SolveCollisions() {
do {
collision_found = false;
for (auto &pair : collision_pairs) {
if (SolveCollision(*std::get<0>(pair), *std::get<1>(pair),
std::get<2>(pair))) {
uint64_t k = SolveCollision(*std::get<0>(pair), *std::get<1>(pair),
std::get<2>(pair));
score_ += k;
if(k!=0){
collision_found = true;
}
}
Expand All @@ -88,4 +90,7 @@ void World::ApplyGravity(float delta_time) {
cube.second.velocity += cube.second.gravity * delta_time;
}
}
void World::GetScore(){
printf("%lu",score_);
}
} // namespace GameX::Physics
2 changes: 2 additions & 0 deletions src/GameX/physics/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class World {

void ApplyGravity(float delta_time);

void GetScore();
private:
std::map<uint64_t, Sphere> spheres_;
uint64_t next_sphere_id_{1};
std::map<uint64_t, Cube> cubes_;
uint64_t next_cube_id_{1};
uint64_t score_=0;
};
} // namespace GameX::Physics