Skip to content

Commit

Permalink
Physics world created
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyJazz committed Dec 30, 2023
1 parent 8644a78 commit b3ff09f
Show file tree
Hide file tree
Showing 22 changed files with 587 additions and 120 deletions.
4 changes: 2 additions & 2 deletions src/GameBall/core/actors/common_ball_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ void CommonBallActor::SetInertiaTensor(const glm::mat3 &inertia_tensor) {
J_inv_ = glm::inverse(inertia_tensor);
}

void CommonBallActor::SetWeight(float weight) {
weight_ = weight;
void CommonBallActor::SetMass(float mass) {
mass_ = mass;
}

void CommonBallActor::SetGravity(const glm::vec3 &gravity) {
Expand Down
4 changes: 2 additions & 2 deletions src/GameBall/core/actors/common_ball_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CommonBallActor : public Actor {

void SetInertiaTensor(const glm::mat3 &inertia_tensor);

void SetWeight(float weight);
void SetMass(float mass);

void SetGravity(const glm::vec3 &gravity);

Expand All @@ -38,6 +38,6 @@ class CommonBallActor : public Actor {
glm::mat3 J_inv_{1.0f};

glm::vec3 gravity_{};
float weight_{};
float mass_{};
};
} // namespace GameBall::Actors
2 changes: 1 addition & 1 deletion src/GameBall/core/actors/common_block_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void CommonBlockActor::SetInertiaTensor(const glm::mat3 &inertia_tensor) {
J_inv_ = glm::inverse(inertia_tensor);
}

void CommonBlockActor::SetWeight(float weight) {
void CommonBlockActor::SetMass(float weight) {
weight_ = weight;
}

Expand Down
2 changes: 1 addition & 1 deletion src/GameBall/core/actors/common_block_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CommonBlockActor : public Actor {

void SetInertiaTensor(const glm::mat3 &inertia_tensor);

void SetWeight(float weight);
void SetMass(float weight);

void SetGravity(const glm::vec3 &gravity);

Expand Down
9 changes: 6 additions & 3 deletions src/GameBall/core/game_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ void GameBall::OnInit() {

auto primary_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,
1.0f);
primary_player->PlayerId(), glm::vec3{0.0f, 1.0f, 0.0f}, 1.0f, 1.0f);
auto primary_obstacle = world->CreateObstacle<Logic::Obstacles::Block>(
glm::vec3{0.0f, -5.0f, 0.0f}, 1.0f, false, glm::vec3{10.0f});
glm::vec3{0.0f, -10.0f, 0.0f}, std::numeric_limits<float>::infinity(),
false, 20.0f);

primary_unit->SetMotion(glm::vec3{0.0f, 1.0f, 0.0f}, glm::vec3{0.0f},
glm::mat3{1.0f}, glm::vec3{0.0f, 0.0f, 1.0f});

primary_player_id_ = primary_player->PlayerId();

Expand Down
105 changes: 48 additions & 57 deletions src/GameBall/logic/obstacles/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,96 +4,87 @@
#include "GameBall/logic/world.h"

namespace GameBall::Logic::Obstacles {
Block::Block(World *world,
const glm::mat4 &transform,
float weight,
bool gravity)
: Block(world,
glm::vec3(transform[3]),
weight,
gravity,
glm::mat3(transform)) {
}

Block::Block(World *world,
glm::vec3 position,
float weight,
float mass,
bool gravity,
glm::vec3 scale)
: Block(world,
position,
weight,
gravity,
glm::mat3{scale.x, 0.0f, 0.0f, 0.0f, scale.y, 0.0f, 0.0f, 0.0f,
scale.z}) {
}

Block::Block(World *world,
glm::vec3 position,
float weight,
bool gravity,
const glm::mat3 &transform)
float side_length)
: Obstacle(world),
position_(position),
transform_(transform),
side_length_(side_length),
gravity_(gravity ? glm::vec3{0.0f, -9.8f, 0.0f} : glm::vec3{0.0}),
weight_(weight) {
mass_(mass) {
auto physics_world = world_->PhysicsWorld();
cude_id_ = physics_world->CreateCube();
auto &cube = physics_world->GetCube(cude_id_);
SetGravity(gravity_);
SetMass(mass_);
SetSideLength(side_length_);
SetMotion(position_, velocity_, orientation_, L_);
cube.elasticity = 0.0f;
cube.friction = 0.5f;
}

SYNC_ACTOR_FUNC(Block) {
actor->SetTransform(transform_);
actor->SetWeight(weight_);
actor->SetTransform(glm::mat3{side_length_});
actor->SetMass(mass_);
actor->SetInertiaTensor(J_);
actor->SetGravity(gravity_);
actor->SetMotion(position_, velocity_, rotation_, L_);
actor->SetMotion(position_, velocity_, orientation_, L_);
if (ActorInitialize()) {
actor->Entity()->SetAlbedoImage(app->AssetManager()->ImageFile(
"textures/floor_tiles_06_2k/floor_tiles_06_diff_2k.jpg"));
}
}

void Block::SetMomentOfInertia(float moment_of_inertia) {
J_ = glm::mat3{moment_of_inertia};
}

void Block::SetInertiaTensor(const glm::mat3 &inertia_tensor) {
J_ = inertia_tensor;
}

void Block::SetWeight(float weight) {
weight_ = weight;
void Block::SetMass(float mass) {
auto physics_world = world_->PhysicsWorld();
auto &cube = physics_world->GetCube(cude_id_);
mass_ = mass;
cube.SetSideLengthMass(side_length_, mass_);
J_ = cube.inertia;
}

void Block::SetGravity(const glm::vec3 &gravity) {
auto physics_world = world_->PhysicsWorld();
auto &cube = physics_world->GetCube(cude_id_);
gravity_ = gravity;
}

void Block::SetTransform(const glm::mat3 &transform) {
transform_ = transform;
cube.gravity = gravity_;
}

void Block::SetMotion(const glm::vec3 &position,
const glm::vec3 &velocity,
const glm::mat3 &rotation,
const glm::mat3 &orientation,
const glm::vec3 &angular_momentum) {
rotation_ = rotation;
auto physics_world = world_->PhysicsWorld();
auto &cube = physics_world->GetCube(cude_id_);

orientation_ = orientation;
position_ = position;
velocity_ = velocity;
L_ = angular_momentum;

cube.position = position_;
cube.velocity = velocity_;
cube.orientation = orientation_;
cube.angular_velocity = cube.inertia_inv * L_;
}

void Block::UpdateTick() {
auto delta_time = world_->TickDeltaT();
glm::vec3 acceleration = gravity_;
velocity_ += acceleration * delta_time;
position_ += velocity_ * delta_time;

glm::vec3 omega = rotation_ * glm::inverse(J_) * (L_ * rotation_);
auto physics_world = world_->PhysicsWorld();
auto &cube = physics_world->GetCube(cude_id_);
position_ = cube.position;
velocity_ = cube.velocity;
orientation_ = cube.orientation;
L_ = cube.inertia * cube.angular_velocity;
}

float theta = glm::length(omega);
if (theta > 0.0f) {
rotation_ =
glm::mat3{glm::rotate(glm::mat4{1.0f}, theta, omega)} * rotation_;
}
void Block::SetSideLength(float side_length) {
side_length_ = side_length;
auto physics_world = world_->PhysicsWorld();
auto &cube = physics_world->GetCube(cude_id_);
cube.SetSideLengthMass(side_length_, mass_);
J_ = cube.inertia;
}
} // namespace GameBall::Logic::Obstacles
33 changes: 10 additions & 23 deletions src/GameBall/logic/obstacles/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,37 @@ class Block : public Obstacle {
public:
Block(World *world,
glm::vec3 position,
float weight = std::numeric_limits<float>::infinity(),
float mass = std::numeric_limits<float>::infinity(),
bool gravity = false,
const glm::mat3 &transform = glm::mat3(1.0f));

Block(World *world,
glm::vec3 position,
float weight = std::numeric_limits<float>::infinity(),
bool gravity = false,
glm::vec3 scale = glm::vec3(1.0f, 1.0f, 1.0f));

Block(World *world,
const glm::mat4 &transform,
float weight = std::numeric_limits<float>::infinity(),
bool gravity = false);
float side_length = 1.0f);

SYNC_ACTOR_FUNC_DECLARE(Actors::CommonBlockActor)

void SetMomentOfInertia(float moment_of_inertia);

void SetInertiaTensor(const glm::mat3 &inertia_tensor);

void SetWeight(float weight);
void SetMass(float mass);

void SetGravity(const glm::vec3 &gravity);

void SetTransform(const glm::mat3 &transform);
void SetSideLength(float side_length);

void SetMotion(const glm::vec3 &position,
const glm::vec3 &velocity,
const glm::mat3 &rotation,
const glm::mat3 &orientation,
const glm::vec3 &angular_momentum);

void UpdateTick() override;

private:
glm::mat3 transform_{};
float side_length_{};
glm::vec3 position_{};
glm::vec3 velocity_{};

glm::mat3 rotation_{1.0f};
glm::mat3 orientation_{1.0f};
glm::vec3 L_{0.0f}; // angular momentum
glm::mat3 J_{1.0f}; // inertia tensor

glm::vec3 gravity_{};
float weight_{};
float mass_{};

uint64_t cude_id_{};
};
} // namespace GameBall::Logic::Obstacles
83 changes: 62 additions & 21 deletions src/GameBall/logic/units/regular_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,82 @@ namespace GameBall::Logic::Units {
RegularBall::RegularBall(World *world,
uint64_t player_id,
const glm::vec3 &position,
float scale,
float weight,
float J)
float radius,
float mass)
: Unit(world, player_id) {
scale_ = scale;
weight_ = weight;
radius_ = radius;
mass_ = mass;
position_ = position;
L_ = glm::vec3{0.0f};
J_ = J;
auto physics_world = world_->PhysicsWorld();
sphere_id_ = physics_world->CreateSphere();
auto &sphere = physics_world->GetSphere(sphere_id_);
sphere.position = position_;
sphere.SetRadiusMass(radius_, mass_);
sphere.orientation = orientation_;
sphere.velocity = velocity_;
sphere.angular_velocity = glm::vec3{0.0f};
sphere.elasticity = 0.5f;
sphere.friction = 0.5f;
sphere.gravity = glm::vec3{0.0f, -9.8f, 0.0f};
}

RegularBall::~RegularBall() {
;
}

SYNC_ACTOR_FUNC(RegularBall) {
actor->SetWeight(1.0f);
auto physics_world = world_->PhysicsWorld();
auto &sphere = physics_world->GetSphere(sphere_id_);
actor->SetMass(1.0f);
actor->SetGravity(glm::vec3{0.0f, -9.8f, 0.0f});
actor->SetTransform(glm::mat3{scale_});
actor->SetMotion(position_, velocity_, rotation_, L_);
actor->SetMomentOfInertia(J_);
actor->SetTransform(glm::mat3{radius_});
actor->SetMotion(position_, velocity_, orientation_, augular_momentum_);
actor->SetMomentOfInertia(sphere.inertia[0][0]);
}

void RegularBall::UpdateTick() {
auto delta_time = world_->TickDeltaT();
glm::vec3 acceleration = glm::vec3{0.0f, -9.8f, 0.0f};
velocity_ += acceleration * delta_time;
position_ += velocity_ * delta_time;
auto physics_world = world_->PhysicsWorld();
auto &sphere = physics_world->GetSphere(sphere_id_);
position_ = sphere.position;
velocity_ = sphere.velocity;
orientation_ = sphere.orientation;
augular_momentum_ = sphere.inertia * sphere.angular_velocity;
}

void RegularBall::SetMass(float mass) {
auto physics_world = world_->PhysicsWorld();
auto &sphere = physics_world->GetSphere(sphere_id_);
sphere.SetRadiusMass(radius_, mass);
mass_ = mass;
}

void RegularBall::SetGravity(const glm::vec3 &gravity) {
auto physics_world = world_->PhysicsWorld();
auto &sphere = physics_world->GetSphere(sphere_id_);
sphere.gravity = gravity;
}

glm::vec3 omega = L_ / J_;
void RegularBall::SetRadius(float radius) {
auto physics_world = world_->PhysicsWorld();
auto &sphere = physics_world->GetSphere(sphere_id_);
sphere.SetRadiusMass(radius, mass_);
radius_ = radius;
}

float theta = glm::length(omega) * delta_time;
if (theta > 0.0f) {
rotation_ =
glm::mat3{glm::rotate(glm::mat4{1.0f}, theta, omega)} * rotation_;
}
void RegularBall::SetMotion(const glm::vec3 &position,
const glm::vec3 &velocity,
const glm::mat3 &orientation,
const glm::vec3 &angular_momentum) {
auto physics_world = world_->PhysicsWorld();
auto &sphere = physics_world->GetSphere(sphere_id_);
sphere.position = position;
sphere.velocity = velocity;
sphere.orientation = orientation;
sphere.angular_velocity = sphere.inertia_inv * angular_momentum;
position_ = position;
velocity_ = velocity;
orientation_ = orientation;
augular_momentum_ = angular_momentum;
}

} // namespace GameBall::Logic::Units
Loading

0 comments on commit b3ff09f

Please sign in to comment.