-
Notifications
You must be signed in to change notification settings - Fork 43
/
SimpleDynamicBodyComponentTest.cpp
101 lines (82 loc) · 2.83 KB
/
SimpleDynamicBodyComponentTest.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Copyright 2012-2020 Matthew Reid
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <SkyboltSim/Components/SimpleDynamicBodyComponent.h>
#include <SkyboltSim/Components/Node.h>
#include <SkyboltSim/SimMath.h>
#include <catch2/catch.hpp>
#include <assert.h>
using namespace skybolt;
using namespace skybolt::sim;
static void integrateOverTime(SimpleDynamicBodyComponent& body, TimeReal duration)
{
constexpr TimeReal dt = 0.01f;
for (TimeReal t = 0; t < duration; t += dt)
{
body.updatePreDynamicsSubstep(dt);
}
}
TEST_CASE("Body accelerates under force")
{
Node node;
Real mass = 5;
Vector3 momentOfInertia(2, 3, 4);
SimpleDynamicBodyComponent body(&node, mass, momentOfInertia);
// Calculate expected results
// https://www.calculatorsoup.com/calculators/physics/displacement_v_a_t.php
float t = 3;
float f = 10;
Real a = f / mass;
Real s = 0.5f * a * t * t;
// Simulate
body.applyCentralForce(Vector3(f, 0, 0));
integrateOverTime(body, t);
CHECK(node.getPosition().x == Approx(s).epsilon(0.01));
CHECK(node.getPosition().y == Approx(0).epsilon(1e-8f));
CHECK(node.getPosition().z == Approx(0).epsilon(1e-8f));
}
TEST_CASE("Body rotates under torque")
{
Node node;
Real mass = 5;
Vector3 momentOfInertia(2, 3, 4);
SimpleDynamicBodyComponent body(&node, mass, momentOfInertia);
// Calculate expected results
// https://www.calculatorsoup.com/calculators/physics/displacement_v_a_t.php
float t = 3;
float T = 0.1f;
Real a = T / Real(momentOfInertia.x);
Real theta = 0.5f * a * t * t;
// Simulate
body.applyTorque(Vector3(T, 0, 0));
integrateOverTime(body, t);
Vector3 euler = math::eulerFromQuat(node.getOrientation());
CHECK(euler.x == Approx(theta).epsilon(0.01));
CHECK(euler.y == Approx(0).epsilon(1e-8f));
CHECK(euler.z == Approx(0).epsilon(1e-8f));
}
TEST_CASE("Force at distance produces torque")
{
Node node;
Real mass = 5;
Vector3 momentOfInertia(2, 3, 4);
SimpleDynamicBodyComponent body(&node, mass, momentOfInertia);
Vector3 centerOfMass(0.2, 0.3, 0.4);
body.setCenterOfMass(centerOfMass);
// Calculate expected results
// https://www.calculatorsoup.com/calculators/physics/displacement_v_a_t.php
float t = 3;
float f = 0.1f;
float offset = 0.5f;
Real a = f * offset / Real(momentOfInertia.x);
Real theta = 0.5f * a * t * t;
// Simulate
body.applyForce(Vector3(0, 0, f), Vector3(0, offset, 0) + centerOfMass);
integrateOverTime(body, t);
Vector3 euler = math::eulerFromQuat(node.getOrientation());
CHECK(euler.x == Approx(theta).epsilon(0.01));
CHECK(euler.y == Approx(0).epsilon(1e-8f));
CHECK(euler.z == Approx(0).epsilon(1e-8f));
}