-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrigidworld.cpp
More file actions
390 lines (332 loc) · 12.8 KB
/
rigidworld.cpp
File metadata and controls
390 lines (332 loc) · 12.8 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "rigidworld.h"
#include "math_utils.h"
#include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
#include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h"
#include <algorithm>
#include <iostream>
#include <set>
namespace SPD {
using namespace Eigen;
inline btCollisionShape* btbox(std::shared_ptr<Shape> shape) {
const Cuboid* box = static_cast<Cuboid*>(shape.get());
return new btBoxShape(btv3(box->half_dims));
}
inline btCollisionShape* btsphere(std::shared_ptr<Shape> shape) {
const Sphere* sp = static_cast<Sphere*>(shape.get());
return new btSphereShape(sp->radius);
}
inline btCollisionShape* btconvexhull(std::shared_ptr<Shape> shape) {
const ConvexHull* ch = static_cast<ConvexHull*>(shape.get());
btConvexHullShape* btshape = new btConvexHullShape();
for (const Vector3f& p : ch->positions) {
btshape->addPoint(btv3(p));
}
btshape->setMargin(0.0f);
return btshape;
}
inline btCollisionShape* btcylinder(std::shared_ptr<Shape> shape) {
const Cylinder* cy = static_cast<Cylinder*>(shape.get());
return new btCylinderShape(btVector3(cy->half_dims.x(), cy->half_dims.y(), cy->half_dims.x()));
}
inline btCollisionShape* btcompound(std::shared_ptr<Shape> shape) {
const CompoundShape* cs = static_cast<CompoundShape*>(shape.get());
btCompoundShape* btshape = new btCompoundShape(true, cs->compositions.size());
for (const CompoundShape::Composition& comp : cs->compositions) {
if (comp.shape->type == Shape::Type::Cuboid) {
btshape->addChildShape(bttrans(comp.rotation, comp.translation), btbox(comp.shape));
}
else if (comp.shape->type == Shape::Type::ConvexHull) {
btshape->addChildShape(bttrans(comp.rotation, comp.translation), btconvexhull(comp.shape));
}
else if (comp.shape->type == Shape::Type::Sphere) {
btshape->addChildShape(bttrans(comp.rotation, comp.translation), btsphere(comp.shape));
}
else if (comp.shape->type == Shape::Type::Cylinder) {
btshape->addChildShape(bttrans(comp.rotation, comp.translation), btcylinder(comp.shape));
}
else {
assert(false);
}
}
return btshape;
}
std::shared_ptr<RigidWorld::Collider> RigidWorld::Collider::create(const RigidBody& rigidbody, int user_id) {
btCollisionShape* shape = nullptr;
if (rigidbody.shape->type == Shape::Type::Cuboid) shape = btbox(rigidbody.shape);
else if (rigidbody.shape->type == Shape::Type::Sphere) shape = btsphere(rigidbody.shape);
else if (rigidbody.shape->type == Shape::Type::ConvexHull) shape = btconvexhull(rigidbody.shape);
else if (rigidbody.shape->type == Shape::Type::Compound) shape = btcompound(rigidbody.shape);
else if (rigidbody.shape->type == Shape::Type::Cylinder) shape = btcylinder(rigidbody.shape);
else {
assert(false);
}
btCollisionObject* obj = new btCollisionObject();
obj->setCollisionShape(shape);
btTransform transform(
btquat(rigidbody.rotation),
btv3(rigidbody.translation));
obj->setWorldTransform(transform);
obj->setUserIndex(user_id);
obj->setUserIndex2(-1); // articulated body indicator. -1 indicated it is not an art body
std::shared_ptr<Collider> collider = std::make_shared<Collider>();
collider->shape.reset(shape);
collider->obj.reset(obj);
return collider;
}
std::vector<std::shared_ptr<RigidWorld::Collider>> RigidWorld::Collider::create(const ArticulatedBody& artbody, int user_id) {
std::vector<std::shared_ptr<RigidWorld::Collider>> art_colliders;
for (int i = 0; i < artbody.bodies.size(); ++i) {
auto& body = artbody.bodies[i];
btCollisionShape* shape = nullptr;
assert(body);
if (!body->shape) {
continue;
}
if (body->shape->type == Shape::Type::Cuboid) shape = btbox(body->shape);
else if (body->shape->type == Shape::Type::Sphere) shape = btsphere(body->shape);
else if (body->shape->type == Shape::Type::ConvexHull) shape = btconvexhull(body->shape);
else if (body->shape->type == Shape::Type::Compound) shape = btcompound(body->shape);
else if (body->shape->type == Shape::Type::Cylinder) shape = btcylinder(body->shape);
else {
assert(false);
}
btCollisionObject* obj = new btCollisionObject();
obj->setCollisionShape(shape);
btTransform transform(
btquat(body->rotation),
btv3(body->translation));
obj->setWorldTransform(transform);
obj->setUserIndex(user_id);
obj->setUserIndex2(i);
std::shared_ptr<Collider> collider = std::make_shared<Collider>();
collider->shape.reset(shape);
collider->obj.reset(obj);
art_colliders.push_back(collider);
}
return art_colliders;
}
void RigidWorld::Collider::update(Eigen::Vector3f translation, Eigen::Quaternionf rotation) {
btTransform transform(
btquat(rotation),
btv3(translation));
obj->setWorldTransform(transform);
}
RigidWorld::RigidWorld(Eigen::Vector3f gravity) {
this->gravity = gravity;
btDefaultCollisionConfiguration* config = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(config );
btDbvtBroadphase* broadphase = new btDbvtBroadphase();
btCollisionWorld* world = new btCollisionWorld(dispatcher, broadphase, config);
AdjacentLinkFilter* filter = new RigidWorld::AdjacentLinkFilter();
collision_world.reset(new CollisionWorld);
collision_world->articulation_collision_filter.reset(filter);
collision_world->config.reset(config);
collision_world->dispatcher.reset(dispatcher);
collision_world->broadphase.reset(broadphase);
collision_world->world.reset(world);
collision_world->world->getPairCache()->setOverlapFilterCallback(filter);
contact_solver.reset(new ContactSolver());
loop_joint_solver.reset(new LoopJointSolver());
}
RigidWorld::~RigidWorld() {
for (auto c : rigid_colliders) {
collision_world->world->removeCollisionObject(c->obj.get());
}
for (auto& c : art_colliders) {
for (auto cc : c) {
collision_world->world->removeCollisionObject(cc->obj.get());
}
}
gContactDestroyedCallback = nullptr;
collision_world->world->getPairCache()->setOverlapFilterCallback(nullptr);
collision_world->world.reset();
collision_world->broadphase.reset();
collision_world->dispatcher.reset();
collision_world->config.reset();
collision_world->articulation_collision_filter.reset();
}
void RigidWorld::add_body(std::shared_ptr<RigidBody> rigidbody) {
if (!rigidbody || !rigidbody->shape || rigidbody->shape->type == Shape::Type::Default) {
return;
}
rigidbodies.push_back(rigidbody);
std::shared_ptr<Collider> c = Collider::create(*rigidbody, rigidbodies.size() - 1);
rigid_colliders.push_back(c);
collision_world->world->addCollisionObject(c->obj.get());
}
void RigidWorld::add_body(std::shared_ptr<ArticulatedBody> artbody) {
if (artbody->bodies.empty()) {
assert(false);
return;
}
artbody->build_tree();
artbodies.push_back(artbody);
artbody->set_gravity(this->gravity);
collision_world->articulation_collision_filter->add_body(artbody);
std::vector<std::shared_ptr<Collider>> cs = std::move(Collider::create(*artbody, artbodies.size() - 1));
art_colliders.push_back(cs);
for (std::shared_ptr<Collider> c : cs) {
collision_world->world->addCollisionObject(c->obj.get());
}
}
static int step_count = 0;
void RigidWorld::step(float dt) {
collide();
integrate_velocity(dt);
contact_solver->initialize(rigidbodies, artbodies, collision_world->dispatcher);
loop_joint_solver->initialize(artbodies);
contact_solver->warm_start();
loop_joint_solver->warm_start();
for (uint32_t i = 0; i < max_velocity_solve_iterations; ++i) {
contact_solver->solve_velocity();
loop_joint_solver->solve_velocity();
}
for (auto artbody : artbodies) {
artbody->project_velocity();
}
// contact_solver->project_velocity(); // TODO: consider moving this to RigidWorld. project_velocity should not be exclusive to contact solver
integrate_position(dt);
for (uint32_t i = 0; i < max_position_solve_iterations; ++i) {
contact_solver->solve_position();
loop_joint_solver->solve_position();
}
//for (auto artbody : artbodies) {
// artbody->project_velocity(); //
//}
}
void RigidWorld::collide() {
for (int i = 0; i < rigidbodies.size(); ++i) {
RigidBody& b = *rigidbodies[i];
Collider& c = *rigid_colliders[i];
if (b.type == RigidBody::DynamicType::Static) {
continue;
}
c.update(b.translation, b.rotation);
collision_world->world->updateSingleAabb(c.obj.get());
}
for (int i = 0; i < artbodies.size(); ++i) {
ArticulatedBody& art_b = *artbodies[i];
std::vector<std::shared_ptr<Collider>>& art_c = art_colliders[i];
for (int j = 0; j < art_b.bodies.size(); ++j) {
art_c[j]->update(art_b.bodies[j]->translation, art_b.bodies[j]->rotation);
collision_world->world->updateSingleAabb(art_c[j]->obj.get());
}
}
collision_world->world->performDiscreteCollisionDetection();
}
void RigidWorld::integrate_velocity(float dt) {
for (auto b : rigidbodies) {
if (b->type == RigidBody::DynamicType::Static) {
continue;
}
FVector g6_com;
g6_com << Vector3f::Zero(), gravity;
FVector fe_o = dual_transform(m_transform(Matrix3f::Identity(), Matrix3f::Identity(), b->rotation * -b->shape->com)) * g6_com * b->mass;
// Accomodates rotation
MTransform Xr = m_transform(Matrix3f::Identity(), b->rotation.toRotationMatrix(), Vector3f::Zero());
Dyad I = transform_dyad2(Xr, b->I);
FVector bias = dual_transform(derivative_cross(b->v)) * I * b->v;
MVector a = transform_inv_dyad2(Xr, b->inv_I) * (fe_o - bias);
MVector v_ortho = b->v;
MVector a_ortho = a;
Vector3f v_ang = v_ortho.head<3>();
Vector3f v_linear = v_ortho.tail<3>();
Vector3f a_ang = a_ortho.head<3>();
Vector3f a_linear = a_ortho.tail<3>() + cross_mat(v_ang) * v_linear;
// implicit euler on ortho linear velocity
v_linear += a_linear * dt;
v_linear *= std::pow(1.0f - b->linear_damping, dt); // linear damping
// implicit euler on ortho angular velocity
v_ang += a_ang * dt;
v_ang *= std::pow(1.0f - b->angular_damping, dt); // angular damping
v_ortho << v_ang, v_linear;
b->v = /*inverse_transform(X_ortho) */ v_ortho;
}
for (auto ab : artbodies) {
ab->integrate_velocity(dt);
}
}
void RigidWorld::integrate_position(float dt) {
// TODO: for an accurate integration, see line 1103 of btDiscretePhysicsWorld.cpp predictIntegratedTransform
for (auto b : rigidbodies) {
// implicit euler on ortho linear velocity
Vector3f v_linear = b->v.tail<3>();
b->translation += v_linear * dt;
// implicit euler on ortho angular velocity
// TODO:
// follows this answer https://math.stackexchange.com/a/5035902
//Vector3f v_ang = b->v.head<3>();
//Quaternionf q = b->rotation;
//Quaternionf dq = q * Quaternionf(0.0f, v_ang.x(), v_ang.y(), v_ang.z());
//dq.coeffs() *= 0.5f;
//Quaternionf dqdt;
//dqdt.coeffs() = dq.coeffs() * dt;
//q.coeffs() = q.coeffs() + dqdt.coeffs();
//b->rotation = q;
//b->rotation.normalize();
// calculation given by deepseek. Works great, but doesnt align with the stackoverflow answer
//Vector3f v_ang = b->v.head<3>();
//Quaternionf omega_q(0.0f, v_ang.x(), v_ang.y(), v_ang.z());
//Quaternionf dq = (omega_q * b->rotation);
//dq.coeffs() *= 0.5f * dt;
//b->rotation.coeffs() += dq.coeffs();
//b->rotation.normalize();
// Not accurate but the the easiest to understand
Vector3f v_ang = b->v.head<3>();
float angle = v_ang.norm() * dt;
if (angle > 1e-8f) {
Vector3f axis = v_ang.normalized();
Quaternionf delta_q(AngleAxisf(angle, axis));
b->rotation = delta_q * b->rotation;
}
b->rotation.normalize();
}
// TODO integrate articulated body positions
for (auto ab : artbodies) {
ab->integrate_position(dt);
}
}
void RigidWorld::AdjacentLinkFilter::add_body(std::shared_ptr<ArticulatedBody> artbody) {
collision_disable_maps.emplace_back();
AdjacencyMap& am = collision_disable_maps.back();
for (auto joint : artbody->tree_joints) {
if (!joint) {
continue;
}
if (joint->disable_collision) {
am[joint->b0->id].insert(joint->b1->id);
am[joint->b1->id].insert(joint->b0->id);
}
}
for (auto joint : artbody->loop_joints) {
if (joint->disable_collision) {
am[joint->b0->id].insert(joint->b1->id);
am[joint->b1->id].insert(joint->b0->id);
}
}
for (auto constraint : artbody->constraints) {
if (constraint->disable_collision) {
am[constraint->j0->b1->id].insert(constraint->j1->b1->id); // TODO: arbitrary constrained joints polarity
am[constraint->j1->b1->id].insert(constraint->j0->b1->id);
}
}
}
bool RigidWorld::AdjacentLinkFilter::needBroadphaseCollision(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1) const {
auto* obj0 = static_cast<btCollisionObject*>(proxy0->m_clientObject);
auto* obj1 = static_cast<btCollisionObject*>(proxy1->m_clientObject);
int id0 = obj0->getUserIndex();
int id1 = obj1->getUserIndex();
int sub_id0 = obj0->getUserIndex2();
int sub_id1 = obj1->getUserIndex2();
bool need_collision = true;
if (id0 == id1 && sub_id0 >= 0 && sub_id1 >= 0) {
// in the same articulated body, different link
auto iter = collision_disable_maps[id0].find(sub_id0);
if (iter != collision_disable_maps[id0].end() &&
iter->second.count(sub_id1) > 0) {
need_collision = false;
}
}
return need_collision;
}
}