-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrigidbody.cpp
203 lines (160 loc) · 4.14 KB
/
rigidbody.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "rigidbody.h"
#include <iostream>
//#include "mat.h"
#include "vec.h"
#include "gluvi.h"
using namespace std;
RigidBody::RigidBody(const float d, RigidGeometry& geom) :
density(d), geometry(geom),
velocity(0,0), angularMomentum(0), angularVelocity(0)
{
localFrame.set_position(Vec2f(0,0));
localFrame.set_orientation(0);
// Initializing inertial matrix
imatrix = 0;
geometry.get_inertia_tensor(density, imatrix);
mass = geometry.get_mass(density);
// Computing the inverse
imatrixInv = 1.0f/imatrix;
}
void RigidBody::getWorldInvInertiaMatrix(float& worldInvImatrix) const
{
worldInvImatrix = imatrixInv;
}
void RigidBody::get2DVertices(std::vector<Vec2f>&verts) const
{
geometry.get_vertices(verts);
for (unsigned int i = 0; i < verts.size(); ++i)
verts[i] = localFrame.local_to_global(verts[i]);
}
const float RigidBody::getMass() const
{
return mass;
}
CoordFrame2D RigidBody::getCoordFrame() const{
return localFrame;
}
void RigidBody::setCoordFrame(CoordFrame2D new_frame) {
localFrame = new_frame;
}
void RigidBody::getCOM(Vec2f& pos) const
{
pos = localFrame.position;
}
void RigidBody::setCOM(const Vec2f& pos)
{
localFrame.set_position(pos);
}
void RigidBody::setAngle(float angle) {
localFrame.set_orientation(angle);
}
float RigidBody::getAngle() {
return localFrame.orientation;
}
const float RigidBody::getDensity() const
{
return density;
}
const float RigidBody::getInertiaModulus() const
{
return imatrix;
}
const float RigidBody::getInvInertiaModulus() const
{
return imatrixInv;
}
void RigidBody::getLinearVelocity(Vec2f& vel) const
{
for (int i = 0; i != 2; ++i)
vel[i] = velocity[i];
}
void RigidBody::getAngularMomentum(float& Lz) const
{
Lz = angularMomentum;
}
// Setting angular momentum
void RigidBody::setAngularMomentum(const float& Lz)
{
angularMomentum = Lz;
recomputeAngularVelocity();
}
void RigidBody::setLinearVelocity(const Vec2f& vel)
{
for (int i = 0; i != 2; ++i)
velocity[i] = vel[i];
}
Vec2f RigidBody::getPointVelocity(const Vec2f& point) {
Vec2f linear;
float angular;
getLinearVelocity(linear);
getAngularVelocity(angular);
Vec2f centre;
getCOM(centre);
Vec3f radius(point[0]-centre[0], point[1]-centre[0], 0);
Vec3f point_vel = Vec3f(linear[0], linear[1], 0) + cross(Vec3f(0,0,angular), radius);
return Vec2f(point_vel[0], point_vel[1]);
}
const bool RigidBody::isInside(const Vec2f& P) const
{
// Convert P to the local frame
Vec2f Plocal = localFrame.global_to_local(P);
// Return true if Plocal belongs to the geometry
return geometry.is_inside(Plocal);
}
const bool RigidBody::testCollisionAndProject(const Vec2f& P, Vec2f& newPos)
{
// Convert P to the local frame
Vec2f Plocal = localFrame.global_to_local(P);
geometry.project_out(Plocal);
newPos = localFrame.local_to_global(Plocal);
return true;
}
const float RigidBody::getSignedDist(const Vec2f& P) const
{
// Convert P to the local frame
Vec2f Plocal = localFrame.global_to_local(P);
return geometry.get_signed_distance(Plocal);
}
void RigidBody::advance(const float dt)
{
// Updating position/orientation
updatePosition(dt);
updateOrientation(dt);
//Add force and torque
applyForce(Vec2f(0, -0.1f), dt);
applyTorque(0.0, dt);
}
void RigidBody::getAngularVelocity(float&wz)
{
recomputeAngularVelocity(); //just to be safe
wz = angularVelocity;
}
void RigidBody::applyForce(const Vec2f& force, const float dt)
{
//Updating velocity with external force
velocity[0] += force[0] * dt;
velocity[1] += force[1] * dt;
}
void RigidBody::applyTorque(float torque, const float dt)
{
// Updating angular momentum
angularMomentum += torque * dt;
recomputeAngularVelocity();
}
void RigidBody::updatePosition(const float dt)
{
// Updating center of mass
localFrame.position += velocity*dt;
}
void RigidBody::recomputeAngularVelocity()
{
// Updating angular velocity omega
// NB: omega = (I^-1)L
angularVelocity = imatrixInv * angularMomentum;
}
void RigidBody::updateOrientation(const float dt)
{
float rotation = angularVelocity*dt;
localFrame.rotate(rotation);
recomputeAngularVelocity();
}