1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2015 Google Inc. http://bulletphysics.org
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #include "CompoundBoxes.h"
17
18 #include "btBulletDynamicsCommon.h"
19 #include "LinearMath/btVector3.h"
20 #include "LinearMath/btAlignedObjectArray.h"
21 #include "../CommonInterfaces/CommonRigidBodyBase.h"
22
23 struct CompoundBoxesExample : public CommonRigidBodyBase
24 {
CompoundBoxesExampleCompoundBoxesExample25 CompoundBoxesExample(struct GUIHelperInterface* helper)
26 : CommonRigidBodyBase(helper)
27 {
28 }
~CompoundBoxesExampleCompoundBoxesExample29 virtual ~CompoundBoxesExample() {}
30 virtual void initPhysics();
31 virtual void renderScene();
resetCameraCompoundBoxesExample32 void resetCamera()
33 {
34 float dist = 41;
35 float pitch = -35;
36 float yaw = 52;
37 float targetPos[3] = {0, 0.46, 0};
38 m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
39 }
40 };
41
initPhysics()42 void CompoundBoxesExample::initPhysics()
43 {
44 m_guiHelper->setUpAxis(1);
45
46 createEmptyDynamicsWorld();
47
48 m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
49
50 if (m_dynamicsWorld->getDebugDrawer())
51 m_dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe + btIDebugDraw::DBG_DrawContactPoints);
52
53 ///create a few basic rigid bodies
54 btBoxShape* groundShape = createBoxShape(btVector3(btScalar(50.), btScalar(50.), btScalar(50.)));
55 m_collisionShapes.push_back(groundShape);
56
57 btTransform groundTransform;
58 groundTransform.setIdentity();
59 groundTransform.setOrigin(btVector3(0, -50, 0));
60 {
61 btScalar mass(0.);
62 createRigidBody(mass, groundTransform, groundShape, btVector4(0, 0, 1, 1));
63 }
64
65 {
66 //create a few dynamic rigidbodies
67 // Re-using the same collision is better for memory usage and performance
68 btBoxShape* cube = createBoxShape(btVector3(0.5, 0.5, 0.5));
69 m_collisionShapes.push_back(cube);
70
71 // create a new compound shape for making an L-beam from `cube`s
72 btCompoundShape* compoundShape = new btCompoundShape();
73
74 btTransform transform;
75
76 // add cubes in an L-beam fashion to the compound shape
77 transform.setIdentity();
78 transform.setOrigin(btVector3(0, 0, 0));
79 compoundShape->addChildShape(transform, cube);
80
81 transform.setIdentity();
82 transform.setOrigin(btVector3(0, -1, 0));
83 compoundShape->addChildShape(transform, cube);
84
85 transform.setIdentity();
86 transform.setOrigin(btVector3(0, 0, 1));
87 compoundShape->addChildShape(transform, cube);
88
89 btScalar masses[3] = {1, 1, 1};
90 btTransform principal;
91 btVector3 inertia;
92 compoundShape->calculatePrincipalAxisTransform(masses, principal, inertia);
93
94 // new compund shape to store
95 btCompoundShape* compound2 = new btCompoundShape();
96 m_collisionShapes.push_back(compound2);
97 #if 0
98 // less efficient way to add the entire compund shape
99 // to a new compund shape as a child
100 compound2->addChildShape(principal.inverse(), compoundShape);
101 #else
102 // recompute the shift to make sure the compound shape is re-aligned
103 for (int i = 0; i < compoundShape->getNumChildShapes(); i++)
104 compound2->addChildShape(compoundShape->getChildTransform(i) * principal.inverse(),
105 compoundShape->getChildShape(i));
106 #endif
107 delete compoundShape;
108
109 transform.setIdentity();
110 transform.setOrigin(btVector3(0, 10, 0));
111 createRigidBody(1.0, transform, compound2);
112 }
113
114 m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
115 }
116
renderScene()117 void CompoundBoxesExample::renderScene()
118 {
119 CommonRigidBodyBase::renderScene();
120 }
121
ET_CompoundBoxesCreateFunc(CommonExampleOptions & options)122 CommonExampleInterface* ET_CompoundBoxesCreateFunc(CommonExampleOptions& options)
123 {
124 return new CompoundBoxesExample(options.m_guiHelper);
125 }
126