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 | #ifndef HAVE_PHYS_USE_BULLET
|
---|
44 | #define HAVE_PHYS_USE_BULLET
|
---|
45 | #endif /* HAVE_PHYS_USE_BULLET */
|
---|
46 |
|
---|
47 | #include "Physics/LolPhysics.h"
|
---|
48 | #include "Physics/EasyPhysics.h"
|
---|
49 | #include "PhysicObject.h"
|
---|
50 | #include "BtPhysTest.h"
|
---|
51 |
|
---|
52 | using namespace lol::phys;
|
---|
53 |
|
---|
54 | #define CUBE_HALF_EXTENTS .5f
|
---|
55 | #define EXTRA_HEIGHT 1.f
|
---|
56 |
|
---|
57 | int gNumObjects = 64;
|
---|
58 |
|
---|
59 | BtPhysTest::BtPhysTest(bool editor)
|
---|
60 | {
|
---|
61 | /* Create a camera that matches the settings of XNA BtPhysTest */
|
---|
62 | m_camera = new Camera(vec3(0.f, 600.f, 0.f),
|
---|
63 | vec3(0.f, 0.f, 0.f),
|
---|
64 | vec3(0, 1, 0));
|
---|
65 | m_camera->SetRotation(quat::fromeuler_xyz(0.f, 0.f, 0.f));
|
---|
66 | m_camera->SetPerspective(90.f, 1280.f, 960.f, .1f, 1000.f);
|
---|
67 | //m_camera->SetOrtho(1280.f / 6, 960.f / 6, -1000.f, 1000.f);
|
---|
68 | Ticker::Ref(m_camera);
|
---|
69 |
|
---|
70 | m_ready = false;
|
---|
71 |
|
---|
72 | m_simulation = new Simulation();
|
---|
73 | m_simulation->InitContext();
|
---|
74 | vec3 NewGravity = vec3(.0f, -10.0f, .0f);
|
---|
75 | m_simulation->SetGravity(NewGravity);
|
---|
76 |
|
---|
77 | m_ground_object = new PhysicsObject(m_simulation);
|
---|
78 | Ticker::Ref(m_ground_object);
|
---|
79 |
|
---|
80 | for (int x=0; x < 10; x++)
|
---|
81 | {
|
---|
82 | for (int y=0; y < 10; y++)
|
---|
83 | {
|
---|
84 | PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 10.f, vec3(0.f, 20.f, -20.0f) + vec3(.0f, 4.f * (float)y, 4.f * (float)x));
|
---|
85 | m_physobj_list << new_physobj;
|
---|
86 | Ticker::Ref(new_physobj);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | #if 0
|
---|
91 | //init Physics
|
---|
92 | {
|
---|
93 | m_bt_ccd_mode = USE_CCD;
|
---|
94 |
|
---|
95 | //collision configuration contains default setup for memory, collision setup
|
---|
96 | m_bt_collision_config = new btDefaultCollisionConfiguration();
|
---|
97 |
|
---|
98 | //use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
|
---|
99 | m_bt_dispatcher = new btCollisionDispatcher(m_bt_collision_config);
|
---|
100 | m_bt_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,
|
---|
101 | BOX_SHAPE_PROXYTYPE,
|
---|
102 | m_bt_collision_config->getCollisionAlgorithmCreateFunc(CONVEX_SHAPE_PROXYTYPE,
|
---|
103 | CONVEX_SHAPE_PROXYTYPE));
|
---|
104 |
|
---|
105 | m_bt_broadphase = new btDbvtBroadphase();
|
---|
106 |
|
---|
107 | ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
|
---|
108 | m_bt_solver = new btSequentialImpulseConstraintSolver;
|
---|
109 |
|
---|
110 | m_bt_world = new btDiscreteDynamicsWorld(m_bt_dispatcher, m_bt_broadphase, m_bt_solver, m_bt_collision_config);
|
---|
111 | //m_bt_world->setDebugDrawer(&sDebugDrawer);
|
---|
112 | m_bt_world->getSolverInfo().m_splitImpulse = true;
|
---|
113 | m_bt_world->getSolverInfo().m_numIterations = 20;
|
---|
114 |
|
---|
115 | m_bt_world->getDispatchInfo().m_useContinuous = (m_bt_ccd_mode == USE_CCD);
|
---|
116 | m_bt_world->setGravity(btVector3(0,-10,0));
|
---|
117 |
|
---|
118 | ///create a few basic rigid bodies
|
---|
119 | btBoxShape* box = new btBoxShape(btVector3(btScalar(110.),btScalar(1.),btScalar(110.)));
|
---|
120 | btCollisionShape* groundShape = box;
|
---|
121 | m_bt_collision_shapes << groundShape;
|
---|
122 | m_ground_mesh.Compile("[sc#ddd afcb220 2 220 -1]");
|
---|
123 |
|
---|
124 | //m_bt_collision_shapes << new btCylinderShape(btVector3(.5f,.5f,.5f));
|
---|
125 |
|
---|
126 | btTransform groundTransform;
|
---|
127 | groundTransform.setIdentity();
|
---|
128 |
|
---|
129 | //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
|
---|
130 | {
|
---|
131 | btScalar mass(0.);
|
---|
132 |
|
---|
133 | //rigidbody is dynamic if and only if mass is non zero, otherwise static
|
---|
134 | bool isDynamic = (mass != 0.f);
|
---|
135 |
|
---|
136 | btVector3 localInertia(0,0,0);
|
---|
137 | if (isDynamic)
|
---|
138 | groundShape->calculateLocalInertia(mass,localInertia);
|
---|
139 |
|
---|
140 | //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
|
---|
141 | btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
|
---|
142 | btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
|
---|
143 | btRigidBody* body = new btRigidBody(rbInfo);
|
---|
144 |
|
---|
145 | //add the body to the dynamics world
|
---|
146 | m_bt_world->addRigidBody(body);
|
---|
147 | }
|
---|
148 |
|
---|
149 | //Adding Shapes
|
---|
150 | {
|
---|
151 | //create a few dynamic rigidbodies
|
---|
152 | // Re-using the same collision is better for memory usage and performance
|
---|
153 | btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
|
---|
154 | m_rigid_mesh[0].Compile("[sc#add afcb2 2 2 -.1]");
|
---|
155 | m_rigid_mesh[1].Compile("[sc#dad afcb2 2 2 -.1]");
|
---|
156 | m_rigid_mesh[2].Compile("[sc#dda afcb2 2 2 -.1]");
|
---|
157 | m_rigid_mesh[3].Compile("[sc#daa afcb2 2 2 -.1]");
|
---|
158 | m_rigid_mesh[4].Compile("[sc#ada afcb2 2 2 -.1]");
|
---|
159 | m_rigid_mesh[5].Compile("[sc#aad afcb2 2 2 -.1]");
|
---|
160 |
|
---|
161 | m_bt_collision_shapes << colShape;
|
---|
162 | m_bt_dynamic_shapes << colShape;
|
---|
163 |
|
---|
164 | /// Create Dynamic Objects
|
---|
165 | btTransform startTransform;
|
---|
166 | startTransform.setIdentity();
|
---|
167 | btScalar mass(1.f);
|
---|
168 |
|
---|
169 | //rigidbody is dynamic if and only if mass is non zero, otherwise static
|
---|
170 | bool isDynamic = (mass != 0.f);
|
---|
171 |
|
---|
172 | btVector3 localInertia(0,0,0);
|
---|
173 | if (isDynamic)
|
---|
174 | colShape->calculateLocalInertia(mass,localInertia);
|
---|
175 |
|
---|
176 | int i;
|
---|
177 | for (i=0;i<gNumObjects;i++)
|
---|
178 | {
|
---|
179 | btCollisionShape* shape = colShape;
|
---|
180 | btTransform trans;
|
---|
181 | trans.setIdentity();
|
---|
182 |
|
---|
183 | //stack them
|
---|
184 | int colsize = 10;
|
---|
185 | int row = int(((float)i*CUBE_HALF_EXTENTS*2.0f)/((float)colsize*2.0f*CUBE_HALF_EXTENTS));
|
---|
186 | int row2 = row;
|
---|
187 | int col = (i)%(colsize)-colsize/2;
|
---|
188 |
|
---|
189 | if (col>3)
|
---|
190 | {
|
---|
191 | col=11;
|
---|
192 | row2 |=1;
|
---|
193 | }
|
---|
194 |
|
---|
195 | btVector3 pos(((row+col+row2) % 4)*CUBE_HALF_EXTENTS,
|
---|
196 | 20.0f + row*8*CUBE_HALF_EXTENTS+CUBE_HALF_EXTENTS+EXTRA_HEIGHT,
|
---|
197 | col*8*CUBE_HALF_EXTENTS + 2 * (row2%2)*CUBE_HALF_EXTENTS);
|
---|
198 |
|
---|
199 | trans.setOrigin(pos);
|
---|
200 |
|
---|
201 | float mass = 1.f;
|
---|
202 |
|
---|
203 |
|
---|
204 | btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));
|
---|
205 |
|
---|
206 | //rigidbody is dynamic if and only if mass is non zero, otherwise static
|
---|
207 | bool isDynamic = (mass != 0.f);
|
---|
208 |
|
---|
209 | btVector3 localInertia(0,0,0);
|
---|
210 | if (isDynamic)
|
---|
211 | shape->calculateLocalInertia(mass,localInertia);
|
---|
212 |
|
---|
213 | //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
|
---|
214 |
|
---|
215 | btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
|
---|
216 |
|
---|
217 | btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);
|
---|
218 |
|
---|
219 | btRigidBody* body = new btRigidBody(cInfo);
|
---|
220 | body->setContactProcessingThreshold(BT_LARGE_FLOAT);
|
---|
221 |
|
---|
222 | m_bt_world->addRigidBody(body);
|
---|
223 |
|
---|
224 | ///when using m_ccdMode
|
---|
225 | if (m_bt_ccd_mode == USE_CCD)
|
---|
226 | {
|
---|
227 | body->setCcdMotionThreshold(CUBE_HALF_EXTENTS);
|
---|
228 | body->setCcdSweptSphereRadius(0.9*CUBE_HALF_EXTENTS);
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 | #endif
|
---|
234 | }
|
---|
235 |
|
---|
236 | void BtPhysTest::TickGame(float seconds)
|
---|
237 | {
|
---|
238 | WorldEntity::TickGame(seconds);
|
---|
239 |
|
---|
240 | if (Input::GetButtonState(27 /*SDLK_ESCAPE*/))
|
---|
241 | Ticker::Shutdown();
|
---|
242 |
|
---|
243 | m_simulation->TickContext(seconds);
|
---|
244 |
|
---|
245 | m_camera->SetTarget(vec3(.0f));
|
---|
246 | m_camera->SetPosition(vec3(-30.0f, 10.0f, .0f));
|
---|
247 |
|
---|
248 | #if 0
|
---|
249 | ///step the simulation
|
---|
250 | if (m_bt_world)
|
---|
251 | {
|
---|
252 | //int steps = (int)(seconds / 0.005f);
|
---|
253 | //for (int i = 0; i < steps; i++)
|
---|
254 | m_bt_world->stepSimulation(seconds /*/ steps*/);
|
---|
255 | //optional but useful: debug drawing
|
---|
256 | //m_bt_world->debugDrawWorld();
|
---|
257 | }
|
---|
258 | #endif
|
---|
259 | }
|
---|
260 |
|
---|
261 | void BtPhysTest::TickDraw(float seconds)
|
---|
262 | {
|
---|
263 | WorldEntity::TickDraw(seconds);
|
---|
264 |
|
---|
265 | if (!m_ready)
|
---|
266 | {
|
---|
267 | #if 0
|
---|
268 | m_ground_mesh.MeshConvert();
|
---|
269 | m_rigid_mesh[0].MeshConvert();
|
---|
270 | m_rigid_mesh[1].MeshConvert();
|
---|
271 | m_rigid_mesh[2].MeshConvert();
|
---|
272 | m_rigid_mesh[3].MeshConvert();
|
---|
273 | m_rigid_mesh[4].MeshConvert();
|
---|
274 | m_rigid_mesh[5].MeshConvert();
|
---|
275 | #endif
|
---|
276 | /* FIXME: this object never cleans up */
|
---|
277 | m_ready = true;
|
---|
278 | }
|
---|
279 |
|
---|
280 | Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
|
---|
281 |
|
---|
282 | #if 0
|
---|
283 | vec3 BarycenterLocation = vec3(.0f);
|
---|
284 | float BarycenterFactor = 0.0f;
|
---|
285 | for(int i=0;i<gNumObjects;i++)
|
---|
286 | {
|
---|
287 | mat4 m(1.0f);
|
---|
288 | btMatrix3x3 rot; rot.setIdentity();
|
---|
289 | btCollisionObject* colObj = m_bt_world->getCollisionObjectArray()[i];
|
---|
290 | btRigidBody* body = btRigidBody::upcast(colObj);
|
---|
291 | if(body && body->getMotionState())
|
---|
292 | {
|
---|
293 | btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
|
---|
294 | myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(&m[0][0]);
|
---|
295 | rot = myMotionState->m_graphicsWorldTrans.getBasis();
|
---|
296 | }
|
---|
297 | else
|
---|
298 | {
|
---|
299 | colObj->getWorldTransform().getOpenGLMatrix(&m[0][0]);
|
---|
300 | rot = colObj->getWorldTransform().getBasis();
|
---|
301 | }
|
---|
302 | if (i > 0)
|
---|
303 | {
|
---|
304 | BarycenterLocation += m.v3.xyz;
|
---|
305 | BarycenterFactor += 1.0f;
|
---|
306 | }
|
---|
307 | if (i == 0)
|
---|
308 | m_ground_mesh.Render(m);
|
---|
309 | else
|
---|
310 | m_rigid_mesh[i % 6].Render(m);
|
---|
311 | }
|
---|
312 | if (BarycenterFactor > .0f)
|
---|
313 | {
|
---|
314 | BarycenterLocation /= BarycenterFactor;
|
---|
315 |
|
---|
316 | m_camera->SetTarget(BarycenterLocation);
|
---|
317 | m_camera->SetPosition(BarycenterLocation + vec3(-20.0f, 8.0f, .0f));
|
---|
318 | }
|
---|
319 | #endif
|
---|
320 | }
|
---|
321 |
|
---|
322 | BtPhysTest::~BtPhysTest()
|
---|
323 | {
|
---|
324 | Ticker::Unref(m_camera);
|
---|
325 | Ticker::Unref(m_ground_object);
|
---|
326 | while (m_physobj_list.Count())
|
---|
327 | {
|
---|
328 | PhysicsObject* CurPop = m_physobj_list.Last();
|
---|
329 | m_physobj_list.Pop();
|
---|
330 | Ticker::Unref(CurPop);
|
---|
331 | }
|
---|
332 |
|
---|
333 | #if 0
|
---|
334 | //Exit Physics
|
---|
335 | {
|
---|
336 | //cleanup in the reverse order of creation/initialization
|
---|
337 | //remove the rigidbodies from the dynamics world and delete them
|
---|
338 | for (int i = m_bt_world->getNumCollisionObjects() - 1; i >= 0 ;i--)
|
---|
339 | {
|
---|
340 | btCollisionObject* obj = m_bt_world->getCollisionObjectArray()[i];
|
---|
341 | btRigidBody* body = btRigidBody::upcast(obj);
|
---|
342 | if (body && body->getMotionState())
|
---|
343 | delete body->getMotionState();
|
---|
344 |
|
---|
345 | m_bt_world->removeCollisionObject(obj);
|
---|
346 | delete obj;
|
---|
347 | }
|
---|
348 |
|
---|
349 | //delete collision shapes
|
---|
350 | for (int j = 0; j < m_bt_collision_shapes.Count(); j++)
|
---|
351 | {
|
---|
352 | btCollisionShape* shape = m_bt_collision_shapes[j];
|
---|
353 | delete shape;
|
---|
354 | }
|
---|
355 | m_bt_collision_shapes.Empty();
|
---|
356 |
|
---|
357 | delete m_bt_world;
|
---|
358 | delete m_bt_solver;
|
---|
359 | delete m_bt_broadphase;
|
---|
360 | delete m_bt_dispatcher;
|
---|
361 | delete m_bt_collision_config;
|
---|
362 | }
|
---|
363 | #endif
|
---|
364 | }
|
---|
365 |
|
---|
366 | int main(int argc, char **argv)
|
---|
367 | {
|
---|
368 | Application app("BtPhysTest", ivec2(1280, 720), 60.0f);
|
---|
369 |
|
---|
370 | #if defined _MSC_VER && !defined _XBOX
|
---|
371 | _chdir("..");
|
---|
372 | #elif defined _WIN32 && !defined _XBOX
|
---|
373 | _chdir("../..");
|
---|
374 | #endif
|
---|
375 |
|
---|
376 | new BtPhysTest(argc > 1);
|
---|
377 | app.ShowPointer(false);
|
---|
378 |
|
---|
379 | app.Run();
|
---|
380 |
|
---|
381 | return EXIT_SUCCESS;
|
---|
382 | }
|
---|
383 |
|
---|