vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
solver_math.h
1#pragma once
2
3#include <algorithm>
4
5#include <glm/glm.hpp>
6#include <glm/gtc/constants.hpp>
7
8#include "system/physics/solver/solver.h"
9
10namespace Vkm::Engine::SolverMath {
11
19inline glm::vec3 velocityAt(const PhysicsBody& body, const glm::vec3& r) {
20 return body.linearVelocity + glm::cross(body.angularVelocity, r);
21}
22
31inline void applyImpulse(PhysicsBody& body, const glm::vec3& impulse, const glm::vec3& r, float sign) {
32 body.linearVelocity += impulse * (body.invMass * sign);
33 body.angularVelocity += body.invInertiaWorld * glm::cross(r, impulse * sign);
34}
35
48inline float effectiveMass(
49 const PhysicsBody& a,
50 const PhysicsBody& b,
51 const glm::vec3& rA,
52 const glm::vec3& rB,
53 const glm::vec3& direction
54) {
55 const glm::vec3 crossA = glm::cross(rA, direction);
56 const glm::vec3 crossB = glm::cross(rB, direction);
57 const float k = a.invMass + b.invMass
58 + glm::dot(crossA, a.invInertiaWorld * crossA)
59 + glm::dot(crossB, b.invInertiaWorld * crossB);
60 return k > 0.0f ? 1.0f / k : 0.0f;
61}
62
71 float biasRate = 0.0f;
72 float massScale = 1.0f;
73 float impulseScale = 0.0f;
74};
75
84inline SoftConstraint softness(float hertz, float damping, float dt) {
85 if (dt <= 0.0f) return {};
86 // Half the tick rate is what the step can carry; without damping and relax passes it would ring.
87 const float clamped = std::min(hertz, 0.5f / dt);
88 const float omega = 2.0f * glm::pi<float>() * clamped;
89 const float a1 = 2.0f * damping + dt * omega;
90 const float a2 = dt * omega * a1;
91 const float a3 = 1.0f / (1.0f + a2);
92 return SoftConstraint{omega / a1, a2 * a3, a3};
93}
94
95} // namespace Vkm::Engine::SolverMath
The three numbers that make a correction a stiff, damped spring.
Definition solver_math.h:70