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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2013 Blender Foundation
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): Joshua Leung, Sergej Reich
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file rb_bullet_api.cpp
* \ingroup RigidBody
* \brief Rigid Body API implementation for Bullet
*/
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* This file defines the "RigidBody interface" for the
* Bullet Physics Engine. This API is designed to be used
* from C-code in Blender as part of the Rigid Body simulation
* system.
*
* It is based on the Bullet C-API, but is heavily modified to
* give access to more data types and to offer a nicer interface.
*
* -- Joshua Leung, June 2010
*/
#include <stdio.h>
#include "RBI_api.h"
#include "BLI_math.h"
#include "btBulletDynamicsCommon.h"
#include "LinearMath/btVector3.h"
#include "LinearMath/btScalar.h"
#include "LinearMath/btMatrix3x3.h"
#include "LinearMath/btTransform.h"
#include "LinearMath/btConvexHullComputer.h"
#include "BulletCollision/Gimpact/btGImpactShape.h"
#include "BulletCollision/Gimpact/btGImpactCollisionAlgorithm.h"
#include "BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h"
struct rbDynamicsWorld {
btDiscreteDynamicsWorld *dynamicsWorld;
btDefaultCollisionConfiguration *collisionConfiguration;
btDispatcher *dispatcher;
btBroadphaseInterface *pairCache;
btConstraintSolver *constraintSolver;
btOverlapFilterCallback *filterCallback;
};
struct rbRigidBody {
btRigidBody *body;
int col_groups;
};
struct rbCollisionShape {
btCollisionShape *cshape;
btTriangleMesh *mesh;
};
struct rbFilterCallback : public btOverlapFilterCallback
{
virtual bool needBroadphaseCollision(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1) const
{
rbRigidBody *rb0 = (rbRigidBody *)((btRigidBody *)proxy0->m_clientObject)->getUserPointer();
rbRigidBody *rb1 = (rbRigidBody *)((btRigidBody *)proxy1->m_clientObject)->getUserPointer();
bool collides;
collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
collides = collides && (rb0->col_groups & rb1->col_groups);
return collides;
}
};
static inline void copy_v3_btvec3(float vec[3], const btVector3 &btvec)
{
vec[0] = (float)btvec[0];
vec[1] = (float)btvec[1];
vec[2] = (float)btvec[2];
}
static inline void copy_quat_btquat(float quat[3], const btQuaternion &btquat)
{
quat[0] = btquat.getW();
quat[1] = btquat.getX();
quat[2] = btquat.getY();
quat[3] = btquat.getZ();
}
/* ********************************** */
/* Dynamics World Methods */
/* Setup ---------------------------- */
rbDynamicsWorld *RB_dworld_new(const float gravity[3])
{
rbDynamicsWorld *world = new rbDynamicsWorld;
/* collision detection/handling */
world->collisionConfiguration = new btDefaultCollisionConfiguration();
world->dispatcher = new btCollisionDispatcher(world->collisionConfiguration);
btGImpactCollisionAlgorithm::registerAlgorithm((btCollisionDispatcher *)world->dispatcher); // XXX: experimental
world->pairCache = new btDbvtBroadphase();
world->filterCallback = new rbFilterCallback();
world->pairCache->getOverlappingPairCache()->setOverlapFilterCallback(world->filterCallback);
/* constraint solving */
world->constraintSolver = new btSequentialImpulseConstraintSolver();
/* world */
world->dynamicsWorld = new btDiscreteDynamicsWorld(world->dispatcher,
world->pairCache,
world->constraintSolver,
world->collisionConfiguration);
RB_dworld_set_gravity(world, gravity);
return world;
}
void RB_dworld_delete(rbDynamicsWorld *world)
{
/* bullet doesn't like if we free these in a different order */
delete world->dynamicsWorld;
delete world->constraintSolver;
delete world->pairCache;
delete world->dispatcher;
delete world->collisionConfiguration;
delete world->filterCallback;
delete world;
}
/* Settings ------------------------- */
/* Gravity */
void RB_dworld_get_gravity(rbDynamicsWorld *world, float g_out[3])
{
copy_v3_btvec3(g_out, world->dynamicsWorld->getGravity());
}
void RB_dworld_set_gravity(rbDynamicsWorld *world, const float g_in[3])
{
world->dynamicsWorld->setGravity(btVector3(g_in[0], g_in[1], g_in[2]));
}
/* Constraint Solver */
void RB_dworld_set_solver_iterations(rbDynamicsWorld *world, int num_solver_iterations)
{
btContactSolverInfo& info = world->dynamicsWorld->getSolverInfo();
info.m_numIterations = num_solver_iterations;
}
/* Split Impulse */
void RB_dworld_set_split_impulse(rbDynamicsWorld *world, int split_impulse)
{
btContactSolverInfo& info = world->dynamicsWorld->getSolverInfo();
info.m_splitImpulse = split_impulse;
}
/* Simulation ----------------------- */
void RB_dworld_step_simulation(rbDynamicsWorld *world, float timeStep, int maxSubSteps, float timeSubStep)
{
world->dynamicsWorld->stepSimulation(timeStep, maxSubSteps, timeSubStep);
}
/* Export -------------------------- */
/* Exports entire dynamics world to Bullet's "*.bullet" binary format
* which is similar to Blender's SDNA system...
* < rbDynamicsWorld: dynamics world to write to file
* < filename: assumed to be a valid filename, with .bullet extension
*/
void RB_dworld_export(rbDynamicsWorld *world, const char *filename)
{
//create a large enough buffer. There is no method to pre-calculate the buffer size yet.
int maxSerializeBufferSize = 1024 * 1024 * 5;
btDefaultSerializer *serializer = new btDefaultSerializer(maxSerializeBufferSize);
world->dynamicsWorld->serialize(serializer);
FILE *file = fopen(filename, "wb");
fwrite(serializer->getBufferPointer(), serializer->getCurrentBufferSize(), 1, file);
fclose(file);
}
/* ********************************** */
/* Rigid Body Methods */
/* Setup ---------------------------- */
void RB_dworld_add_body(rbDynamicsWorld *world, rbRigidBody *object, int col_groups)
{
btRigidBody *body = object->body;
object->col_groups = col_groups;
world->dynamicsWorld->addRigidBody(body);
}
void RB_dworld_remove_body(rbDynamicsWorld *world, rbRigidBody *object)
{
btRigidBody *body = object->body;
world->dynamicsWorld->removeRigidBody(body);
}
/* ............ */
rbRigidBody *RB_body_new(rbCollisionShape *shape, const float loc[3], const float rot[4])
{
rbRigidBody *object = new rbRigidBody;
/* current transform */
btTransform trans;
trans.setOrigin(btVector3(loc[0], loc[1], loc[2]));
trans.setRotation(btQuaternion(rot[1], rot[2], rot[3], rot[0]));
/* create motionstate, which is necessary for interpolation (includes reverse playback) */
btDefaultMotionState *motionState = new btDefaultMotionState(trans);
/* make rigidbody */
btRigidBody::btRigidBodyConstructionInfo rbInfo(1.0f, motionState, shape->cshape);
object->body = new btRigidBody(rbInfo);
object->body->setUserPointer(object);
return object;
}
void RB_body_delete(rbRigidBody *object)
{
btRigidBody *body = object->body;
/* motion state */
btMotionState *ms = body->getMotionState();
if (ms)
delete ms;
/* collision shape is done elsewhere... */
/* body itself */
/* manually remove constraint refs of the rigid body, normally this happens when removing constraints from the world
* but since we delete everything when the world is rebult, we need to do it manually here */
for (int i = body->getNumConstraintRefs() - 1; i >= 0; i--) {
btTypedConstraint *con = body->getConstraintRef(i);
body->removeConstraintRef(con);
}
delete body;
delete object;
}
/* Settings ------------------------- */
void RB_body_set_collision_shape(rbRigidBody *object, rbCollisionShape *shape)
{
btRigidBody *body = object->body;
/* set new collision shape */
body->setCollisionShape(shape->cshape);
/* recalculate inertia, since that depends on the collision shape... */
RB_body_set_mass(object, RB_body_get_mass(object));
}
/* ............ */
float RB_body_get_mass(rbRigidBody *object)
{
btRigidBody *body = object->body;
/* there isn't really a mass setting, but rather 'inverse mass'
* which we convert back to mass by taking the reciprocal again
*/
float value = (float)body->getInvMass();
if (value)
value = 1.0 / value;
return value;
}
void RB_body_set_mass(rbRigidBody *object, float value)
{
btRigidBody *body = object->body;
btVector3 localInertia(0, 0, 0);
/* calculate new inertia if non-zero mass */
if (value) {
btCollisionShape *shape = body->getCollisionShape();
shape->calculateLocalInertia(value, localInertia);
}
body->setMassProps(value, localInertia);
body->updateInertiaTensor();
}
float RB_body_get_friction(rbRigidBody *object)
{
btRigidBody *body = object->body;
return body->getFriction();
}
void RB_body_set_friction(rbRigidBody *object, float value)
{
btRigidBody *body = object->body;
body->setFriction(value);
}
float RB_body_get_restitution(rbRigidBody *object)
{
btRigidBody *body = object->body;
return body->getRestitution();
}
void RB_body_set_restitution(rbRigidBody *object, float value)
{
btRigidBody *body = object->body;
body->setRestitution(value);
}
float RB_body_get_linear_damping(rbRigidBody *object)
{
btRigidBody *body = object->body;
return body->getLinearDamping();
}
void RB_body_set_linear_damping(rbRigidBody *object, float value)
{
RB_body_set_damping(object, value, RB_body_get_linear_damping(object));
}
float RB_body_get_angular_damping(rbRigidBody *object)
{
btRigidBody *body = object->body;
return body->getAngularDamping();
}
void RB_body_set_angular_damping(rbRigidBody *object, float value)
{
RB_body_set_damping(object, RB_body_get_linear_damping(object), value);
}
void RB_body_set_damping(rbRigidBody *object, float linear, float angular)
{
btRigidBody *body = object->body;
body->setDamping(linear, angular);
}
float RB_body_get_linear_sleep_thresh(rbRigidBody *object)
{
btRigidBody *body = object->body;
return body->getLinearSleepingThreshold();
}
void RB_body_set_linear_sleep_thresh(rbRigidBody *object, float value)
{
RB_body_set_sleep_thresh(object, value, RB_body_get_angular_sleep_thresh(object));
}
float RB_body_get_angular_sleep_thresh(rbRigidBody *object)
{
btRigidBody *body = object->body;
return body->getAngularSleepingThreshold();
}
void RB_body_set_angular_sleep_thresh(rbRigidBody *object, float value)
{
RB_body_set_sleep_thresh(object, RB_body_get_linear_sleep_thresh(object), value);
}
void RB_body_set_sleep_thresh(rbRigidBody *object, float linear, float angular)
{
btRigidBody *body = object->body;
body->setSleepingThresholds(linear, angular);
}
/* ............ */
void RB_body_get_linear_velocity(rbRigidBody *object, float v_out[3])
{
btRigidBody *body = object->body;
copy_v3_btvec3(v_out, body->getLinearVelocity());
}
void RB_body_set_linear_velocity(rbRigidBody *object, const float v_in[3])
{
btRigidBody *body = object->body;
body->setLinearVelocity(btVector3(v_in[0], v_in[1], v_in[2]));
}
void RB_body_get_angular_velocity(rbRigidBody *object, float v_out[3])
{
btRigidBody *body = object->body;
copy_v3_btvec3(v_out, body->getAngularVelocity());
}
void RB_body_set_angular_velocity(rbRigidBody *object, const float v_in[3])
{
btRigidBody *body = object->body;
body->setAngularVelocity(btVector3(v_in[0], v_in[1], v_in[2]));
}
void RB_body_set_linear_factor(rbRigidBody *object, float x, float y, float z)
{
btRigidBody *body = object->body;
body->setLinearFactor(btVector3(x, y, z));
}
void RB_body_set_angular_factor(rbRigidBody *object, float x, float y, float z)
{
btRigidBody *body = object->body;
body->setAngularFactor(btVector3(x, y, z));
}
/* ............ */
void RB_body_set_kinematic_state(rbRigidBody *object, int kinematic)
{
btRigidBody *body = object->body;
if (kinematic)
body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
else
body->setCollisionFlags(body->getCollisionFlags() & ~btCollisionObject::CF_KINEMATIC_OBJECT);
}
/* ............ */
void RB_body_set_activation_state(rbRigidBody *object, int use_deactivation)
{
btRigidBody *body = object->body;
if (use_deactivation)
body->forceActivationState(ACTIVE_TAG);
else
body->setActivationState(DISABLE_DEACTIVATION);
}
void RB_body_activate(rbRigidBody *object)
{
btRigidBody *body = object->body;
body->setActivationState(ACTIVE_TAG);
}
void RB_body_deactivate(rbRigidBody *object)
{
btRigidBody *body = object->body;
body->setActivationState(ISLAND_SLEEPING);
}
/* ............ */
/* Simulation ----------------------- */
/* The transform matrices Blender uses are OpenGL-style matrices,
* while Bullet uses the Right-Handed coordinate system style instead.
*/
void RB_body_get_transform_matrix(rbRigidBody *object, float m_out[4][4])
{
btRigidBody *body = object->body;
btMotionState *ms = body->getMotionState();
btTransform trans;
ms->getWorldTransform(trans);
trans.getOpenGLMatrix((btScalar *)m_out);
}
void RB_body_set_loc_rot(rbRigidBody *object, const float loc[3], const float rot[4])
{
btRigidBody *body = object->body;
btMotionState *ms = body->getMotionState();
/* set transform matrix */
btTransform trans;
trans.setOrigin(btVector3(loc[0], loc[1], loc[2]));
trans.setRotation(btQuaternion(rot[1], rot[2], rot[3], rot[0]));
ms->setWorldTransform(trans);
}
void RB_body_set_scale(rbRigidBody *object, const float scale[3])
{
btRigidBody *body = object->body;
/* apply scaling factor from matrix above to the collision shape */
btCollisionShape *cshape = body->getCollisionShape();
if (cshape) {
cshape->setLocalScaling(btVector3(scale[0], scale[1], scale[2]));
/* GIimpact shapes have to be updated to take scaling into account */
if (cshape->getShapeType() == GIMPACT_SHAPE_PROXYTYPE)
((btGImpactMeshShape *)cshape)->updateBound();
}
}
/* ............ */
/* Read-only state info about status of simulation */
void RB_body_get_position(rbRigidBody *object, float v_out[3])
{
btRigidBody *body = object->body;
copy_v3_btvec3(v_out, body->getWorldTransform().getOrigin());
}
void RB_body_get_orientation(rbRigidBody *object, float v_out[4])
{
btRigidBody *body = object->body;
copy_quat_btquat(v_out, body->getWorldTransform().getRotation());
}
/* ............ */
/* Overrides for simulation */
void RB_body_apply_central_force(rbRigidBody *object, const float v_in[3])
{
btRigidBody *body = object->body;
body->applyCentralForce(btVector3(v_in[0], v_in[1], v_in[2]));
}
/* ********************************** */
/* Collision Shape Methods */
/* Setup (Standard Shapes) ----------- */
rbCollisionShape *RB_shape_new_box(float x, float y, float z)
{
rbCollisionShape *shape = new rbCollisionShape;
shape->cshape = new btBoxShape(btVector3(x, y, z));
shape->mesh = NULL;
return shape;
}
rbCollisionShape *RB_shape_new_sphere(float radius)
{
rbCollisionShape *shape = new rbCollisionShape;
shape->cshape = new btSphereShape(radius);
shape->mesh = NULL;
return shape;
}
rbCollisionShape *RB_shape_new_capsule(float radius, float height)
{
rbCollisionShape *shape = new rbCollisionShape;
shape->cshape = new btCapsuleShapeZ(radius, height);
shape->mesh = NULL;
return shape;
}
rbCollisionShape *RB_shape_new_cone(float radius, float height)
{
rbCollisionShape *shape = new rbCollisionShape;
shape->cshape = new btConeShapeZ(radius, height);
shape->mesh = NULL;
return shape;
}
rbCollisionShape *RB_shape_new_cylinder(float radius, float height)
{
rbCollisionShape *shape = new rbCollisionShape;
shape->cshape = new btCylinderShapeZ(btVector3(radius, radius, height));
shape->mesh = NULL;
return shape;
}
/* Setup (Convex Hull) ------------ */
rbCollisionShape *RB_shape_new_convex_hull(float *verts, int stride, int count, float margin, bool *can_embed)
{
btConvexHullComputer hull_computer = btConvexHullComputer();
// try to embed the margin, if that fails don't shrink the hull
if (hull_computer.compute(verts, stride, count, margin, 0.0f) < 0.0f) {
hull_computer.compute(verts, stride, count, 0.0f, 0.0f);
*can_embed = false;
}
rbCollisionShape *shape = new rbCollisionShape;
btConvexHullShape *hull_shape = new btConvexHullShape(&(hull_computer.vertices[0].getX()), hull_computer.vertices.size());
shape->cshape = hull_shape;
shape->mesh = NULL;
return shape;
}
/* Setup (Triangle Mesh) ---------- */
/* Need to call rbTriMeshNewData() followed by rbTriMeshAddTriangle() several times
* to set up the mesh buffer BEFORE calling rbShapeNewTriMesh(). Otherwise,
* we get nasty crashes...
*/
rbMeshData *RB_trimesh_data_new()
{
// XXX: welding threshold?
return (rbMeshData *) new btTriangleMesh(true, false);
}
void RB_trimesh_add_triangle(rbMeshData *mesh, const float v1[3], const float v2[3], const float v3[3])
{
btTriangleMesh *meshData = reinterpret_cast<btTriangleMesh*>(mesh);
/* cast vertices to usable forms for Bt-API */
btVector3 vtx1((btScalar)v1[0], (btScalar)v1[1], (btScalar)v1[2]);
btVector3 vtx2((btScalar)v2[0], (btScalar)v2[1], (btScalar)v2[2]);
btVector3 vtx3((btScalar)v3[0], (btScalar)v3[1], (btScalar)v3[2]);
/* add to the mesh
* - remove duplicated verts is enabled
*/
meshData->addTriangle(vtx1, vtx2, vtx3, false);
}
rbCollisionShape *RB_shape_new_trimesh(rbMeshData *mesh)
{
rbCollisionShape *shape = new rbCollisionShape;
btTriangleMesh *tmesh = reinterpret_cast<btTriangleMesh*>(mesh);
/* triangle-mesh we create is a BVH wrapper for triangle mesh data (for faster lookups) */
// RB_TODO perhaps we need to allow saving out this for performance when rebuilding?
btBvhTriangleMeshShape *unscaledShape = new btBvhTriangleMeshShape(tmesh, true, true);
shape->cshape = new btScaledBvhTriangleMeshShape(unscaledShape, btVector3(1.0f, 1.0f, 1.0f));
shape->mesh = tmesh;
return shape;
}
rbCollisionShape *RB_shape_new_gimpact_mesh(rbMeshData *mesh)
{
rbCollisionShape *shape = new rbCollisionShape;
/* interpret mesh buffer as btTriangleIndexVertexArray (i.e. an impl of btStridingMeshInterface) */
btTriangleMesh *tmesh = reinterpret_cast<btTriangleMesh*>(mesh);
btGImpactMeshShape *gimpactShape = new btGImpactMeshShape(tmesh);
gimpactShape->updateBound(); // TODO: add this to the update collision margin call?
shape->cshape = gimpactShape;
shape->mesh = tmesh;
return shape;
}
/* Cleanup --------------------------- */
void RB_shape_delete(rbCollisionShape *shape)
{
if (shape->cshape->getShapeType() == SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE) {
btBvhTriangleMeshShape *child_shape = ((btScaledBvhTriangleMeshShape *)shape->cshape)->getChildShape();
if (child_shape)
delete child_shape;
}
if (shape->mesh)
delete shape->mesh;
delete shape->cshape;
delete shape;
}
/* Settings --------------------------- */
float RB_shape_get_margin(rbCollisionShape *shape)
{
return shape->cshape->getMargin();
}
void RB_shape_set_margin(rbCollisionShape *shape, float value)
{
shape->cshape->setMargin(value);
}
/* ********************************** */
/* Constraints */
/* Setup ----------------------------- */
void RB_dworld_add_constraint(rbDynamicsWorld *world, rbConstraint *con, int disable_collisions)
{
btTypedConstraint *constraint = reinterpret_cast<btTypedConstraint*>(con);
world->dynamicsWorld->addConstraint(constraint, disable_collisions);
}
void RB_dworld_remove_constraint(rbDynamicsWorld *world, rbConstraint *con)
{
btTypedConstraint *constraint = reinterpret_cast<btTypedConstraint*>(con);
world->dynamicsWorld->removeConstraint(constraint);
}
/* ............ */
static void make_constraint_transforms(btTransform &transform1, btTransform &transform2, btRigidBody *body1, btRigidBody *body2, float pivot[3], float orn[4])
{
btTransform pivot_transform = btTransform();
pivot_transform.setOrigin(btVector3(pivot[0], pivot[1], pivot[2]));
pivot_transform.setRotation(btQuaternion(orn[1], orn[2], orn[3], orn[0]));
transform1 = body1->getWorldTransform().inverse() * pivot_transform;
transform2 = body2->getWorldTransform().inverse() * pivot_transform;
}
rbConstraint *RB_constraint_new_point(float pivot[3], rbRigidBody *rb1, rbRigidBody *rb2)
{
btRigidBody *body1 = rb1->body;
btRigidBody *body2 = rb2->body;
btVector3 pivot1 = body1->getWorldTransform().inverse() * btVector3(pivot[0], pivot[1], pivot[2]);
btVector3 pivot2 = body2->getWorldTransform().inverse() * btVector3(pivot[0], pivot[1], pivot[2]);
btTypedConstraint *con = new btPoint2PointConstraint(*body1, *body2, pivot1, pivot2);
return (rbConstraint *)con;
}
rbConstraint *RB_constraint_new_fixed(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2)
{
btRigidBody *body1 = rb1->body;
btRigidBody *body2 = rb2->body;
btTransform transform1;
btTransform transform2;
make_constraint_transforms(transform1, transform2, body1, body2, pivot, orn);
btGeneric6DofConstraint *con = new btGeneric6DofConstraint(*body1, *body2, transform1, transform2, true);
/* lock all axes */
for (int i = 0; i < 6; i++)
con->setLimit(i, 0, 0);
return (rbConstraint *)con;
}
rbConstraint *RB_constraint_new_hinge(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2)
{
btRigidBody *body1 = rb1->body;
btRigidBody *body2 = rb2->body;
btTransform transform1;
btTransform transform2;
make_constraint_transforms(transform1, transform2, body1, body2, pivot, orn);
btHingeConstraint *con = new btHingeConstraint(*body1, *body2, transform1, transform2);
return (rbConstraint *)con;
}
rbConstraint *RB_constraint_new_slider(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2)
{
btRigidBody *body1 = rb1->body;
btRigidBody *body2 = rb2->body;
btTransform transform1;
btTransform transform2;
make_constraint_transforms(transform1, transform2, body1, body2, pivot, orn);
btSliderConstraint *con = new btSliderConstraint(*body1, *body2, transform1, transform2, true);
return (rbConstraint *)con;
}
rbConstraint *RB_constraint_new_piston(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2)
{
btRigidBody *body1 = rb1->body;
btRigidBody *body2 = rb2->body;
btTransform transform1;
btTransform transform2;
make_constraint_transforms(transform1, transform2, body1, body2, pivot, orn);
btSliderConstraint *con = new btSliderConstraint(*body1, *body2, transform1, transform2, true);
con->setUpperAngLimit(-1.0f); // unlock rotation axis
return (rbConstraint *)con;
}
rbConstraint *RB_constraint_new_6dof(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2)
{
btRigidBody *body1 = rb1->body;
btRigidBody *body2 = rb2->body;
btTransform transform1;
btTransform transform2;
make_constraint_transforms(transform1, transform2, body1, body2, pivot, orn);
btTypedConstraint *con = new btGeneric6DofConstraint(*body1, *body2, transform1, transform2, true);
return (rbConstraint *)con;
}
rbConstraint *RB_constraint_new_6dof_spring(float pivot[3], float orn[4], rbRigidBody *rb1, rbRigidBody *rb2)
{
btRigidBody *body1 = rb1->body;
btRigidBody *body2 = rb2->body;
btTransform transform1;
btTransform transform2;
make_constraint_transforms(transform1, transform2, body1, body2, pivot, orn);
btTypedConstraint *con = new btGeneric6DofSpringConstraint(*body1, *body2, transform1, transform2, true);
return (rbConstraint *)con;
}
/* Cleanup ----------------------------- */
void RB_constraint_delete(rbConstraint *con)
{
btTypedConstraint *constraint = reinterpret_cast<btTypedConstraint*>(con);
delete constraint;
}
/* Settings ------------------------- */
void RB_constraint_set_enabled(rbConstraint *con, int enabled)
{
btTypedConstraint *constraint = reinterpret_cast<btTypedConstraint*>(con);
constraint->setEnabled(enabled);
}
void RB_constraint_set_limits_hinge(rbConstraint *con, float lower, float upper)
{
btHingeConstraint *constraint = reinterpret_cast<btHingeConstraint*>(con);
// RB_TODO expose these
float softness = 0.9f;
float bias_factor = 0.3f;
float relaxation_factor = 1.0f;
constraint->setLimit(lower, upper, softness, bias_factor, relaxation_factor);
}
void RB_constraint_set_limits_slider(rbConstraint *con, float lower, float upper)
{
btSliderConstraint *constraint = reinterpret_cast<btSliderConstraint*>(con);
constraint->setLowerLinLimit(lower);
constraint->setUpperLinLimit(upper);
}
void RB_constraint_set_limits_piston(rbConstraint *con, float lin_lower, float lin_upper, float ang_lower, float ang_upper)
{
btSliderConstraint *constraint = reinterpret_cast<btSliderConstraint*>(con);
constraint->setLowerLinLimit(lin_lower);
constraint->setUpperLinLimit(lin_upper);
constraint->setLowerAngLimit(ang_lower);
constraint->setUpperAngLimit(ang_upper);
}
void RB_constraint_set_limits_6dof(rbConstraint *con, float axis, float lower, float upper)
{
btGeneric6DofConstraint *constraint = reinterpret_cast<btGeneric6DofConstraint*>(con);
constraint->setLimit(axis, lower, upper);
}
void RB_constraint_set_stiffness_6dof_spring(rbConstraint *con, float axis, float stiffness)
{
btGeneric6DofSpringConstraint *constraint = reinterpret_cast<btGeneric6DofSpringConstraint*>(con);
constraint->setStiffness(axis, stiffness);
}
void RB_constraint_set_damping_6dof_spring(rbConstraint *con, float axis, float damping)
{
btGeneric6DofSpringConstraint *constraint = reinterpret_cast<btGeneric6DofSpringConstraint*>(con);
constraint->setDamping(axis, damping);
}
void RB_constraint_set_spring_6dof_spring(rbConstraint *con, float axis, int enable)
{
btGeneric6DofSpringConstraint *constraint = reinterpret_cast<btGeneric6DofSpringConstraint*>(con);
constraint->enableSpring(axis, enable);
}
void RB_constraint_set_equilibrium_6dof_spring(rbConstraint *con)
{
btGeneric6DofSpringConstraint *constraint = reinterpret_cast<btGeneric6DofSpringConstraint*>(con);
constraint->setEquilibriumPoint();
}
void RB_constraint_set_solver_iterations(rbConstraint *con, int num_solver_iterations)
{
btTypedConstraint *constraint = reinterpret_cast<btTypedConstraint*>(con);
constraint->setOverrideNumSolverIterations(num_solver_iterations);
}
void RB_constraint_set_breaking_threshold(rbConstraint *con, float threshold)
{
btTypedConstraint *constraint = reinterpret_cast<btTypedConstraint*>(con);
constraint->setBreakingImpulseThreshold(threshold);
}
/* ********************************** */
|