1 | //
|
---|
2 | // BtPhysTest
|
---|
3 | //
|
---|
4 | // Copyright: (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
|
---|
5 | // (c) 2012 Sam Hocevar <sam@hocevar.net>
|
---|
6 | //
|
---|
7 |
|
---|
8 | #if defined HAVE_CONFIG_H
|
---|
9 | # include "config.h"
|
---|
10 | #endif
|
---|
11 |
|
---|
12 | #if defined _WIN32
|
---|
13 | # include <direct.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #if defined _XBOX
|
---|
17 | # define _USE_MATH_DEFINES /* for M_PI */
|
---|
18 | # include <xtl.h>
|
---|
19 | # undef near /* Fuck Microsoft */
|
---|
20 | # undef far /* Fuck Microsoft again */
|
---|
21 | #elif defined _WIN32
|
---|
22 | # define _USE_MATH_DEFINES /* for M_PI */
|
---|
23 | # define WIN32_LEAN_AND_MEAN
|
---|
24 | # include <windows.h>
|
---|
25 | # undef near /* Fuck Microsoft */
|
---|
26 | # undef far /* Fuck Microsoft again */
|
---|
27 | #else
|
---|
28 | # include <cmath>
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #if USE_SDL && defined __APPLE__
|
---|
32 | # include <SDL_main.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include <bullet/btBulletDynamicsCommon.h>
|
---|
36 | #include <bullet/btBulletCollisionCommon.h>
|
---|
37 |
|
---|
38 | #include "core.h"
|
---|
39 | #include "loldebug.h"
|
---|
40 |
|
---|
41 | using namespace lol;
|
---|
42 |
|
---|
43 | #include "BtPhysTest.h"
|
---|
44 |
|
---|
45 | #define CUBE_HALF_EXTENTS .5f
|
---|
46 | #define EXTRA_HEIGHT 1.f
|
---|
47 |
|
---|
48 | int gNumObjects = 64;
|
---|
49 |
|
---|
50 | BtPhysTest::BtPhysTest(bool editor)
|
---|
51 | {
|
---|
52 | /* Create a camera that matches the settings of XNA BtPhysTest */
|
---|
53 | m_camera = new Camera(vec3(0.f, 600.f, 0.f),
|
---|
54 | vec3(0.f, 0.f, 0.f),
|
---|
55 | vec3(0, 1, 0));
|
---|
56 | m_camera->SetRotation(quat::fromeuler_xyz(0.f, 0.f, 0.f));
|
---|
57 | m_camera->SetPerspective(90.f, 1280.f, 960.f, .1f, 1000.f);
|
---|
58 | //m_camera->SetOrtho(1280.f / 6, 960.f / 6, -1000.f, 1000.f);
|
---|
59 | Ticker::Ref(m_camera);
|
---|
60 |
|
---|
61 | m_ready = false;
|
---|
62 |
|
---|
63 | //init Physics
|
---|
64 | {
|
---|
65 | m_bt_ccd_mode = USE_CCD;
|
---|
66 |
|
---|
67 | //collision configuration contains default setup for memory, collision setup
|
---|
68 | m_bt_collision_config = new btDefaultCollisionConfiguration();
|
---|
69 |
|
---|
70 | //use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
|
---|
71 | m_bt_dispatcher = new btCollisionDispatcher(m_bt_collision_config);
|
---|
72 | m_bt_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,BOX_SHAPE_PROXYTYPE,m_bt_collision_config->getCollisionAlgorithmCreateFunc(CONVEX_SHAPE_PROXYTYPE,CONVEX_SHAPE_PROXYTYPE));
|
---|
73 |
|
---|
74 | m_bt_broadphase = new btDbvtBroadphase();
|
---|
75 |
|
---|
76 | ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
|
---|
77 | m_bt_solver = new btSequentialImpulseConstraintSolver;
|
---|
78 |
|
---|
79 | m_bt_world = new btDiscreteDynamicsWorld(m_bt_dispatcher, m_bt_broadphase, m_bt_solver, m_bt_collision_config);
|
---|
80 | //m_bt_world->setDebugDrawer(&sDebugDrawer);
|
---|
81 | m_bt_world->getSolverInfo().m_splitImpulse = true;
|
---|
82 | m_bt_world->getSolverInfo().m_numIterations = 20;
|
---|
83 |
|
---|
84 | m_bt_world->getDispatchInfo().m_useContinuous = (m_bt_ccd_mode == USE_CCD);
|
---|
85 | m_bt_world->setGravity(btVector3(0,-10,0));
|
---|
86 |
|
---|
87 | ///create a few basic rigid bodies
|
---|
88 | btBoxShape* box = new btBoxShape(btVector3(btScalar(110.),btScalar(1.),btScalar(110.)));
|
---|
89 | btCollisionShape* groundShape = box;
|
---|
90 | m_bt_collision_shapes << groundShape;
|
---|
91 | m_ground_mesh.Compile("[sc#ddd afcb110 1 110 -1]");
|
---|
92 |
|
---|
93 | //m_bt_collision_shapes << new btCylinderShape(btVector3(.5f,.5f,.5f));
|
---|
94 |
|
---|
95 | btTransform groundTransform;
|
---|
96 | groundTransform.setIdentity();
|
---|
97 |
|
---|
98 | //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
|
---|
99 | {
|
---|
100 | btScalar mass(0.);
|
---|
101 |
|
---|
102 | //rigidbody is dynamic if and only if mass is non zero, otherwise static
|
---|
103 | bool isDynamic = (mass != 0.f);
|
---|
104 |
|
---|
105 | btVector3 localInertia(0,0,0);
|
---|
106 | if (isDynamic)
|
---|
107 | groundShape->calculateLocalInertia(mass,localInertia);
|
---|
108 |
|
---|
109 | //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
|
---|
110 | btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
|
---|
111 | btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
|
---|
112 | btRigidBody* body = new btRigidBody(rbInfo);
|
---|
113 |
|
---|
114 | //add the body to the dynamics world
|
---|
115 | m_bt_world->addRigidBody(body);
|
---|
116 | }
|
---|
117 |
|
---|
118 | //Adding Shapes
|
---|
119 | {
|
---|
120 | //create a few dynamic rigidbodies
|
---|
121 | // Re-using the same collision is better for memory usage and performance
|
---|
122 | btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
|
---|
123 | m_rigid_mesh[0].Compile("[sc#daa afcb2 2 2 -.1]");
|
---|
124 | m_rigid_mesh[1].Compile("[sc#ada afcb2 2 2 -.1]");
|
---|
125 | m_rigid_mesh[2].Compile("[sc#aad afcb2 2 2 -.1]");
|
---|
126 |
|
---|
127 | m_bt_collision_shapes << colShape;
|
---|
128 | m_bt_dynamic_shapes << colShape;
|
---|
129 |
|
---|
130 | /// Create Dynamic Objects
|
---|
131 | btTransform startTransform;
|
---|
132 | startTransform.setIdentity();
|
---|
133 | btScalar mass(1.f);
|
---|
134 |
|
---|
135 | //rigidbody is dynamic if and only if mass is non zero, otherwise static
|
---|
136 | bool isDynamic = (mass != 0.f);
|
---|
137 |
|
---|
138 | btVector3 localInertia(0,0,0);
|
---|
139 | if (isDynamic)
|
---|
140 | colShape->calculateLocalInertia(mass,localInertia);
|
---|
141 |
|
---|
142 | int i;
|
---|
143 | for (i=0;i<gNumObjects;i++)
|
---|
144 | {
|
---|
145 | btCollisionShape* shape = colShape;
|
---|
146 | btTransform trans;
|
---|
147 | trans.setIdentity();
|
---|
148 |
|
---|
149 | //stack them
|
---|
150 | int colsize = 10;
|
---|
151 | int row = (i*CUBE_HALF_EXTENTS*2)/(colsize*2*CUBE_HALF_EXTENTS);
|
---|
152 | int row2 = row;
|
---|
153 | int col = (i)%(colsize)-colsize/2;
|
---|
154 |
|
---|
155 | if (col>3)
|
---|
156 | {
|
---|
157 | col=11;
|
---|
158 | row2 |=1;
|
---|
159 | }
|
---|
160 |
|
---|
161 | btVector3 pos(((row+col+row2) % 4)*CUBE_HALF_EXTENTS,
|
---|
162 | 20.0f + row*4*CUBE_HALF_EXTENTS+CUBE_HALF_EXTENTS+EXTRA_HEIGHT,
|
---|
163 | col*3*CUBE_HALF_EXTENTS + (row2%2)*CUBE_HALF_EXTENTS);
|
---|
164 |
|
---|
165 | trans.setOrigin(pos);
|
---|
166 |
|
---|
167 | float mass = 1.f;
|
---|
168 |
|
---|
169 |
|
---|
170 | btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));
|
---|
171 |
|
---|
172 | //rigidbody is dynamic if and only if mass is non zero, otherwise static
|
---|
173 | bool isDynamic = (mass != 0.f);
|
---|
174 |
|
---|
175 | btVector3 localInertia(0,0,0);
|
---|
176 | if (isDynamic)
|
---|
177 | shape->calculateLocalInertia(mass,localInertia);
|
---|
178 |
|
---|
179 | //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
|
---|
180 |
|
---|
181 | btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
|
---|
182 |
|
---|
183 | btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);
|
---|
184 |
|
---|
185 | btRigidBody* body = new btRigidBody(cInfo);
|
---|
186 | body->setContactProcessingThreshold(BT_LARGE_FLOAT);
|
---|
187 |
|
---|
188 | m_bt_world->addRigidBody(body);
|
---|
189 |
|
---|
190 | ///when using m_ccdMode
|
---|
191 | if (m_bt_ccd_mode == USE_CCD)
|
---|
192 | {
|
---|
193 | body->setCcdMotionThreshold(CUBE_HALF_EXTENTS);
|
---|
194 | body->setCcdSweptSphereRadius(0.9*CUBE_HALF_EXTENTS);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | void BtPhysTest::TickGame(float seconds)
|
---|
202 | {
|
---|
203 | WorldEntity::TickGame(seconds);
|
---|
204 |
|
---|
205 | if (Input::GetButtonState(27 /*SDLK_ESCAPE*/))
|
---|
206 | Ticker::Shutdown();
|
---|
207 |
|
---|
208 | ///step the simulation
|
---|
209 | if (m_bt_world)
|
---|
210 | {
|
---|
211 | m_bt_world->stepSimulation(seconds);
|
---|
212 | //optional but useful: debug drawing
|
---|
213 | //m_bt_world->debugDrawWorld();
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | void BtPhysTest::TickDraw(float seconds)
|
---|
218 | {
|
---|
219 | WorldEntity::TickDraw(seconds);
|
---|
220 |
|
---|
221 | if (!m_ready)
|
---|
222 | {
|
---|
223 | m_ground_mesh.MeshConvert();
|
---|
224 | m_rigid_mesh[0].MeshConvert();
|
---|
225 | m_rigid_mesh[1].MeshConvert();
|
---|
226 | m_rigid_mesh[2].MeshConvert();
|
---|
227 | /* FIXME: this object never cleans up */
|
---|
228 | m_ready = true;
|
---|
229 | }
|
---|
230 |
|
---|
231 | Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
|
---|
232 |
|
---|
233 | vec3 BarycenterLocation = vec3(.0f);
|
---|
234 | float BarycenterFactor = 0.0f;
|
---|
235 | for(int i=0;i<gNumObjects;i++)
|
---|
236 | {
|
---|
237 | mat4 m(1.0f);
|
---|
238 | btMatrix3x3 rot; rot.setIdentity();
|
---|
239 | btCollisionObject* colObj = m_bt_world->getCollisionObjectArray()[i];
|
---|
240 | btRigidBody* body = btRigidBody::upcast(colObj);
|
---|
241 | if(body && body->getMotionState())
|
---|
242 | {
|
---|
243 | btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
|
---|
244 | myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(&m[0][0]);
|
---|
245 | rot = myMotionState->m_graphicsWorldTrans.getBasis();
|
---|
246 | }
|
---|
247 | else
|
---|
248 | {
|
---|
249 | colObj->getWorldTransform().getOpenGLMatrix(&m[0][0]);
|
---|
250 | rot = colObj->getWorldTransform().getBasis();
|
---|
251 | }
|
---|
252 | if (i > 0)
|
---|
253 | {
|
---|
254 | BarycenterLocation += m.v3.xyz;
|
---|
255 | BarycenterFactor += 1.0f;
|
---|
256 | }
|
---|
257 | if (i == 0)
|
---|
258 | m_ground_mesh.Render(m);
|
---|
259 | else
|
---|
260 | m_rigid_mesh[i % 3].Render(m);
|
---|
261 | }
|
---|
262 | if (BarycenterFactor > .0f)
|
---|
263 | {
|
---|
264 | BarycenterLocation /= BarycenterFactor;
|
---|
265 |
|
---|
266 | m_camera->SetTarget(BarycenterLocation);
|
---|
267 | m_camera->SetPosition(BarycenterLocation + vec3(-15.0f, 8.0f, .0f));
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | BtPhysTest::~BtPhysTest()
|
---|
272 | {
|
---|
273 | Ticker::Unref(m_camera);
|
---|
274 |
|
---|
275 | //Exit Physics
|
---|
276 | {
|
---|
277 | //cleanup in the reverse order of creation/initialization
|
---|
278 | //remove the rigidbodies from the dynamics world and delete them
|
---|
279 | for (int i = m_bt_world->getNumCollisionObjects() - 1; i >= 0 ;i--)
|
---|
280 | {
|
---|
281 | btCollisionObject* obj = m_bt_world->getCollisionObjectArray()[i];
|
---|
282 | btRigidBody* body = btRigidBody::upcast(obj);
|
---|
283 | if (body && body->getMotionState())
|
---|
284 | delete body->getMotionState();
|
---|
285 |
|
---|
286 | m_bt_world->removeCollisionObject(obj);
|
---|
287 | delete obj;
|
---|
288 | }
|
---|
289 |
|
---|
290 | //delete collision shapes
|
---|
291 | for (int j = 0; j < m_bt_collision_shapes.Count(); j++)
|
---|
292 | {
|
---|
293 | btCollisionShape* shape = m_bt_collision_shapes[j];
|
---|
294 | delete shape;
|
---|
295 | }
|
---|
296 | m_bt_collision_shapes.Empty();
|
---|
297 |
|
---|
298 | delete m_bt_world;
|
---|
299 | delete m_bt_solver;
|
---|
300 | delete m_bt_broadphase;
|
---|
301 | delete m_bt_dispatcher;
|
---|
302 | delete m_bt_collision_config;
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | int main(int argc, char **argv)
|
---|
307 | {
|
---|
308 | Application app("BtPhysTest", ivec2(800, 600), 60.0f);
|
---|
309 |
|
---|
310 | #if defined _MSC_VER && !defined _XBOX
|
---|
311 | _chdir("..");
|
---|
312 | #elif defined _WIN32 && !defined _XBOX
|
---|
313 | _chdir("../..");
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | new BtPhysTest(argc > 1);
|
---|
317 | app.ShowPointer(false);
|
---|
318 |
|
---|
319 | app.Run();
|
---|
320 |
|
---|
321 | return EXIT_SUCCESS;
|
---|
322 | }
|
---|
323 |
|
---|