-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegration.h
84 lines (69 loc) · 2.82 KB
/
Integration.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
// TODO shouldn't need these
#include "PhysicalShapes/CircleShape.h"
#include "PhysicalShapes/PolygonShape.h"
#include "PhysicalShapes/SegmentShape.h"
namespace Integration
{
inline void IntegratePositionals(const real dt, const Dynamical& dynamicsComp, Positional& outputPositional)
{
outputPositional.position = add(outputPositional.position, mul(dynamicsComp.linearVelocity, dt));
outputPositional.angle = outputPositional.angle + dynamicsComp.angularVelocity * dt;
}
inline void IntegrateCorrections(const real dt, const OverlapCorrectors& dynamicsComp, Positional& outputPositional, const real scale)
{
outputPositional.position = add(outputPositional.position, mul(dynamicsComp.linearCorrection, dt * scale));
outputPositional.angle = outputPositional.angle + dynamicsComp.angularCorrection * dt * scale;
}
inline void UpdateTransform(const Positional& integratedPos, Transform& outputTransform)
{
const real sinA = sin(integratedPos.angle);
const real cosA = cos(integratedPos.angle);
outputTransform = {
{ cosA, sinA, -sinA, cosA },
{
integratedPos.position.x - (integratedPos.centerOfMass.x * cosA - integratedPos.centerOfMass.y * sinA),
integratedPos.position.y - (integratedPos.centerOfMass.x * sinA + integratedPos.centerOfMass.y * cosA)
}
};
}
inline void ZeroOverlapCorrectors(OverlapCorrectors& dynamics)
{
dynamics.linearCorrection = ::zero<vec2>;
dynamics.angularCorrection = 0.0f;
}
inline void TransformPolyToWorld(const Transform& transform, PolygonShape* poly)
{
auto count = poly->localLines.size();
auto& dst = poly->transformedLines;
auto& src = poly->localLines;
for (size_t i = 0; i < count; i++) {
vec2 v = transform.transformPoint(src[i].point);
vec2 n = transform.rotation * src[i].normal;
dst[i].point = v;
dst[i].normal = n;
}
}
inline void TransformSegmentToWorld(const Transform& transform, SegmentShape& segment)
{
segment.transformedA = transform.transformPoint(segment.pointA);
segment.transformedB = transform.transformPoint(segment.pointB);
segment.transformedNormal = transform.rotation * segment.normal;
}
inline void TransformCircleToWorld(const Transform& transform, CircleShape& circle)
{
circle.transformedOffset = transform.transformPoint(circle.offset);
}
inline void IntegrateVelocities(
const InversePhysicalProps& physicalProps, const Forces forces, const real damping, const real dt,
Dynamical& dynamicsOutput)
{
dynamicsOutput.linearVelocity = add(mul(dynamicsOutput.linearVelocity, damping), mul(mul(forces.linearForce, physicalProps.massInverse), dt));
dynamicsOutput.angularVelocity = dynamicsOutput.angularVelocity * damping + forces.angularForce * physicalProps.momentInverse * dt;
}
inline void ZeroForces(Forces& forces)
{
forces.linearForce = ::zero<vec2>;
forces.angularForce = 0.0f;
}
};