Skip to content

Commit

Permalink
fix(physics): add missing components to complex physics sample and ad…
Browse files Browse the repository at this point in the history
…d more general physicsPrepareTag
  • Loading branch information
fallenatlas committed Aug 13, 2024
1 parent 60c1789 commit 1346bc1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
1 change: 1 addition & 0 deletions engine/include/cubos/engine/physics/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace cubos::engine

/// @brief Tag which should be used on all systems that modify velocity or apply forces or impulses.
CUBOS_ENGINE_API extern Tag physicsApplyForcesTag;
extern Tag physicsPrepareTag;

/// @brief Plugin entry function.
/// @param cubos @b Cubos main class
Expand Down
35 changes: 25 additions & 10 deletions engine/samples/complex_physics/assets/scenes/red_cube.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
"z": 0.0
},
"cubos::engine::BoxCollisionShape": {
"x": 0.5,
"y": 0.5,
"z": 0.5
"box" : {
"x": 0.5,
"y": 0.5,
"z": 0.5
},
"changed" : true
},
"cubos::engine::Collider": {
"a": {
Expand Down Expand Up @@ -38,18 +41,25 @@
}
},
"cubos::engine::Force": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"cubos::engine::Torque": {
},
"cubos::engine::Impulse": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"cubos::engine::AngularImpulse": {
},
"cubos::engine::Mass": {
"inverseMass": 0.02,
"mass": 50.0
"mass": 50.0,
"changed": true
},
"cubos::engine::Inertia": {
"autoUpdate": true
},
"cubos::engine::CenterOfMass": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"cubos::engine::PhysicsMaterial": {
"friction": 0.02,
Expand Down Expand Up @@ -81,6 +91,11 @@
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"cubos::engine::AngularVelocity": {
"x": 0.0,
"y": 0.0,
"z": 0.0
}
}
},
Expand Down
35 changes: 25 additions & 10 deletions engine/samples/complex_physics/assets/scenes/white_cube.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
"z": 0.0
},
"cubos::engine::BoxCollisionShape": {
"x": 0.5,
"y": 0.5,
"z": 0.5
"box" : {
"x": 0.5,
"y": 0.5,
"z": 0.5
},
"changed" : true
},
"cubos::engine::Collider": {
"a": {
Expand Down Expand Up @@ -38,18 +41,25 @@
}
},
"cubos::engine::Force": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"cubos::engine::Torque": {
},
"cubos::engine::Impulse": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"cubos::engine::AngularImpulse": {
},
"cubos::engine::Mass": {
"inverseMass": 0.1,
"mass": 10.0
"mass": 10.0,
"changed": true
},
"cubos::engine::Inertia": {
"autoUpdate": true
},
"cubos::engine::CenterOfMass": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"cubos::engine::PhysicsMaterial": {
"friction": 0.02,
Expand Down Expand Up @@ -81,6 +91,11 @@
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"cubos::engine::AngularVelocity": {
"x": 0.0,
"y": 0.0,
"z": 0.0
}
}
},
Expand Down
13 changes: 12 additions & 1 deletion engine/src/physics/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <cubos/core/reflection/traits/enum.hpp>
#include <cubos/core/reflection/type.hpp>

#include <cubos/engine/collisions/plugin.hpp>
#include <cubos/engine/collisions/shapes/box.hpp>
#include <cubos/engine/fixed_step/plugin.hpp>
#include <cubos/engine/physics/plugin.hpp>
#include <cubos/engine/physics/solver/plugin.hpp>

Expand Down Expand Up @@ -41,6 +43,7 @@ CUBOS_REFLECT_IMPL(Inertia)
return cubos::core::ecs::TypeBuilder<Inertia>("cubos::engine::Inertia")
.withField("inertia", &Inertia::inertia)
.withField("inverseInertia", &Inertia::inverseInertia)
.withField("autoUpdate", &Inertia::autoUpdate)
.build();
}

Expand Down Expand Up @@ -118,6 +121,8 @@ CUBOS_REFLECT_IMPL(Damping)
return core::ecs::TypeBuilder<Damping>("cubos::engine::Damping").build();
}

CUBOS_DEFINE_TAG(cubos::engine::physicsPrepareTag);

// Compute Inertia Tensor for box shape
static glm::mat3 boxInertiaTensor(float mass, glm::vec3 dimensions)
{
Expand All @@ -132,8 +137,13 @@ static glm::mat3 boxInertiaTensor(float mass, glm::vec3 dimensions)

void cubos::engine::physicsPlugin(Cubos& cubos)
{
cubos.depends(fixedStepPlugin);
cubos.depends(collisionsPlugin);

cubos.plugin(physicsFixedSubstepPlugin);

cubos.tag(physicsPrepareTag).after(collisionsTag).tagged(fixedStepTag);

cubos.resource<Damping>();

cubos.component<Velocity>();
Expand All @@ -144,6 +154,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
cubos.component<AngularImpulse>();
cubos.component<Mass>();
cubos.component<Inertia>();
cubos.component<CenterOfMass>();
cubos.component<AccumulatedCorrection>();
cubos.component<PhysicsMaterial>();
cubos.component<PhysicsBundle>();
Expand Down Expand Up @@ -187,7 +198,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)

// Should this be here?
cubos.system("update inertia - box collider")
.tagged(physicsPrepareSolveTag)
.tagged(physicsPrepareTag)
.call([](Query<Mass&, BoxCollisionShape&, Inertia&> query) {
for (auto [mass, shape, inertia] : query)
{
Expand Down
2 changes: 1 addition & 1 deletion engine/src/physics/solver/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void cubos::engine::physicsSolverPlugin(Cubos& cubos)

// doesn't need to be after collisions but for now it should stay in case we do velocity based prediction for
// collisions in the future
cubos.tag(physicsPrepareSolveTag).after(collisionsTag).tagged(fixedStepTag);
cubos.tag(physicsPrepareSolveTag).tagged(physicsPrepareTag);

cubos.tag(physicsIntegrateVelocityTag)
.after(physicsPrepareSolveTag)
Expand Down

0 comments on commit 1346bc1

Please sign in to comment.