Skip to content

Commit

Permalink
fix(physics): fix documentation and code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenatlas committed Nov 11, 2023
1 parent 6b7bc78 commit eeadeda
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <glm/glm.hpp>

#include <cubos/core/reflection/reflect.hpp>

namespace cubos::engine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <glm/vec3.hpp>

#include <cubos/core/reflection/reflect.hpp>

namespace cubos::engine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <glm/vec3.hpp>

#include <cubos/core/reflection/reflect.hpp>

namespace cubos::engine
Expand All @@ -15,7 +16,7 @@ namespace cubos::engine
struct [[cubos::component("cubos/physics_velocity", VecStorage)]] PhysicsVelocity
{
CUBOS_REFLECT;

glm::vec3 velocity;
glm::vec3 force;
glm::vec3 impulse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <glm/glm.hpp>

#include <cubos/core/reflection/reflect.hpp>

namespace cubos::engine
Expand Down
38 changes: 11 additions & 27 deletions engine/include/cubos/engine/physics/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@
/// @brief @ref physics-plugin plugin directory.

/// @file
/// @brief Plugin entry point, resources @ref cubos::engine::FixedDeltaTime and @ref cubos::engine::Accumulator and components @ref
/// cubos::engine::PhysicsVelocity and @ref cubos::engine::PhysicsMass.
/// @brief Plugin entry point.
/// @ingroup physics-plugin

#pragma once

#include <cubos/engine/cubos.hpp>
/// [Component Refl]
#include <cubos/core/ecs/component/reflection.hpp>
#include <cubos/core/reflection/external/glm.hpp>
#include <cubos/core/reflection/external/primitives.hpp>

#include <cubos/engine/cubos.hpp>
#include <cubos/engine/physics/components/accumulated_correction.hpp>
#include <cubos/engine/physics/components/physics_mass.hpp>
#include <cubos/engine/physics/components/physics_velocity.hpp>
#include <cubos/engine/physics/components/previous_position.hpp>
#include <cubos/engine/physics/plugins/gravity.hpp>

#include <cubos/engine/physics/resources/damping.hpp>
#include <cubos/engine/physics/resources/fixed_delta_time.hpp>
#include <cubos/engine/physics/resources/substeps.hpp>

#include <cubos/engine/physics/components/physics_velocity.hpp>
#include <cubos/engine/physics/components/physics_mass.hpp>
#include <cubos/engine/physics/components/previous_position.hpp>
#include <cubos/engine/physics/components/accumulated_correction.hpp>

/// [Component Refl]
#include <cubos/core/ecs/component/reflection.hpp>
#include <cubos/core/reflection/external/glm.hpp>
#include <cubos/core/reflection/external/primitives.hpp>

namespace cubos::engine
{
/// @defgroup physics-plugin Physics
Expand All @@ -46,19 +42,7 @@ namespace cubos::engine
/// - @ref PreviousPosition - holds the previous position of the entity in a substep.
/// - @ref AccumulatedCorrection - holds the corrections accumulated from the constraints solving.
///
/// ## Startup tags
/// - do we need any?
///
/// ## Internal Simulation Tags
/// - `cubos.physics.simulation.prepare`
/// - `cubos.physics.simulation.apply_impulses`
/// - `cubos.physics.simulation.substeps.integrate`
/// - `cubos.physics.simulation.substeps.correct_position`
/// - `cubos.physics.simulation.substeps.update_velocity`
/// - `cubos.physics.simulation.clear_forces`
/// - `cubos.physics.simulation.decrease_accumulator`
///
/// ## User Tags
/// ## Tags
/// - `cubos.physics.apply_forces`

/// @brief Plugin entry function.
Expand Down
15 changes: 7 additions & 8 deletions engine/include/cubos/engine/physics/plugins/gravity.hpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
/// @file
/// @brief Gravity plugin, resource @ref cubos::engine::Gravity.
/// @ingroup physics-plugin
/// @ingroup physics-gravity-plugin

#pragma once

#include <cubos/engine/cubos.hpp>

#include <cubos/engine/physics/resources/gravity.hpp>

#include <cubos/engine/physics/components/physics_velocity.hpp>
#include <cubos/engine/physics/components/physics_mass.hpp>
#include <cubos/engine/physics/components/physics_velocity.hpp>
#include <cubos/engine/physics/resources/gravity.hpp>

namespace cubos::engine
{
/// @defgroup physics-plugin Physics
/// @ingroup engine
/// @defgroup physics-gravity-plugin Physics
/// @ingroup physics-plugin
/// @brief Adds gravity to particles.
///
/// ## Resources
/// - @ref Gravity - holds the global value of gravity.

/// @brief Plugin entry function.
/// @param cubos @b CUBOS. main class
/// @ingroup physics-plugin
/// @ingroup physics-gravity-plugin
void gravityPlugin(Cubos& cubos);
} // namespace cubos::engine
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace cubos::engine
/// @ingroup physics-plugin
struct FixedDeltaTime
{
float value = 1.0f/100.0F;
float value = 1.0F / 100.0F;
};
} // namespace cubos::engine
2 changes: 1 addition & 1 deletion engine/include/cubos/engine/physics/resources/gravity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ namespace cubos::engine
/// @ingroup physics-plugin
struct Gravity
{
glm::vec3 value = {0.0f, -9.81f, 0.0f};
glm::vec3 value = {0.0F, -9.81F, 0.0F};
};
} // namespace cubos::engine
42 changes: 23 additions & 19 deletions engine/samples/physics/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include <cubos/core/ecs/system/query.hpp>

