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(engine): added resource to easily configure magic numbers in solver #1379

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added anti-aliasing using FXAA technique (#1334, **@kuukitenshi**).
- Point light shadows (#1188, **@tomas7770**).
- Added method to save Settings to file (#1349, **@SrGesus**).
- Added resource to easily configure magic numbers in solver (#1281, **@GCeSilva**).

### Changed

Expand Down
2 changes: 2 additions & 0 deletions engine/include/cubos/engine/physics/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <cubos/engine/physics/components/velocity.hpp>
#include <cubos/engine/physics/physics_bundle.hpp>
#include <cubos/engine/physics/resources/damping.hpp>
#include <cubos/engine/physics/resources/solver_constants.hpp>
#include <cubos/engine/prelude.hpp>

namespace cubos::engine
Expand All @@ -37,6 +38,7 @@ namespace cubos::engine
/// ## Resources
/// - @ref Damping - holds the damping value for integration.
/// - @ref Substeps - holds the amount of substeps for the physics update.
/// - @ref SolverConstants - holds the constants for the solver.
///
/// ## Components
/// - @ref PhysicsBundle - bundle that holds the physics information to give to a new entity.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/// @file
/// @brief Resource @ref cubos::engine::SolverConstants.
/// @ingroup physics-plugin

#pragma once

#include <glm/glm.hpp>

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

#include <cubos/engine/api.hpp>

namespace cubos::engine
{
/// @brief Resource which allows configuration over constants in the solver plugin.
/// @ingroup physics-plugin
struct SolverConstants
{
CUBOS_REFLECT;

/// @brief Minimum inverse mass for a body to be considered as imovable (as if it had infinite mass).
float minInvMass = 0.0F;

/// @brief Minimum inverse inertia for a body to rotate.
glm::mat3 minInvInertia = glm::mat3(0.0F);

/// @brief The maximum "boost" for the initial solving of the constraint.
float maxBias = -4.0F;

/// @brief The minimum update frequency of the collision solving.
float minContactHertz = 30.0F;

/// @brief The minimum value for KNormal (to avoid divisions by zero).
float minKNormal = 0.0F;

/// @brief The minimum value for KFriction (to avoid divisions by zero).
float minKFriction = 0.0F;

/// @brief The minimum length of the tangent vector in the direction of the relative velocity (if it's below this we recalculate a basis for the tangents).
float minTangentLenSq = 1e-6F * 1e-6F;

/// @brief The minimum restitution value to calculate restitution for a body (restitution is bounciness).
float minRestitution = 0.0F;

/// @brief The minimum speed in the normal direction to calculate restitution for a body (lower is closer to elastic collisions).
float minNormalSpeed = -0.01F;

/// @brief The minimum impulse being applied in the normal direction to calculate restitution for a body (to avoid applying restitution when body position didn't change).
float minNormalImpulse = 0.0F;
};

} // namespace cubos::engine
17 changes: 17 additions & 0 deletions engine/src/physics/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ CUBOS_REFLECT_IMPL(Damping)
return core::ecs::TypeBuilder<Damping>("cubos::engine::Damping").build();
}

CUBOS_REFLECT_IMPL(SolverConstants)
{
return core::ecs::TypeBuilder<SolverConstants>("cubos::engine::SolverConstants")
.withField("minInvMass", &SolverConstants::minInvMass)
.withField("minInvInertia", &SolverConstants::minInvInertia)
.withField("maxBias", &SolverConstants::maxBias)
.withField("minContactHertz", &SolverConstants::minContactHertz)
.withField("minKNormal", &SolverConstants::minKNormal)
.withField("minKFriction", &SolverConstants::minKFriction)
.withField("minTangentLenSq", &SolverConstants::minTangentLenSq)
.withField("minRestitution", &SolverConstants::minRestitution)
.withField("minNormalSpeed", &SolverConstants::minNormalSpeed)
.withField("minNormalImpulse", &SolverConstants::minNormalImpulse)
.build();
}

CUBOS_DEFINE_TAG(cubos::engine::physicsPrepareTag);

// Compute Inertia Tensor for box shape
Expand All @@ -146,6 +162,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
cubos.tag(physicsPrepareTag).after(collisionsTag).tagged(fixedStepTag);

cubos.resource<Damping>();
cubos.resource<SolverConstants>();

cubos.component<Velocity>();
cubos.component<AngularVelocity>();
Expand Down
19 changes: 11 additions & 8 deletions engine/src/physics/solver/integration/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ void cubos::engine::physicsIntegrationPlugin(Cubos& cubos)
.call([](Query<Velocity&, AngularVelocity&, const Force&, const Torque&, const Mass&, const Inertia&,
const Rotation&>
query,
const Damping& damping, const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps) {
const Damping& damping, const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps,
const SolverConstants& solverConstants) {
float subDeltaTime = fixedDeltaTime.value / (float)substeps.value;

for (auto [velocity, angVelocity, force, torque, mass, inertia, rotation] : query)
{
// Linear velocity
if (mass.inverseMass <= 0.0F)
if (mass.inverseMass <= solverConstants.minInvMass)
{
continue;
}
Expand All @@ -74,7 +75,7 @@ void cubos::engine::physicsIntegrationPlugin(Cubos& cubos)
velocity.vec += deltaLinearVelocity;

// Angular velocity
if (inertia.inverseInertia == glm::mat3(0.0F))
if (inertia.inverseInertia == solverConstants.minInvInertia)
{
continue;
}
Expand All @@ -97,21 +98,22 @@ void cubos::engine::physicsIntegrationPlugin(Cubos& cubos)
.call([](Query<AccumulatedCorrection&, Rotation&, const Velocity&, const AngularVelocity&, const Mass&,
const Inertia&>
query,
const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps) {
const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps,
const SolverConstants& solverConstants) {
float subDeltaTime = fixedDeltaTime.value / (float)substeps.value;

for (auto [correction, rotation, velocity, angVelocity, mass, inertia] : query)
{
// Position
if (mass.inverseMass <= 0.0F)
if (mass.inverseMass <= solverConstants.minInvMass)
{
continue;
}

correction.position += velocity.vec * subDeltaTime;

// Rotation
if (inertia.inverseInertia == glm::mat3(0.0F))
if (inertia.inverseInertia == solverConstants.minInvInertia)
{
continue;
}
Expand All @@ -124,10 +126,11 @@ void cubos::engine::physicsIntegrationPlugin(Cubos& cubos)

cubos.system("finalize position")
.tagged(physicsFinalizePositionTag)
.call([](Query<Position&, AccumulatedCorrection&, const Mass&> query) {
.call([](Query<Position&, AccumulatedCorrection&, const Mass&> query,
const SolverConstants& solverConstants) {
for (auto [position, correction, mass] : query)
{
if (mass.inverseMass <= 0.0F)
if (mass.inverseMass <= solverConstants.minInvMass)
{
continue;
}
Expand Down
48 changes: 28 additions & 20 deletions engine/src/physics/solver/penetration_constraint/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ static void getPlaneSpace(const glm::vec3& n, glm::vec3& tangent1, glm::vec3& ta
}

static void getTangents(const glm::vec3& velocity1, const glm::vec3& velocity2, const glm::vec3& n, glm::vec3& tangent1,
glm::vec3& tangent2)
glm::vec3& tangent2, const SolverConstants& solverConstants)
{
// Use linear relative velocity for determining tangents.
glm::vec3 linearVr = velocity2 - velocity1;

tangent1 = linearVr - glm::dot(linearVr, n) * n;
float tangentLenSq = glm::length2(tangent1);
if (tangentLenSq > 1e-6 * 1e-6) /// TODO: check this
if (tangentLenSq > solverConstants.minTangentLenSq) /// TODO: check this
{
tangent1 = glm::normalize(tangent1);
tangent2 = glm::cross(n, tangent1);
Expand Down Expand Up @@ -82,7 +82,8 @@ static void solvePenetrationConstraint(
PenetrationConstraint&, Entity, const Mass&, const Inertia&, const Rotation&, AccumulatedCorrection&,
Velocity&, AngularVelocity&>
query,
const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps, const bool useBias)
const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps, const SolverConstants& solverConstants,
const bool useBias)
{
for (auto [ent1, mass1, inertia1, rotation1, correction1, velocity1, angVelocity1, constraint, ent2, mass2,
inertia2, rotation2, correction2, velocity2, angVelocity2] : query)
Expand Down Expand Up @@ -120,7 +121,7 @@ static void solvePenetrationConstraint(
}
else if (useBias)
{
bias = glm::max(constraint.biasCoefficient * separation, -4.0F);
bias = glm::max(constraint.biasCoefficient * separation, solverConstants.maxBias);
massScale = constraint.massCoefficient;
impulseScale = constraint.impulseCoefficient;
}
Expand Down Expand Up @@ -161,11 +162,11 @@ static void solvePenetrationConstraint(
glm::vec3 tangent2;
if (ent1 != constraint.entity)
{
getTangents(v2, v1, constraint.normal, tangent1, tangent2);
getTangents(v2, v1, constraint.normal, tangent1, tangent2, solverConstants);
}
else
{
getTangents(v1, v2, constraint.normal, tangent1, tangent2);
getTangents(v1, v2, constraint.normal, tangent1, tangent2, solverConstants);
}

// Friction
Expand Down Expand Up @@ -242,8 +243,10 @@ void cubos::engine::penetrationConstraintPlugin(Cubos& cubos)
AngularVelocity&, PenetrationConstraint&, Entity, const Mass&, const Inertia&, const Rotation&,
AccumulatedCorrection&, Velocity&, AngularVelocity&>
query,
const FixedDeltaTime& fixedDeltaTime,
const Substeps& substeps) { solvePenetrationConstraint(query, fixedDeltaTime, substeps, true); });
const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps,
const SolverConstants& solverConstants) {
solvePenetrationConstraint(query, fixedDeltaTime, substeps, solverConstants, true);
});

cubos.system("solve contacts no bias")
.tagged(penetrationConstraintSolveRelaxTag)
Expand All @@ -252,8 +255,10 @@ void cubos::engine::penetrationConstraintPlugin(Cubos& cubos)
AngularVelocity&, PenetrationConstraint&, Entity, const Mass&, const Inertia&, const Rotation&,
AccumulatedCorrection&, Velocity&, AngularVelocity&>
query,
const FixedDeltaTime& fixedDeltaTime,
const Substeps& substeps) { solvePenetrationConstraint(query, fixedDeltaTime, substeps, false); });
const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps,
const SolverConstants& solverConstants) {
solvePenetrationConstraint(query, fixedDeltaTime, substeps, solverConstants, false);
});

cubos.system("add restitution")
.tagged(penetrationConstraintRestitutionTag)
Expand All @@ -263,11 +268,12 @@ void cubos::engine::penetrationConstraintPlugin(Cubos& cubos)
.call([](Query<Entity, const Mass&, const Inertia&, AccumulatedCorrection&, Velocity&, AngularVelocity&,
PenetrationConstraint&, Entity, const Mass&, const Inertia&, AccumulatedCorrection&, Velocity&,
AngularVelocity&>
query) {
query,
const SolverConstants& solverConstants) {
for (auto [ent1, mass1, inertia1, correction1, velocity1, angVelocity1, constraint, ent2, mass2, inertia2,
correction2, velocity2, angVelocity2] : query)
{
if (constraint.restitution == 0.0F)
if (constraint.restitution == solverConstants.minRestitution)
{
continue;
}
Expand All @@ -279,7 +285,8 @@ void cubos::engine::penetrationConstraintPlugin(Cubos& cubos)

for (auto point : constraint.points)
{
if (point.normalSpeed > -0.01F || point.normalImpulse == 0.0F)
if (point.normalSpeed > solverConstants.minNormalSpeed ||
point.normalImpulse == solverConstants.minNormalImpulse)
{
continue;
}
Expand Down Expand Up @@ -330,9 +337,10 @@ void cubos::engine::penetrationConstraintPlugin(Cubos& cubos)
const Inertia&, const CenterOfMass&, const Rotation&, const Velocity&, const AngularVelocity&,
const PhysicsMaterial&>
query,
const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps) {
const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps,
const SolverConstants& solverConstants) {
float subDeltaTime = fixedDeltaTime.value / (float)substeps.value;
float contactHertz = glm::min(30.0F, 0.25F * (1.0F / subDeltaTime));
float contactHertz = glm::min(solverConstants.minContactHertz, 0.25F * (1.0F / subDeltaTime));

for (auto [ent1, mass1, inertia1, centerOfMass1, rotation1, velocity1, angVelocity1, material1, manifold,
ent2, mass2, inertia2, centerOfMass2, rotation2, velocity2, angVelocity2, material2] : query)
Expand All @@ -341,11 +349,11 @@ void cubos::engine::penetrationConstraintPlugin(Cubos& cubos)
glm::vec3 tangent2;
if (ent1 != manifold.entity)
{
getTangents(velocity2.vec, velocity1.vec, manifold.normal, tangent1, tangent2);
getTangents(velocity2.vec, velocity1.vec, manifold.normal, tangent1, tangent2, solverConstants);
}
else
{
getTangents(velocity1.vec, velocity2.vec, manifold.normal, tangent1, tangent2);
getTangents(velocity1.vec, velocity2.vec, manifold.normal, tangent1, tangent2, solverConstants);
}

std::vector<PenetrationConstraintPointData> points;
Expand Down Expand Up @@ -386,7 +394,7 @@ void cubos::engine::penetrationConstraintPlugin(Cubos& cubos)
float kNormal = mass1.inverseMass + mass2.inverseMass +
glm::dot(inertia1.inverseInertia * rn1, rn1) +
glm::dot(inertia2.inverseInertia * rn2, rn2);
pointData.normalMass = kNormal > 0.0F ? 1.0F / kNormal : 0.0F;
pointData.normalMass = kNormal > solverConstants.minKNormal ? 1.0F / kNormal : 0.0F;

// friction mass
glm::vec3 rt11 = glm::cross(r1, tangent1);
Expand All @@ -406,8 +414,8 @@ void cubos::engine::penetrationConstraintPlugin(Cubos& cubos)
mass1.inverseMass + mass2.inverseMass + glm::dot(i1Rt21, rt21) + glm::dot(i2Rt22, rt22);

/// TODO: these could be an array in the point
pointData.frictionMass1 = kFriction1 > 0.0F ? 1.0F / kFriction1 : 0.0F;
pointData.frictionMass2 = kFriction2 > 0.0F ? 1.0F / kFriction2 : 0.0F;
pointData.frictionMass1 = kFriction1 > solverConstants.minKFriction ? 1.0F / kFriction1 : 0.0F;
pointData.frictionMass2 = kFriction2 > solverConstants.minKFriction ? 1.0F / kFriction2 : 0.0F;

points.push_back(pointData);
}
Expand Down
Loading