-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathSmoothPositionSolver.cpp
56 lines (43 loc) · 1.49 KB
/
SmoothPositionSolver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "iw/physics/Dynamics/SmoothPositionSolver.h"
#include "iw/physics/Dynamics/Rigidbody.h"
#include "iw/log/logger.h"
namespace iw {
namespace Physics {
void SmoothPositionSolver::Solve(
std::vector<Manifold>& manifolds,
scalar dt)
{
std::vector<std::pair<glm::vec3, glm::vec3>> deltas;
for (Manifold& manifold : manifolds) {
Rigidbody* aBody = manifold.ObjA->IsDynamic ? (Rigidbody*)manifold.ObjA : nullptr;
Rigidbody* bBody = manifold.ObjB->IsDynamic ? (Rigidbody*)manifold.ObjB : nullptr;
scalar aInvMass = aBody ? aBody->InvMass : 0.0f;
scalar bInvMass = bBody ? bBody->InvMass : 0.0f;
const float percent = 0.8f;
const float slop = 0.01f;
glm::vec3 correction = manifold.Normal * percent
* fmax(manifold.Depth - slop, 0.0f)
/ (aInvMass + bInvMass);
glm::vec3 deltaA;
glm::vec3 deltaB;
if (aBody ? aBody->IsSimulated : false) {
deltaA = aInvMass * correction;
}
if (bBody ? bBody->IsSimulated : false) {
deltaB = bInvMass * correction;
}
deltas.emplace_back(deltaA, deltaB);
}
for (unsigned i = 0; i < manifolds.size(); i++) {
Rigidbody* aBody = manifolds[i].ObjA->IsDynamic ? (Rigidbody*)manifolds[i].ObjA : nullptr;
Rigidbody* bBody = manifolds[i].ObjB->IsDynamic ? (Rigidbody*)manifolds[i].ObjB : nullptr;
if (aBody ? aBody->IsSimulated : false) {
aBody->Transform.Position -= deltas[i].first;
}
if (bBody ? bBody->IsSimulated : false) {
bBody->Transform.Position += deltas[i].second;
}
}
}
}
}