#include <cubos/engine/physics/plugin.hpp>
#include <cubos/engine/renderer/directional_light.hpp>
#include <cubos/engine/renderer/plugin.hpp>
#include <cubos/engine/settings/settings.hpp>
#include <cubos/engine/transform/plugin.hpp>
#include <cubos/engine/voxels/plugin.hpp>
#include <cubos/engine/physics/plugin.hpp>
#include <cubos/engine/transform/position.hpp>
#include <cubos/core/ecs/system/query.hpp>
#include <cubos/engine/voxels/plugin.hpp>

using cubos::core::ecs::Commands;
using cubos::core::ecs::Query;
using cubos::core::ecs::Read;
using cubos::core::ecs::Write;
using cubos::core::ecs::Query;

using namespace cubos::engine;

Expand Down Expand Up @@ -61,26 +62,29 @@ static void spawnCarSystem(Commands cmds, Read<Assets> assets)
glm::vec3 offset = glm::vec3(car->size().x, 0.0F, car->size().z) / -2.0F;

// Create the car entity
cmds.create().add(RenderableGrid{CarAsset, offset})
.add(Position{{0.0F, 0.0F, 0.0F}})
.add(PreviousPosition{{0.0F, 0.0F, 0.0F}})
.add(PhysicsVelocity{.velocity = {0.0F, 0.0F, 0.0F},
.force = {0.0F, 0.0F, 0.0F},
.impulse = {0.0F, 0.0F, 0.0F}})
.add(PhysicsMass{.mass = 500.0F, .inverseMass = 1.0F/500.0F})
.add(AccumulatedCorrection{{0.0F, 0.0F, 0.0F}})
.add(LocalToWorld{});
cmds.create()
.add(RenderableGrid{CarAsset, offset})
.add(Position{{0.0F, 0.0F, 0.0F}})
.add(PreviousPosition{{0.0F, 0.0F, 0.0F}})
.add(
PhysicsVelocity{.velocity = {0.0F, 0.0F, 0.0F}, .force = {0.0F, 0.0F, 0.0F}, .impulse = {0.0F, 0.0F, 0.0F}})
.add(PhysicsMass{.mass = 500.0F, .inverseMass = 1.0F / 500.0F})
.add(AccumulatedCorrection{{0.0F, 0.0F, 0.0F}})
.add(LocalToWorld{});
}

static void pushCarSystem(Query<Write<PhysicsVelocity>> query, Write<MaxTime> time, Read<DeltaTime> deltaTime) {
for (auto [entity, velocity] : query) {
if (time->current < time->max) {
static void pushCarSystem(Query<Write<PhysicsVelocity>> query, Write<MaxTime> time, Read<DeltaTime> deltaTime)
{
for (auto [entity, velocity] : query)
{
if (time->current < time->max)
{
if (time->current == 0.0F)
{
velocity->impulse += glm::vec3(0.0F, 5000.0F, 0.0F);
}
velocity->force = velocity->force + glm::vec3(0.0F, 0.0F, -5000.0F);
time->current = time->current + deltaTime->value;
velocity->force += glm::vec3(0.0F, 0.0F, -5000.0F);
time->current += deltaTime->value;
}
}
}
Expand All @@ -100,7 +104,7 @@ int main(int argc, char** argv)
cubos.startupSystem(spawnLightSystem);
cubos.startupSystem(setPaletteSystem).after("cubos.renderer.init");
cubos.startupSystem(spawnCarSystem).after("cubos.settings").after("cubos.assets.bridge");

cubos.system(pushCarSystem).tagged("cubos.physics.apply_forces");

cubos.run();
Expand Down
11 changes: 7 additions & 4 deletions engine/src/cubos/engine/physics/gravity.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <glm/glm.hpp>

#include <cubos/core/ecs/system/query.hpp>

#include <cubos/engine/physics/plugins/gravity.hpp>

#include <glm/glm.hpp>

using cubos::core::ecs::Query;
using cubos::core::ecs::Read;
using cubos::core::ecs::Write;
Expand All @@ -14,12 +14,15 @@ static void applyGravitySystem(Query<Write<PhysicsVelocity>, Read<PhysicsMass>>
{
for (auto [entity, velocity, mass] : query)
{
if (mass->inverseMass <= 0.0f) return;
if (mass->inverseMass <= 0.0F)
{
return;
}

// Apply gravity force
glm::vec3 gravitationForce = mass->mass * gravity->value;

velocity->force = velocity->force + gravitationForce;
velocity->force += gravitationForce;
}
}

Expand Down
Loading

0 comments on commit eeadeda

Please sign in to comment.