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