1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2007 Erwin Coumans https://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 ///-----includes_start-----
17 #include "btBulletDynamicsCommon.h"
18 #include <stdio.h>
19
20 /// This is a Hello World program for running a basic Bullet physics simulation
21
main(int argc,char ** argv)22 int main(int argc, char** argv)
23 {
24 ///-----includes_end-----
25
26 int i;
27 ///-----initialization_start-----
28
29 ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
30 btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
31
32 ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
33 btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
34
35 ///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
36 btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
37
38 ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
39 btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
40
41 btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
42
43 dynamicsWorld->setGravity(btVector3(0, -10, 0));
44
45 ///-----initialization_end-----
46
47 //keep track of the shapes, we release memory at exit.
48 //make sure to re-use collision shapes among rigid bodies whenever possible!
49 btAlignedObjectArray<btCollisionShape*> collisionShapes;
50
51 ///create a few basic rigid bodies
52
53 //the ground is a cube of side 100 at position y = -56.
54 //the sphere will hit it at y = -6, with center at -5
55 {
56 btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.), btScalar(50.), btScalar(50.)));
57
58 collisionShapes.push_back(groundShape);
59
60 btTransform groundTransform;
61 groundTransform.setIdentity();
62 groundTransform.setOrigin(btVector3(0, -56, 0));
63
64 btScalar mass(0.);
65
66 //rigidbody is dynamic if and only if mass is non zero, otherwise static
67 bool isDynamic = (mass != 0.f);
68
69 btVector3 localInertia(0, 0, 0);
70 if (isDynamic)
71 groundShape->calculateLocalInertia(mass, localInertia);
72
73 //using motionstate is optional, it provides interpolation capabilities, and only synchronizes 'active' objects
74 btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
75 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, groundShape, localInertia);
76 btRigidBody* body = new btRigidBody(rbInfo);
77
78 //add the body to the dynamics world
79 dynamicsWorld->addRigidBody(body);
80 }
81
82 {
83 //create a dynamic rigidbody
84
85 //btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
86 btCollisionShape* colShape = new btSphereShape(btScalar(1.));
87 collisionShapes.push_back(colShape);
88
89 /// Create Dynamic Objects
90 btTransform startTransform;
91 startTransform.setIdentity();
92
93 btScalar mass(1.f);
94
95 //rigidbody is dynamic if and only if mass is non zero, otherwise static
96 bool isDynamic = (mass != 0.f);
97
98 btVector3 localInertia(0, 0, 0);
99 if (isDynamic)
100 colShape->calculateLocalInertia(mass, localInertia);
101
102 startTransform.setOrigin(btVector3(2, 10, 0));
103
104 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
105 btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
106 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, colShape, localInertia);
107 btRigidBody* body = new btRigidBody(rbInfo);
108
109 dynamicsWorld->addRigidBody(body);
110 }
111
112 /// Do some simulation
113
114 ///-----stepsimulation_start-----
115 for (i = 0; i < 150; i++)
116 {
117 dynamicsWorld->stepSimulation(1.f / 60.f, 10);
118
119 //print positions of all objects
120 for (int j = dynamicsWorld->getNumCollisionObjects() - 1; j >= 0; j--)
121 {
122 btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
123 btRigidBody* body = btRigidBody::upcast(obj);
124 btTransform trans;
125 if (body && body->getMotionState())
126 {
127 body->getMotionState()->getWorldTransform(trans);
128 }
129 else
130 {
131 trans = obj->getWorldTransform();
132 }
133 printf("world pos object %d = %f,%f,%f\n", j, float(trans.getOrigin().getX()), float(trans.getOrigin().getY()), float(trans.getOrigin().getZ()));
134 }
135 }
136
137 ///-----stepsimulation_end-----
138
139 //cleanup in the reverse order of creation/initialization
140
141 ///-----cleanup_start-----
142
143 //remove the rigidbodies from the dynamics world and delete them
144 for (i = dynamicsWorld->getNumCollisionObjects() - 1; i >= 0; i--)
145 {
146 btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];
147 btRigidBody* body = btRigidBody::upcast(obj);
148 if (body && body->getMotionState())
149 {
150 delete body->getMotionState();
151 }
152 dynamicsWorld->removeCollisionObject(obj);
153 delete obj;
154 }
155
156 //delete collision shapes
157 for (int j = 0; j < collisionShapes.size(); j++)
158 {
159 btCollisionShape* shape = collisionShapes[j];
160 collisionShapes[j] = 0;
161 delete shape;
162 }
163
164 //delete dynamics world
165 delete dynamicsWorld;
166
167 //delete solver
168 delete solver;
169
170 //delete broadphase
171 delete overlappingPairCache;
172
173 //delete dispatcher
174 delete dispatcher;
175
176 delete collisionConfiguration;
177
178 //next line is optional: it will be cleared by the destructor when the array goes out of scope
179 collisionShapes.clear();
180 }
181