-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAPI2.cpp
executable file
·3158 lines (2818 loc) · 104 KB
/
API2.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
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyrightD
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "BulletSim.h"
#include "Util.h"
#include <stdarg.h>
#include "BulletCollision/CollisionDispatch/btSimulationIslandManager.h"
#include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
#include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
#include <map>
#if defined(_WIN32) || defined(_WIN64)
#define DLL_EXPORT __declspec( dllexport )
#define DLL_IMPORT __declspec( dllimport )
#else
#define DLL_EXPORT
#define DLL_IMPORT
#endif
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C extern
#endif
EXTERN_C DLL_EXPORT void DumpConstraint2(BulletSim* sim, btTypedConstraint* constrain);
#pragma warning( disable: 4190 ) // Warning about returning Vector3 that we can safely ignore
// The minimum thickness for terrain. If less than this, we correct
#define TERRAIN_MIN_THICKNESS (0.2)
/**
* Returns a pointer to a string that identifies the version of the BulletSim.dll
* @return pointer to zero terminated static string of format BULLETENGINEVERSION,BULLETSIMVERSION ("3.25,1.3")
*/
EXTERN_C DLL_EXPORT const char* GetVersion2()
{
return BulletSimVersionString.c_str();
}
// DEBUG DEBUG DEBUG =========================================================================================
// USE ONLY FOR VITAL DEBUGGING!!!!
// These are really, really dangerous as they rely on static settings which will only work if there
// is only one instance of BulletSim.
// Put in the log messages and, when done, take them out for release.
static int lastNumberOverlappingPairs;
static BulletSim* staticSim;
static void InitCheckOverlappingPairs(BulletSim* pSim)
{
staticSim = pSim;
lastNumberOverlappingPairs = staticSim->getDynamicsWorld()->getPairCache()->getNumOverlappingPairs();
}
static void CheckOverlappingPairs(char* pReason)
{
int thisOverlapping = staticSim->getDynamicsWorld()->getPairCache()->getNumOverlappingPairs();
if (thisOverlapping != lastNumberOverlappingPairs)
{
btBroadphasePairArray& pairArray = staticSim->getDynamicsWorld()->getPairCache()->getOverlappingPairArray();
int ii = thisOverlapping -1;
staticSim->getWorldData()->BSLog("Pair cache change. old=%d, new=%d, from=%s. Last added id0=%u, id1=%u",
lastNumberOverlappingPairs, thisOverlapping, pReason,
((btCollisionObject*)pairArray[ii].m_pProxy0->m_clientObject)->getUserPointer(),
((btCollisionObject*)pairArray[ii].m_pProxy1->m_clientObject)->getUserPointer());
lastNumberOverlappingPairs = thisOverlapping;
}
}
/*
void __cdecl StaticBSLog(const char* msg, ...)
{
if (staticSim->getWorldData()->debugLogCallback != NULL) {
va_list args;
va_start(args, msg);
staticSim->getWorldData()->BSLog2(msg, args);
va_end(args);
}
}
*/
// END DEBUG DEBUG DEBUG =========================================================================================
/**
* Initializes the physical simulation.
* @param maxPosition Top north-east corner of the simulation, with Z being up. The bottom south-west corner is 0,0,0.
* @param maxCollisions maximum number of collisions that can be reported each tick
* @param updateArray pointer to pinned memory to return the collision info
* @param maxUpdates maximum number of property updates that can be reported each tick
* @param maxCollisions pointer to pinned memory to return the update information
* @return pointer to the created simulator
*/
EXTERN_C DLL_EXPORT BulletSim* Initialize2(Vector3 maxPosition, ParamBlock* parms,
int maxCollisions, CollisionDesc* collisionArray,
int maxUpdates, EntityProperties* updateArray,
DebugLogCallback* debugLog)
{
bsDebug_Initialize();
BulletSim* sim = new BulletSim(maxPosition.X, maxPosition.Y, maxPosition.Z);
sim->getWorldData()->debugLogCallback = debugLog;
sim->initPhysics2(parms, maxCollisions, collisionArray, maxUpdates, updateArray);
return sim;
}
/**
* Update the internal value of a parameter. Some parameters require changing world state.
* @param worldID ID of the world to change the paramter in
* @param localID ID of the object to change the paramter on or special values for NONE or ALL
* @param parm the name of the parameter to change (must be passed in as lower case)
* @param value the value to change the parameter to
*/
EXTERN_C DLL_EXPORT bool UpdateParameter2(BulletSim* sim, unsigned int localID, const char* parm, float value)
{
return sim->UpdateParameter2(localID, parm, value);
}
/**
* Shuts down the physical simulation.
* @param worldID ID of the world to shut down.
*/
EXTERN_C DLL_EXPORT void Shutdown2(BulletSim* sim)
{
sim->exitPhysics2();
bsDebug_AllDone();
delete sim;
}
// Very low level reset of collision proxy pool
EXTERN_C DLL_EXPORT void ResetBroadphasePool(BulletSim* sim)
{
sim->getDynamicsWorld()->getBroadphase()->resetPool(sim->getDynamicsWorld()->getDispatcher());
}
// Very low level reset of the constraint solver
EXTERN_C DLL_EXPORT void ResetConstraintSolver(BulletSim* sim)
{
sim->getDynamicsWorld()->getConstraintSolver()->reset();
}
/**
* Steps the simulation forward a given amount of time and retrieves any physics updates.
* @param worldID ID of the world to step.
* @param timeStep Length of time in seconds to move the simulation forward.
* @param maxSubSteps Clamps the maximum number of fixed duration sub steps taken this step.
* @param fixedTimeStep Length in seconds of the sub steps Bullet actually uses for simulation. Example: 1.0 / TARGET_FPS.
* @param updatedEntityCount Pointer to the number of EntityProperties generated this call.
* @param updatedEntities Pointer to an array of pointers to EntityProperties containing physics updates generated this call.
* @param collidersCount Pointer to the number of colliders detected this call.
* @param colliders Pointer to an array of colliding object IDs (in pairs of two).
* @return Number of sub steps that were taken this call.
*/
EXTERN_C DLL_EXPORT int PhysicsStep2(BulletSim* sim, float timeStep, int maxSubSteps, float fixedTimeStep,
int* updatedEntityCount, int* collidersCount)
{
return sim->PhysicsStep2(timeStep, maxSubSteps, fixedTimeStep, updatedEntityCount, collidersCount);
}
// Cause a position update to happen next physics step.
// This works by placing an entry for this object in the SimMotionState's
// update event array.
EXTERN_C DLL_EXPORT bool PushUpdate2(btCollisionObject* obj)
{
bsDebug_AssertIsKnownCollisionObject(obj, "PushUpdate2: not a known body");
bool ret = false;
btRigidBody* rb = btRigidBody::upcast(obj);
if (rb != NULL)
{
SimMotionState* sms = (SimMotionState*)rb->getMotionState();
if (sms != NULL)
{
btTransform wt;
sms->getWorldTransform(wt);
sms->setWorldTransform(wt, true);
ret = true;
}
}
return ret;
}
// =====================================================================
// Mesh, hull, shape and body creation helper routines
EXTERN_C DLL_EXPORT btCollisionShape* CreateMeshShape2(BulletSim* sim,
int indicesCount, int* indices, int verticesCount, float* vertices )
{
btCollisionShape* shape = sim->CreateMeshShape2(indicesCount, indices, verticesCount, vertices);
bsDebug_RememberCollisionShape(shape);
return shape;
}
EXTERN_C DLL_EXPORT btCollisionShape* CreateGImpactShape2(BulletSim* sim,
int indicesCount, int* indices, int verticesCount, float* vertices )
{
btCollisionShape* shape = sim->CreateGImpactShape2(indicesCount, indices, verticesCount, vertices);
bsDebug_RememberCollisionShape(shape);
return shape;
}
EXTERN_C DLL_EXPORT btCollisionShape* CreateHullShape2(BulletSim* sim,
int hullCount, float* hulls )
{
btCollisionShape* shape = sim->CreateHullShape2(hullCount, hulls);
bsDebug_RememberCollisionShape(shape);
return shape;
}
EXTERN_C DLL_EXPORT btCollisionShape* BuildHullShapeFromMesh2(BulletSim* sim, btCollisionShape* mesh, HACDParams* parms) {
btCollisionShape* shape;
bsDebug_AssertIsKnownCollisionShape(mesh, "BuildHullShapeFromMesh2: unknown shape passed for conversion");
if (parms->whichHACD)
shape = sim->BuildVHACDHullShapeFromMesh2(mesh, parms);
else
shape = sim->BuildHullShapeFromMesh2(mesh, parms);
bsDebug_RememberCollisionShape(shape);
return shape;
}
EXTERN_C DLL_EXPORT btCollisionShape* BuildConvexHullShapeFromMesh2(BulletSim* sim, btCollisionShape* mesh) {
bsDebug_AssertIsKnownCollisionShape(mesh, "BuildConvexHullShapeFromMesh2: unknown shape passed for conversion");
btCollisionShape* shape = sim->BuildConvexHullShapeFromMesh2(mesh);
bsDebug_RememberCollisionShape(shape);
return shape;
}
EXTERN_C DLL_EXPORT btCollisionShape* CreateConvexHullShape2(BulletSim* sim,
int indicesCount, int* indices, int verticesCount, float* vertices )
{
btCollisionShape* shape = sim->CreateConvexHullShape2(indicesCount, indices, verticesCount, vertices);
bsDebug_RememberCollisionShape(shape);
return shape;
}
EXTERN_C DLL_EXPORT btCollisionShape* CreateCompoundShape2(BulletSim* sim, bool enableDynamicAabbTree)
{
btCompoundShape* cShape = new btCompoundShape(enableDynamicAabbTree);
bsDebug_RememberCollisionShape(cShape);
return cShape;
}
EXTERN_C DLL_EXPORT int GetNumberOfCompoundChildren2(btCompoundShape* cShape)
{
return cShape->getNumChildShapes();
}
EXTERN_C DLL_EXPORT void AddChildShapeToCompoundShape2(btCompoundShape* cShape,
btCollisionShape* addShape, Vector3 relativePosition, Quaternion relativeRotation)
{
btTransform relativeTransform(relativeRotation.GetBtQuaternion(), relativePosition.GetBtVector3());
cShape->addChildShape(relativeTransform, addShape);
}
EXTERN_C DLL_EXPORT btCollisionShape* GetChildShapeFromCompoundShapeIndex2(btCompoundShape* cShape, int ii)
{
return cShape->getChildShape(ii);
}
EXTERN_C DLL_EXPORT void RemoveChildShapeFromCompoundShape2(btCompoundShape* cShape, btCollisionShape* removeShape)
{
cShape->removeChildShape(removeShape);
}
EXTERN_C DLL_EXPORT btCollisionShape* RemoveChildShapeFromCompoundShapeIndex2(btCompoundShape* cShape, int ii)
{
btCollisionShape* ret = cShape->getChildShape(ii);
cShape->removeChildShapeByIndex(ii);
return ret;
}
EXTERN_C DLL_EXPORT void RecalculateCompoundShapeLocalAabb2(btCompoundShape* cShape)
{
cShape->recalculateLocalAabb();
}
EXTERN_C DLL_EXPORT void UpdateChildTransform2(btCompoundShape* cShape, int childIndex, Vector3 pos, Quaternion rot, bool shouldRecalculateLocalAabb)
{
btTransform newTrans(rot.GetBtQuaternion(), pos.GetBtVector3());
cShape->updateChildTransform(childIndex, newTrans, shouldRecalculateLocalAabb);
}
EXTERN_C DLL_EXPORT Vector3 GetCompoundChildPosition2(btCompoundShape* cShape, int childIndex)
{
btTransform childTrans = cShape->getChildTransform(childIndex);
return childTrans.getOrigin();
}
EXTERN_C DLL_EXPORT Quaternion GetCompoundChildOrientation2(btCompoundShape* cShape, int childIndex)
{
btTransform childTrans = cShape->getChildTransform(childIndex);
return childTrans.getRotation();
}
EXTERN_C DLL_EXPORT btCollisionShape* BuildNativeShape2(BulletSim* sim, ShapeData shapeData)
{
btCollisionShape* shape = NULL;
switch ((int)shapeData.Type)
{
case ShapeData::SHAPE_BOX:
// btBoxShape subtracts the collision margin from the half extents, so no
// fiddling with scale necessary
// boxes are defined by their half extents
shape = new btBoxShape(btVector3(0.5, 0.5, 0.5)); // this is really a unit box
break;
case ShapeData::SHAPE_CONE: // TODO:
shape = new btConeShapeZ(0.5, 1.0);
break;
case ShapeData::SHAPE_CYLINDER: // TODO:
shape = new btCylinderShapeZ(btVector3(0.5f, 0.5f, 0.5f));
break;
case ShapeData::SHAPE_SPHERE:
shape = new btSphereShape(0.5); // this is really a unit sphere
break;
}
if (shape != NULL)
{
shape->setMargin(btScalar(sim->getWorldData()->params->collisionMargin));
shape->setLocalScaling(shapeData.Scale.GetBtVector3());
bsDebug_RememberCollisionShape(shape);
}
return shape;
}
// Return 'true' if this shape is a Bullet implemented native shape
EXTERN_C DLL_EXPORT bool IsNativeShape2(btCollisionShape* shape)
{
bool ret = false;
bsDebug_AssertIsKnownCollisionShape(shape, "IsNativeShape2: not known shape");
switch (shape->getShapeType())
{
case BOX_SHAPE_PROXYTYPE:
case CONE_SHAPE_PROXYTYPE:
case SPHERE_SHAPE_PROXYTYPE:
case CYLINDER_SHAPE_PROXYTYPE:
ret = true;
break;
default:
ret = false;
break;
}
return ret;
}
EXTERN_C DLL_EXPORT void SetShapeCollisionMargin(btCollisionShape* shape, float margin)
{
bsDebug_AssertIsKnownCollisionShape(obj, "SetShapeCollisonMargin: unknown collisionShape");
shape->setMargin(btScalar(margin));
}
EXTERN_C DLL_EXPORT btCollisionShape* BuildCapsuleShape2(BulletSim* sim, float radius, float height, Vector3 scale)
{
btCollisionShape* shape = new btCapsuleShapeZ(btScalar(radius), btScalar(height));
if (shape)
{
shape->setMargin(sim->getWorldData()->params->collisionMargin);
shape->setLocalScaling(scale.GetBtVector3());
bsDebug_RememberCollisionShape(shape);
}
return shape;
}
// Note: this does not do a deep deletion.
EXTERN_C DLL_EXPORT bool DeleteCollisionShape2(BulletSim* sim, btCollisionShape* shape)
{
bsDebug_AssertIsKnownCollisionShape(shape, "DeleteCollisionShape2: not known shape");
bsDebug_ForgetCollisionShape(shape);
delete shape;
return true;
}
EXTERN_C DLL_EXPORT btCollisionShape* DuplicateCollisionShape2(BulletSim* sim, btCollisionShape* src, unsigned int id)
{
btCollisionShape* newShape = NULL;
bsDebug_AssertIsKnownCollisionShape(shape, "DuplicateCollisionShape2: not known shape");
int shapeType = src->getShapeType();
switch (shapeType)
{
case TRIANGLE_MESH_SHAPE_PROXYTYPE:
{
btBvhTriangleMeshShape* srcTriShape = (btBvhTriangleMeshShape*)src;
newShape = new btBvhTriangleMeshShape(srcTriShape->getMeshInterface(), true, true);
break;
}
/*
case SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE:
{
btScaledBvhTriangleMeshShape* srcTriShape = (btScaledBvhTriangleMeshShape*)src;
newShape = new btScaledBvhTriangleMeshShape(srcTriShape, src->getLocalScaling());
break;
}
*/
case COMPOUND_SHAPE_PROXYTYPE:
{
btCompoundShape* srcCompShape = (btCompoundShape*)src;
btCompoundShape* newCompoundShape = new btCompoundShape(false);
int childCount = srcCompShape->getNumChildShapes();
btCompoundShapeChild* children = srcCompShape->getChildList();
for (int i = 0; i < childCount; i++)
{
btCollisionShape* childShape = children[i].m_childShape;
btTransform childTransform = children[i].m_transform;
newCompoundShape->addChildShape(childTransform, childShape);
}
newShape = newCompoundShape;
break;
}
default:
break;
}
if (newShape != NULL)
{
newShape->setUserPointer(PACKLOCALID(id));
bsDebug_RememberCollisionShape(newShape);
}
return newShape;
}
// Returns a btCollisionObject::CollisionObjectTypes
EXTERN_C DLL_EXPORT int GetBodyType2(btCollisionObject* obj)
{
bsDebug_AssertIsKnownCollisionObject(obj, "GetBodyType2: not known collisionObject");
return obj->getInternalType();
}
// ========================================================================
// Create aa btRigidBody with our MotionState structure so we can track updates to this body.
EXTERN_C DLL_EXPORT btCollisionObject* CreateBodyFromShape2(BulletSim* sim, btCollisionShape* shape,
IDTYPE id, Vector3 pos, Quaternion rot)
{
bsDebug_AssertIsKnownCollisionShape(shape, "CreateBodyFromShape2: unknown collision shape");
btTransform bodyTransform(rot.GetBtQuaternion(), pos.GetBtVector3());
// Use the BulletSim motion state so motion updates will be sent up
SimMotionState* motionState = new SimMotionState(id, bodyTransform, &(sim->getWorldData()->updatesThisFrame));
btRigidBody::btRigidBodyConstructionInfo cInfo(0.0, motionState, shape);
btRigidBody* body = new btRigidBody(cInfo);
motionState->RigidBody = body;
body->setUserPointer(PACKLOCALID(id));
bsDebug_RememberCollisionObject(obj);
return body;
}
// Create a btRigidBody with the default MotionState. We will not get any movement updates from this body.
EXTERN_C DLL_EXPORT btCollisionObject* CreateBodyWithDefaultMotionState2(btCollisionShape* shape,
IDTYPE id, Vector3 pos, Quaternion rot)
{
bsDebug_AssertIsKnownCollisionShape(shape, "CreateBodyWithDefaultMotionState2: unknown collision shape");
btTransform heightfieldTr(rot.GetBtQuaternion(), pos.GetBtVector3());
// Use the default motion state since we are not interested in these
// objects reporting collisions. Other objects will report their
// collisions with the terrain.
btDefaultMotionState* motionState = new btDefaultMotionState(heightfieldTr);
btRigidBody::btRigidBodyConstructionInfo cInfo(0.0, motionState, shape);
btRigidBody* body = new btRigidBody(cInfo);
body->setUserPointer(PACKLOCALID(id));
bsDebug_RememberCollisionObject(body);
return body;
}
// Create a btGhostObject with the passed shape
EXTERN_C DLL_EXPORT btCollisionObject* CreateGhostFromShape2(BulletSim* sim, btCollisionShape* shape,
IDTYPE id, Vector3 pos, Quaternion rot)
{
bsDebug_AssertIsKnownCollisionShape(shape, "CreateGhostFromShape2: unknown collision shape");
btTransform bodyTransform(rot.GetBtQuaternion(), pos.GetBtVector3());
btGhostObject* gObj = new btPairCachingGhostObject();
gObj->setWorldTransform(bodyTransform);
gObj->setCollisionShape(shape);
gObj->setUserPointer(PACKLOCALID(id));
bsDebug_RememberCollisionObject(gObj);
sim->getWorldData()->specialCollisionObjects[id] = gObj;
return gObj;
}
/*
// Create a RigidBody from passed shape and construction info.
// NOTE: it is presumed that a previous RigidBody was saved into the construction info
// and that, in particular, the motionState is a SimMotionState from the saved RigidBody.
// This WILL NOT WORK for terrain bodies.
// This does not restore collisionFlags.
EXTERN_C DLL_EXPORT btCollisionObject* CreateBodyFromShapeAndInfo2(BulletSim* sim, btCollisionShape* shape,
IDTYPE id, btRigidBody::btRigidBodyConstructionInfo* consInfo)
{
bsDebug_AssertIsKnownCollisionShape(shape, "CreateBodyFromShapeAndInfo2: unknown collision shape");
consInfo->m_collisionShape = shape;
btRigidBody* body = new btRigidBody(*consInfo);
// The saved motion state was the SimMotionState saved from before.
((SimMotionState*)consInfo->m_motionState)->RigidBody = body;
body->setUserPointer(PACKLOCALID(id));
bsDebug_RememberCollisionObject(body);
return body;
}
// Build a RigidBody construction info from an existing RigidBody.
// Can be used later to recreate the rigid body.
EXTERN_C DLL_EXPORT btRigidBody::btRigidBodyConstructionInfo* AllocateBodyInfo2(btCollisionObject* obj)
{
btRigidBody::btRigidBodyConstructionInfo* consInfo = NULL;
btRigidBody* rb = btRigidBody::upcast(obj);
if (rb)
{
consInfo = new btRigidBody::btRigidBodyConstructionInfo(
1.0 / rb->getInvMass(), rb->getMotionState(), rb->getCollisionShape() );
consInfo->m_localInertia = btVector3(0.0, 0.0, 0.0);
consInfo->m_linearDamping = rb->getLinearDamping();
consInfo->m_angularDamping = rb->getAngularDamping();
consInfo->m_friction = rb->getFriction();
consInfo->m_restitution = rb->getRestitution();
consInfo->m_linearSleepingThreshold = rb->getLinearSleepingThreshold();
consInfo->m_angularSleepingThreshold = rb->getAngularSleepingThreshold();
// The following are unaccessable but they are usually the default anyway.
// If these are used, they must be later set on the restored RigidBody.
// consInfo->m_additionalDamping = rb->m_additionalDamping;
// consInfo->m_additionalDampingFactor = rb->m_additionalDampingFactor;
// consInfo->m_additionalLinearDampingThresholdSqr = rb->m_additionalLinearDampingThresholdSqr;
// consInfo->m_additionalAngularDampingThresholdSqr = rb->m_additionalAngularDampingThresholdSqr;
// consInfo->m_additionalAngularDampingFactor = rb->m_additionalAngularDampingFactor;
}
return consInfo;
}
// Release the btRigidBodyConstructionInfo created in the above routine.
EXTERN_C DLL_EXPORT void ReleaseBodyInfo2(btRigidBody::btRigidBodyConstructionInfo* consInfo)
{
delete consInfo;
return;
}
*/
/**
* Free all memory allocated to an object. The caller must have previously removed
* the object from the dynamicsWorld.
* @param worldID ID of the world to modify.
* @param id Object ID.
* @return True on success, false if the object was not found.
*/
EXTERN_C DLL_EXPORT void DestroyObject2(BulletSim* sim, btCollisionObject* obj)
{
bsDebug_AssertIsKnownCollisionObject(obj, "DestroyObject2: unknown collisionObject");
btRigidBody* rb = btRigidBody::upcast(obj);
if (rb)
{
// If we added a motionState to the object, delete that
btMotionState* motionState = rb->getMotionState();
if (motionState)
delete motionState;
}
// Delete the rest of the memory allocated to this object
btCollisionShape* shape = obj->getCollisionShape();
if (shape)
{
bsDebug_AssertIsKnownCollisionShape(shape, "DestroyObject2: unknown collisionShape");
bsDebug_ForgetCollisionShape(shape);
delete shape;
}
// Remove from special collision objects. A NOOP if not in the list.
IDTYPE id = CONVLOCALID(obj->getUserPointer());
sim->getWorldData()->specialCollisionObjects.erase(id);
// finally make the object itself go away
bsDebug_ForgetCollisionObject(obj);
delete obj;
}
// =====================================================================
// Terrain creation and helper routines
EXTERN_C DLL_EXPORT btCollisionShape* CreateTerrainShape2(IDTYPE id, Vector3 size, float minHeight, float maxHeight, float* heightMap,
float scaleFactor, float collisionMargin)
{
const int upAxis = 2;
btHeightfieldTerrainShape* terrainShape = new btHeightfieldTerrainShape(
(int)size.X, (int)size.Y, heightMap, (btScalar)scaleFactor,
(btScalar)minHeight, (btScalar)maxHeight, upAxis, PHY_FLOAT, false);
terrainShape->setMargin(btScalar(collisionMargin));
terrainShape->setUseDiamondSubdivision(true);
// Add the localID to the object so we know about collisions
terrainShape->setUserPointer(PACKLOCALID(id));
bsDebug_RememberCollisionShape(terrainShape);
return terrainShape;
}
EXTERN_C DLL_EXPORT btCollisionShape* CreateGroundPlaneShape2(
IDTYPE id,
float height, // usually 1
float collisionMargin)
{
// Initialize the ground plane
btVector3 groundPlaneNormal = btVector3(0, 0, 1); // Z up
btStaticPlaneShape* m_planeShape = new btStaticPlaneShape(groundPlaneNormal, (btScalar)height);
m_planeShape->setMargin(collisionMargin);
m_planeShape->setUserPointer(PACKLOCALID(id));
bsDebug_RememberCollisionShape(m_planeShape);
return m_planeShape;
}
// =====================================================================
// Constraint creation and helper routines
/**
* Add a generic 6 degree of freedom constraint between two previously created objects
* @param sim pointer to BulletSim instance this creation is to be in
* @param id1 first object to constrain
* @param id2 other object to constrain
* @param lowLinear low bounds of linear constraint
* @param hiLinear hi bounds of linear constraint
* @param lowAngular low bounds of angular constraint
* @param hiAngular hi bounds of angular constraint
* @param 'true' if to use FrameA as reference for the constraint action
* @param 'true' if disable collisions between the constrained objects
*/
EXTERN_C DLL_EXPORT btTypedConstraint* Create6DofConstraint2(BulletSim* sim,
btCollisionObject* obj1, btCollisionObject* obj2,
Vector3 frame1loc, Quaternion frame1rot,
Vector3 frame2loc, Quaternion frame2rot,
bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
{
bsDebug_AssertIsKnownCollisionObject(obj1, "Create6DofConstraint2: obj1 unknown CollisionObject");
bsDebug_AssertIsKnownCollisionObject(obj2, "Create6DofConstraint2: obj2 unknown CollisionObject");
bsDebug_AssertNoExistingConstraint(obj1, obj2, "Create6DofConstraint2: constraint exists");
btRigidBody* rb1 = btRigidBody::upcast(obj1);
btRigidBody* rb2 = btRigidBody::upcast(obj2);
btGeneric6DofConstraint* constrain = NULL;
if (rb1 != NULL && rb2 != NULL)
{
btTransform frame1t(frame1rot.GetBtQuaternion(), frame1loc.GetBtVector3());
btTransform frame2t(frame2rot.GetBtQuaternion(), frame2loc.GetBtVector3());
constrain = new btGeneric6DofConstraint(*rb1, *rb2, frame1t, frame2t, useLinearReferenceFrameA);
constrain->calculateTransforms();
sim->getDynamicsWorld()->addConstraint(constrain, disableCollisionsBetweenLinkedBodies);
bsDebug_RememberConstraint(constrain);
// sim->getWorldData()->BSLog("CreateConstraint2: loc=%x, body1=%u, body2=%u", constrain,
// CONVLOCALID(obj1->getCollisionShape()->getUserPointer()),
// CONVLOCALID(obj2->getCollisionShape()->getUserPointer()));
// sim->getWorldData()->BSLog(" f1=<%f,%f,%f>, f1r=<%f,%f,%f,%f>, f2=<%f,%f,%f>, f2r=<%f,%f,%f,%f>",
// frame1loc.X, frame1loc.Y, frame1loc.Z, frame1rot.X, frame1rot.Y, frame1rot.Z, frame1rot.W,
// frame2loc.X, frame2loc.Y, frame2loc.Z, frame2rot.X, frame2rot.Y, frame2rot.Z, frame2rot.W);
}
return constrain;
}
// Create a 6Dof constraint between two objects and around the given world point.
EXTERN_C DLL_EXPORT btTypedConstraint* Create6DofConstraintToPoint2(BulletSim* sim,
btCollisionObject* obj1, btCollisionObject* obj2,
Vector3 joinPoint,
bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
{
bsDebug_AssertIsKnownCollisionObject(obj1, "Create6DofConstraint2: obj1 unknown CollisionObject");
bsDebug_AssertIsKnownCollisionObject(obj2, "Create6DofConstraint2: obj2 unknown CollisionObject");
bsDebug_AssertNoExistingConstraint(obj1, obj2, "Create6DofConstraint2: constraint exists");
btGeneric6DofConstraint* constrain = NULL;
btRigidBody* rb1 = btRigidBody::upcast(obj1);
btRigidBody* rb2 = btRigidBody::upcast(obj2);
if (rb1 != NULL && rb2 != NULL)
{
// following example at http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=5805
btTransform joinPointt, frame1t, frame2t;
joinPointt.setIdentity();
joinPointt.setOrigin(joinPoint.GetBtVector3());
frame1t = rb1->getWorldTransform().inverse() * joinPointt;
frame2t = rb2->getWorldTransform().inverse() * joinPointt;
constrain = new btGeneric6DofConstraint(*rb1, *rb2, frame1t, frame2t, useLinearReferenceFrameA);
sim->getDynamicsWorld()->addConstraint(constrain, disableCollisionsBetweenLinkedBodies);
bsDebug_RememberConstraint(constrain);
}
return constrain;
}
// Create a 6Dof constraint tied to a temporary, static world object
EXTERN_C DLL_EXPORT btTypedConstraint* Create6DofConstraintFixed2(BulletSim* sim,
btCollisionObject* obj1, Vector3 frameInBloc, Quaternion frameInBrot,
bool useLinearReferenceFrameB, bool disableCollisionsBetweenLinkedBodies)
{
bsDebug_AssertIsKnownCollisionObject(obj1, "Create6DofConstraintFixed2: obj1 unknown CollisionObject");
btGeneric6DofConstraint* constrain = NULL;
btRigidBody* rb1 = btRigidBody::upcast(obj1);
if (rb1 != NULL)
{
btTransform frameInB(frameInBrot.GetBtQuaternion(), frameInBloc.GetBtVector3());
constrain = new btGeneric6DofConstraint(*rb1, frameInB, useLinearReferenceFrameB);
sim->getDynamicsWorld()->addConstraint(constrain, disableCollisionsBetweenLinkedBodies);
bsDebug_RememberConstraint(constrain);
}
return constrain;
}
EXTERN_C DLL_EXPORT btTypedConstraint* Create6DofSpringConstraint2(BulletSim* sim,
btCollisionObject* obj1, btCollisionObject* obj2,
Vector3 frame1loc, Quaternion frame1rot,
Vector3 frame2loc, Quaternion frame2rot,
bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
{
bsDebug_AssertIsKnownCollisionObject(obj1, "Create6DofSpringConstraint2: obj1 unknown CollisionObject");
bsDebug_AssertIsKnownCollisionObject(obj2, "Create6DofSpringConstraint2: obj2 unknown CollisionObject");
bsDebug_AssertNoExistingConstraint(obj1, obj2, "Create6DofSpringConstraint2: constraint exists");
btRigidBody* rb1 = btRigidBody::upcast(obj1);
btRigidBody* rb2 = btRigidBody::upcast(obj2);
btGeneric6DofSpringConstraint* constrain = NULL;
if (rb1 != NULL && rb2 != NULL)
{
btTransform frame1t(frame1rot.GetBtQuaternion(), frame1loc.GetBtVector3());
btTransform frame2t(frame2rot.GetBtQuaternion(), frame2loc.GetBtVector3());
constrain = new btGeneric6DofSpringConstraint(*rb1, *rb2, frame1t, frame2t, useLinearReferenceFrameA);
sim->getWorldData()->BSLog("Create6DofSpringConstraint2 ++++++++++++");
DumpConstraint2(sim, constrain);
constrain->calculateTransforms();
sim->getDynamicsWorld()->addConstraint(constrain, disableCollisionsBetweenLinkedBodies);
bsDebug_RememberConstraint(constrain);
}
return constrain;
}
EXTERN_C DLL_EXPORT btTypedConstraint* CreateHingeConstraint2(BulletSim* sim,
btCollisionObject* obj1, btCollisionObject* obj2,
Vector3 pivotInA, Vector3 pivotInB,
Vector3 axisInA, Vector3 axisInB,
bool useReferenceFrameA,
bool disableCollisionsBetweenLinkedBodies
)
{
bsDebug_AssertIsKnownCollisionObject(obj1, "CreateHingeConstraint2: obj1 unknown CollisionObject");
bsDebug_AssertIsKnownCollisionObject(obj2, "CreateHingeConstraint2: obj2 unknown CollisionObject");
bsDebug_AssertNoExistingConstraint(obj1, obj2, "CreateHingeConstraint2: constraint exists");
btHingeConstraint* constrain = NULL;
btRigidBody* rb1 = btRigidBody::upcast(obj1);
btRigidBody* rb2 = btRigidBody::upcast(obj2);
if (rb1 != NULL && rb2 != NULL)
{
constrain = new btHingeConstraint(*rb1, *rb2,
pivotInA.GetBtVector3(), pivotInB.GetBtVector3(),
axisInA.GetBtVector3(), axisInB.GetBtVector3(),
useReferenceFrameA);
sim->getDynamicsWorld()->addConstraint(constrain, disableCollisionsBetweenLinkedBodies);
bsDebug_RememberConstraint(constrain);
}
return constrain;
}
EXTERN_C DLL_EXPORT btTypedConstraint* CreateSliderConstraint2(BulletSim* sim,
btCollisionObject* obj1, btCollisionObject* obj2,
Vector3 frame1loc, Quaternion frame1rot,
Vector3 frame2loc, Quaternion frame2rot,
bool useLinearReferenceFrameA, bool disableCollisionsBetweenLinkedBodies)
{
bsDebug_AssertIsKnownCollisionObject(obj1, "CreateSliderConstraint2: obj1 unknown CollisionObject");
bsDebug_AssertIsKnownCollisionObject(obj2, "CreateSliderConstraint2: obj2 unknown CollisionObject");
bsDebug_AssertNoExistingConstraint(obj1, obj2, "CreateSliderConstraint2: constraint exists");
btRigidBody* rb1 = btRigidBody::upcast(obj1);
btRigidBody* rb2 = btRigidBody::upcast(obj2);
btSliderConstraint* constrain = NULL;
if (rb1 != NULL && rb2 != NULL)
{
btTransform frame1t(frame1rot.GetBtQuaternion(), frame1loc.GetBtVector3());
btTransform frame2t(frame2rot.GetBtQuaternion(), frame2loc.GetBtVector3());
constrain = new btSliderConstraint(*rb1, *rb2, frame1t, frame2t, useLinearReferenceFrameA);
sim->getDynamicsWorld()->addConstraint(constrain, disableCollisionsBetweenLinkedBodies);
bsDebug_RememberConstraint(constrain);
}
return constrain;
}
EXTERN_C DLL_EXPORT btTypedConstraint* CreateConeTwistConstraint2(BulletSim* sim,
btCollisionObject* obj1, btCollisionObject* obj2,
Vector3 frame1loc, Quaternion frame1rot,
Vector3 frame2loc, Quaternion frame2rot,
bool disableCollisionsBetweenLinkedBodies)
{
bsDebug_AssertIsKnownCollisionObject(obj1, "CreateConeTwistConstraint2: obj1 unknown CollisionObject");
bsDebug_AssertIsKnownCollisionObject(obj2, "CreateConeTwistConstraint2: obj2 unknown CollisionObject");
bsDebug_AssertNoExistingConstraint(obj1, obj2, "CreateConeTwistConstraint2: constraint exists");
btRigidBody* rb1 = btRigidBody::upcast(obj1);
btRigidBody* rb2 = btRigidBody::upcast(obj2);
btConeTwistConstraint* constrain = NULL;
if (rb1 != NULL && rb2 != NULL)
{
btTransform frame1t(frame1rot.GetBtQuaternion(), frame1loc.GetBtVector3());
btTransform frame2t(frame2rot.GetBtQuaternion(), frame2loc.GetBtVector3());
constrain = new btConeTwistConstraint(*rb1, *rb2, frame1t, frame2t);
sim->getDynamicsWorld()->addConstraint(constrain, disableCollisionsBetweenLinkedBodies);
bsDebug_RememberConstraint(constrain);
}
return constrain;
}
EXTERN_C DLL_EXPORT btTypedConstraint* CreateGearConstraint2(BulletSim* sim,
btCollisionObject* obj1, btCollisionObject* obj2,
Vector3 axisInA, Vector3 axisInB,
Vector3 frame2loc, Quaternion frame2rot,
float ratio, bool disableCollisionsBetweenLinkedBodies)
{
bsDebug_AssertIsKnownCollisionObject(obj1, "CreateGearConstraint2: obj1 unknown CollisionObject");
bsDebug_AssertIsKnownCollisionObject(obj2, "CreateGearConstraint2: obj2 unknown CollisionObject");
bsDebug_AssertNoExistingConstraint(obj1, obj2, "CreateGearConstraint2: constraint exists");
btRigidBody* rb1 = btRigidBody::upcast(obj1);
btRigidBody* rb2 = btRigidBody::upcast(obj2);
btGearConstraint* constrain = NULL;
if (rb1 != NULL && rb2 != NULL)
{
constrain = new btGearConstraint(*rb1, *rb2, axisInA.GetBtVector3(), axisInB.GetBtVector3(), ratio);
sim->getDynamicsWorld()->addConstraint(constrain, disableCollisionsBetweenLinkedBodies);
bsDebug_RememberConstraint(constrain);
}
return constrain;
}
EXTERN_C DLL_EXPORT btTypedConstraint* CreatePoint2PointConstraint2(BulletSim* sim,
btCollisionObject* obj1, btCollisionObject* obj2,
Vector3 pivotInA, Vector3 pivotInB,
bool disableCollisionsBetweenLinkedBodies)
{
bsDebug_AssertIsKnownCollisionObject(obj1, "CreatePoint2PointConstraint2: obj1 unknown CollisionObject");
bsDebug_AssertIsKnownCollisionObject(obj2, "CreatePoint2PointConstraint2: obj2 unknown CollisionObject");
bsDebug_AssertNoExistingConstraint(obj1, obj2, "CreatePoint2PointConstraint2: constraint exists");
btRigidBody* rb1 = btRigidBody::upcast(obj1);
btRigidBody* rb2 = btRigidBody::upcast(obj2);
btPoint2PointConstraint* constrain = NULL;
if (rb1 != NULL && rb2 != NULL)
{
constrain = new btPoint2PointConstraint(*rb1, *rb2, pivotInA.GetBtVector3(), pivotInB.GetBtVector3());
sim->getDynamicsWorld()->addConstraint(constrain, disableCollisionsBetweenLinkedBodies);
bsDebug_RememberConstraint(constrain);
}
return constrain;
}
EXTERN_C DLL_EXPORT bool SetFrames2(btTypedConstraint* constrain,
Vector3 frameA, Quaternion frameArot, Vector3 frameB, Quaternion frameBrot)
{
bool ret = false;
bsDebug_AssertIsKnownConstraint(constrain, "SetFrame2: unknown constraint");
btTransform transA(frameArot.GetBtQuaternion(), frameA.GetBtVector3());
btTransform transB(frameBrot.GetBtQuaternion(), frameB.GetBtVector3());
switch (constrain->getConstraintType())
{
case POINT2POINT_CONSTRAINT_TYPE:
break;
case HINGE_CONSTRAINT_TYPE:
{
btHingeConstraint* cc = (btHingeConstraint*)constrain;
cc->setFrames(transA, transB);
ret = true;
break;
}
case CONETWIST_CONSTRAINT_TYPE:
{
btConeTwistConstraint* cc = (btConeTwistConstraint*)constrain;
cc->setFrames(transA, transB);
ret = true;
break;
}
case D6_CONSTRAINT_TYPE:
{
btGeneric6DofConstraint* cc = (btGeneric6DofConstraint*)constrain;
cc->setFrames(transA, transB);
ret = true;
break;
}
case SLIDER_CONSTRAINT_TYPE:
{
btSliderConstraint* cc = (btSliderConstraint*)constrain;
cc->setFrames(transA, transB);
ret = true;
break;
}
case CONTACT_CONSTRAINT_TYPE:
break;
case D6_SPRING_CONSTRAINT_TYPE:
{
btGeneric6DofSpringConstraint* cc = (btGeneric6DofSpringConstraint*)constrain;
cc->setFrames(transA, transB);
ret = true;
break;
}
case GEAR_CONSTRAINT_TYPE:
break;
default:
break;
}
return ret;
}
EXTERN_C DLL_EXPORT void SetConstraintEnable2(btTypedConstraint* constrain, float trueFalse)
{
bsDebug_AssertIsKnownConstraint(constrain, "SetConstraintEnable2: unknown constraint");
constrain->setEnabled(trueFalse == ParamTrue ? true : false);
}
EXTERN_C DLL_EXPORT void SetConstraintNumSolverIterations2(btTypedConstraint* constrain, float iterations)
{
bsDebug_AssertIsKnownConstraint(constrain, "SetConstraintNumSolverIterations2: unknown constraint");
constrain->setOverrideNumSolverIterations((int)iterations);
}
EXTERN_C DLL_EXPORT bool SetLinearLimits2(btTypedConstraint* constrain, Vector3 low, Vector3 high)
{
bool ret = false;
bsDebug_AssertIsKnownConstraint(constrain, "SetLinearLimits2: unknown constraint");
switch (constrain->getConstraintType())
{
case POINT2POINT_CONSTRAINT_TYPE:
break;
case HINGE_CONSTRAINT_TYPE: