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 | #include "core.h"
|
---|
32 | #include "loldebug.h"
|
---|
33 |
|
---|
34 | using namespace lol;
|
---|
35 |
|
---|
36 | #ifndef HAVE_PHYS_USE_BULLET
|
---|
37 | #define HAVE_PHYS_USE_BULLET
|
---|
38 | #endif /* HAVE_PHYS_USE_BULLET */
|
---|
39 |
|
---|
40 | #include "Physics/LolPhysics.h"
|
---|
41 | #include "Physics/EasyPhysics.h"
|
---|
42 | #include "PhysicObject.h"
|
---|
43 | #include "BtPhysTest.h"
|
---|
44 |
|
---|
45 | using namespace lol::phys;
|
---|
46 |
|
---|
47 | #define CUBE_HALF_EXTENTS .5f
|
---|
48 | #define EXTRA_HEIGHT 1.f
|
---|
49 |
|
---|
50 | int gNumObjects = 64;
|
---|
51 |
|
---|
52 | #define USE_WALL 1
|
---|
53 | #define USE_PLATFORM 1
|
---|
54 | #define USE_ROPE 1
|
---|
55 | #define USE_BODIES 1
|
---|
56 | #define USE_ROTATION 0
|
---|
57 | #define USE_CHARACTER 1
|
---|
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(45.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->SetWorldLimit(vec3(-1000.0f, -1000.0f, -1000.0f), vec3(1000.0f, 1000.0f, 1000.0f));
|
---|
74 | m_simulation->Init();
|
---|
75 | vec3 NewGravity = vec3(.0f, -10.0f, .0f);
|
---|
76 | m_simulation->SetGravity(NewGravity);
|
---|
77 | m_simulation->SetContinuousDetection(true);
|
---|
78 | m_simulation->SetTimestep(1.f / 120.f);
|
---|
79 | Ticker::Ref(m_simulation);
|
---|
80 |
|
---|
81 | float offset = 29.5f;
|
---|
82 | vec3 pos_offset = vec3(.0f, 30.f, .0f);
|
---|
83 | if (USE_WALL)
|
---|
84 | {
|
---|
85 | for (int i=0; i < 6; i++)
|
---|
86 | {
|
---|
87 | vec3 NewPosition = vec3(.0f);
|
---|
88 | quat NewRotation = quat(1.f);
|
---|
89 |
|
---|
90 | PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation);
|
---|
91 |
|
---|
92 | int idx = i/2;
|
---|
93 | NewPosition = pos_offset;
|
---|
94 | NewPosition[idx] += offset;
|
---|
95 | offset *= -1.f;
|
---|
96 |
|
---|
97 | if (idx != 1)
|
---|
98 | {
|
---|
99 | vec3 axis = vec3(.0f);
|
---|
100 | axis[2 - idx] = 1;
|
---|
101 | NewRotation = quat::rotate(90.f, axis);
|
---|
102 | }
|
---|
103 |
|
---|
104 | NewPhyobj->SetTransform(NewPosition, NewRotation);
|
---|
105 | Ticker::Ref(NewPhyobj);
|
---|
106 | m_ground_list << NewPhyobj;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (USE_PLATFORM)
|
---|
111 | {
|
---|
112 | quat NewRotation = quat::fromeuler_xyz(5.f, 0.f, 0.f);
|
---|
113 | vec3 NewPosition = pos_offset + vec3(5.0f, -25.0f, -15.0f);
|
---|
114 |
|
---|
115 | PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
|
---|
116 |
|
---|
117 | m_platform_list << NewPhyobj;
|
---|
118 | Ticker::Ref(NewPhyobj);
|
---|
119 |
|
---|
120 | NewPosition = pos_offset + vec3(-20.0f, -25.0f, 5.0f);
|
---|
121 |
|
---|
122 | NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
|
---|
123 |
|
---|
124 | m_platform_list << NewPhyobj;
|
---|
125 | Ticker::Ref(NewPhyobj);
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (USE_CHARACTER)
|
---|
129 | {
|
---|
130 | quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
|
---|
131 | vec3 NewPosition = pos_offset + vec3(.0f, 20.0f, .0f);
|
---|
132 |
|
---|
133 | PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 2);
|
---|
134 |
|
---|
135 | m_character_list << NewPhyobj;
|
---|
136 | Ticker::Ref(NewPhyobj);
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (USE_BODIES)
|
---|
140 | {
|
---|
141 | for (int x=0; x < 6; x++)
|
---|
142 | {
|
---|
143 | for (int y=0; y < 6; y++)
|
---|
144 | {
|
---|
145 | for (int z=0; z < 5; z++)
|
---|
146 | {
|
---|
147 | PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
|
---|
148 | vec3(-20.f, 15.f, -20.f) +
|
---|
149 | vec3(8.f * (float)x, 8.f * (float)y, 8.f * (float)z));
|
---|
150 | m_physobj_list << new_physobj;
|
---|
151 | Ticker::Ref(new_physobj);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (USE_ROPE)
|
---|
158 | {
|
---|
159 | Array<PhysicsObject*> RopeElements;
|
---|
160 | for (int i = 0; i < 14; i++)
|
---|
161 | {
|
---|
162 | PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
|
---|
163 | vec3(0.f, 15.f, -20.f) +
|
---|
164 | vec3(0.f, 0.f, 2.f * (float)i), 1);
|
---|
165 | RopeElements << new_physobj;
|
---|
166 | m_physobj_list << new_physobj;
|
---|
167 | Ticker::Ref(new_physobj);
|
---|
168 | if (RopeElements.Count() > 1)
|
---|
169 | {
|
---|
170 | EasyConstraint* new_constraint = new EasyConstraint();
|
---|
171 |
|
---|
172 | vec3 A2B = .5f * (RopeElements[i]->GetPhysic()->GetTransform().v3.xyz -
|
---|
173 | RopeElements[i - 1]->GetPhysic()->GetTransform().v3.xyz);
|
---|
174 | new_constraint->SetPhysObjA(RopeElements[i - 1]->GetPhysic(), lol::mat4::translate(A2B));
|
---|
175 | new_constraint->SetPhysObjB(RopeElements[i]->GetPhysic(), lol::mat4::translate(-A2B));
|
---|
176 | new_constraint->InitConstraintToPoint2Point();
|
---|
177 | new_constraint->DisableCollisionBetweenObjs(true);
|
---|
178 | new_constraint->AddToSimulation(m_simulation);
|
---|
179 | m_constraint_list << new_constraint;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | #if 0
|
---|
185 | //init Physics
|
---|
186 | {
|
---|
187 | m_bt_ccd_mode = USE_CCD;
|
---|
188 |
|
---|
189 | //collision configuration contains default setup for memory, collision setup
|
---|
190 | m_bt_collision_config = new btDefaultCollisionConfiguration();
|
---|
191 |
|
---|
192 | //use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
|
---|
193 | m_bt_dispatcher = new btCollisionDispatcher(m_bt_collision_config);
|
---|
194 | m_bt_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,
|
---|
195 | BOX_SHAPE_PROXYTYPE,
|
---|
196 | m_bt_collision_config->getCollisionAlgorithmCreateFunc(CONVEX_SHAPE_PROXYTYPE,
|
---|
197 | CONVEX_SHAPE_PROXYTYPE));
|
---|
198 |
|
---|
199 | m_bt_broadphase = new btDbvtBroadphase();
|
---|
200 |
|
---|
201 | ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
|
---|
202 | m_bt_solver = new btSequentialImpulseConstraintSolver;
|
---|
203 |
|
---|
204 | m_bt_world = new btDiscreteDynamicsWorld(m_bt_dispatcher, m_bt_broadphase, m_bt_solver, m_bt_collision_config);
|
---|
205 | //m_bt_world->setDebugDrawer(&sDebugDrawer);
|
---|
206 | m_bt_world->getSolverInfo().m_splitImpulse = true;
|
---|
207 | m_bt_world->getSolverInfo().m_numIterations = 20;
|
---|
208 |
|
---|
209 | m_bt_world->getDispatchInfo().m_useContinuous = (m_bt_ccd_mode == USE_CCD);
|
---|
210 | m_bt_world->setGravity(btVector3(0,-10,0));
|
---|
211 |
|
---|
212 | ///create a few basic rigid bodies
|
---|
213 | btBoxShape* box = new btBoxShape(btVector3(btScalar(110.),btScalar(1.),btScalar(110.)));
|
---|
214 | btCollisionShape* groundShape = box;
|
---|
215 | m_bt_collision_shapes << groundShape;
|
---|
216 | m_ground_mesh.Compile("[sc#ddd afcb220 2 220 -1]");
|
---|
217 |
|
---|
218 | //m_bt_collision_shapes << new btCylinderShape(btVector3(.5f,.5f,.5f));
|
---|
219 |
|
---|
220 | btTransform groundTransform;
|
---|
221 | groundTransform.setIdentity();
|
---|
222 |
|
---|
223 | //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
|
---|
224 | {
|
---|
225 | btScalar mass(0.);
|
---|
226 |
|
---|
227 | //rigidbody is dynamic if and only if mass is non zero, otherwise static
|
---|
228 | bool isDynamic = (mass != 0.f);
|
---|
229 |
|
---|
230 | btVector3 localInertia(0,0,0);
|
---|
231 | if (isDynamic)
|
---|
232 | groundShape->calculateLocalInertia(mass,localInertia);
|
---|
233 |
|
---|
234 | //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
|
---|
235 | btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
|
---|
236 | btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
|
---|
237 | btRigidBody* body = new btRigidBody(rbInfo);
|
---|
238 |
|
---|
239 | //add the body to the dynamics world
|
---|
240 | m_bt_world->addRigidBody(body);
|
---|
241 | }
|
---|
242 |
|
---|
243 | //Adding Shapes
|
---|
244 | {
|
---|
245 | //create a few dynamic rigidbodies
|
---|
246 | // Re-using the same collision is better for memory usage and performance
|
---|
247 | btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
|
---|
248 | m_rigid_mesh[0].Compile("[sc#add afcb2 2 2 -.1]");
|
---|
249 | m_rigid_mesh[1].Compile("[sc#dad afcb2 2 2 -.1]");
|
---|
250 | m_rigid_mesh[2].Compile("[sc#dda afcb2 2 2 -.1]");
|
---|
251 | m_rigid_mesh[3].Compile("[sc#daa afcb2 2 2 -.1]");
|
---|
252 | m_rigid_mesh[4].Compile("[sc#ada afcb2 2 2 -.1]");
|
---|
253 | m_rigid_mesh[5].Compile("[sc#aad afcb2 2 2 -.1]");
|
---|
254 |
|
---|
255 | m_bt_collision_shapes << colShape;
|
---|
256 | m_bt_dynamic_shapes << colShape;
|
---|
257 |
|
---|
258 | /// Create Dynamic Objects
|
---|
259 | btTransform startTransform;
|
---|
260 | startTransform.setIdentity();
|
---|
261 | btScalar mass(1.f);
|
---|
262 |
|
---|
263 | //rigidbody is dynamic if and only if mass is non zero, otherwise static
|
---|
264 | bool isDynamic = (mass != 0.f);
|
---|
265 |
|
---|
266 | btVector3 localInertia(0,0,0);
|
---|
267 | if (isDynamic)
|
---|
268 | colShape->calculateLocalInertia(mass,localInertia);
|
---|
269 |
|
---|
270 | int i;
|
---|
271 | for (i=0;i<gNumObjects;i++)
|
---|
272 | {
|
---|
273 | btCollisionShape* shape = colShape;
|
---|
274 | btTransform trans;
|
---|
275 | trans.setIdentity();
|
---|
276 |
|
---|
277 | //stack them
|
---|
278 | int colsize = 10;
|
---|
279 | int row = int(((float)i*CUBE_HALF_EXTENTS*2.0f)/((float)colsize*2.0f*CUBE_HALF_EXTENTS));
|
---|
280 | int row2 = row;
|
---|
281 | int col = (i)%(colsize)-colsize/2;
|
---|
282 |
|
---|
283 | if (col>3)
|
---|
284 | {
|
---|
285 | col=11;
|
---|
286 | row2 |=1;
|
---|
287 | }
|
---|
288 |
|
---|
289 | btVector3 pos(((row+col+row2) % 4)*CUBE_HALF_EXTENTS,
|
---|
290 | 20.0f + row*8*CUBE_HALF_EXTENTS+CUBE_HALF_EXTENTS+EXTRA_HEIGHT,
|
---|
291 | col*8*CUBE_HALF_EXTENTS + 2 * (row2%2)*CUBE_HALF_EXTENTS);
|
---|
292 |
|
---|
293 | trans.setOrigin(pos);
|
---|
294 |
|
---|
295 | float mass = 1.f;
|
---|
296 |
|
---|
297 |
|
---|
298 | btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));
|
---|
299 |
|
---|
300 | //rigidbody is dynamic if and only if mass is non zero, otherwise static
|
---|
301 | bool isDynamic = (mass != 0.f);
|
---|
302 |
|
---|
303 | btVector3 localInertia(0,0,0);
|
---|
304 | if (isDynamic)
|
---|
305 | shape->calculateLocalInertia(mass,localInertia);
|
---|
306 |
|
---|
307 | //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
|
---|
308 |
|
---|
309 | btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
|
---|
310 |
|
---|
311 | btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);
|
---|
312 |
|
---|
313 | btRigidBody* body = new btRigidBody(cInfo);
|
---|
314 | body->setContactProcessingThreshold(BT_LARGE_FLOAT);
|
---|
315 |
|
---|
316 | m_bt_world->addRigidBody(body);
|
---|
317 |
|
---|
318 | ///when using m_ccdMode
|
---|
319 | if (m_bt_ccd_mode == USE_CCD)
|
---|
320 | {
|
---|
321 | body->setCcdMotionThreshold(CUBE_HALF_EXTENTS);
|
---|
322 | body->setCcdSweptSphereRadius(0.9*CUBE_HALF_EXTENTS);
|
---|
323 | }
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 | #endif
|
---|
328 | }
|
---|
329 |
|
---|
330 | void BtPhysTest::TickGame(float seconds)
|
---|
331 | {
|
---|
332 | WorldEntity::TickGame(seconds);
|
---|
333 |
|
---|
334 | if (Input::GetButtonState(27 /*SDLK_ESCAPE*/))
|
---|
335 | Ticker::Shutdown();
|
---|
336 |
|
---|
337 | vec3 GroundBarycenter = vec3(.0f);
|
---|
338 | vec3 PhysObjBarycenter = vec3(.0f);
|
---|
339 | float factor = .0f;
|
---|
340 |
|
---|
341 | if (USE_WALL)
|
---|
342 | {
|
---|
343 | for (int i = 0; i < m_ground_list.Count(); i++)
|
---|
344 | {
|
---|
345 | PhysicsObject* PhysObj = m_ground_list[i];
|
---|
346 | mat4 GroundMat = PhysObj->GetTransform();
|
---|
347 |
|
---|
348 | GroundBarycenter += GroundMat.v3.xyz;
|
---|
349 | factor += 1.f;
|
---|
350 | }
|
---|
351 |
|
---|
352 | GroundBarycenter /= factor;
|
---|
353 |
|
---|
354 | for (int i = 0; i < m_ground_list.Count(); i++)
|
---|
355 | {
|
---|
356 | PhysicsObject* PhysObj = m_ground_list[i];
|
---|
357 |
|
---|
358 | mat4 GroundMat = PhysObj->GetTransform();
|
---|
359 | vec3 CenterToGround = GroundMat.v3.xyz - GroundBarycenter;
|
---|
360 | vec3 CenterToCam = m_camera->m_position - GroundBarycenter;
|
---|
361 |
|
---|
362 | if (dot(normalize(CenterToCam - CenterToGround),
|
---|
363 | normalize(CenterToGround)) > 0.f)
|
---|
364 | PhysObj->SetRender(false);
|
---|
365 | else
|
---|
366 | PhysObj->SetRender(true);
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | if (USE_ROTATION)
|
---|
371 | {
|
---|
372 | for (int i = 0; i < m_ground_list.Count(); i++)
|
---|
373 | {
|
---|
374 | PhysicsObject* PhysObj = m_ground_list[i];
|
---|
375 |
|
---|
376 | mat4 GroundMat = PhysObj->GetTransform();
|
---|
377 | mat4 CenterMx = mat4::translate(GroundBarycenter);
|
---|
378 | GroundMat = inverse(CenterMx) * GroundMat;
|
---|
379 | GroundMat = CenterMx *
|
---|
380 | mat4(quat::fromeuler_xyz(vec3(.0f, 20.f, 20.0f) * seconds))
|
---|
381 | * GroundMat;
|
---|
382 | PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | if (USE_PLATFORM)
|
---|
387 | {
|
---|
388 | for (int i = 0; i < m_platform_list.Count(); i++)
|
---|
389 | {
|
---|
390 | PhysicsObject* PhysObj = m_platform_list[i];
|
---|
391 |
|
---|
392 | mat4 GroundMat = PhysObj->GetTransform();
|
---|
393 | if (i == 0)
|
---|
394 | {
|
---|
395 | GroundMat = GroundMat * mat4(quat::fromeuler_xyz(vec3(20.f, .0f, .0f) * seconds));
|
---|
396 | PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | GroundMat = GroundMat * mat4::translate(vec3(.0f, .0f, 10.0f) * seconds);
|
---|
401 | if (GroundMat.v3.z > 40.0f)
|
---|
402 | GroundMat = GroundMat * mat4::translate(vec3(.0f, .0f, -80.0f));
|
---|
403 | PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | if (USE_CHARACTER)
|
---|
409 | {
|
---|
410 | for (int i = 0; i < m_character_list.Count(); i++)
|
---|
411 | {
|
---|
412 | PhysicsObject* PhysObj = m_character_list[i];
|
---|
413 | EasyCharacterController* Character = (EasyCharacterController*)PhysObj->GetCharacter();
|
---|
414 |
|
---|
415 | int HMovement = Input::GetButtonState(275 /*SDLK_RIGHT*/) - Input::GetButtonState(276 /*SDLK_LEFT*/);
|
---|
416 | int VMovement = Input::GetButtonState(273 /*SDLK_UP*/) - Input::GetButtonState(274 /*SDLK_DOWN*/);
|
---|
417 |
|
---|
418 | Character->SetMovementForFrame(vec3((float)VMovement * seconds * 4.f, .0f, (float)HMovement * seconds * 4.f));
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | if (USE_CHARACTER)
|
---|
423 | {
|
---|
424 | PhysObjBarycenter = vec3(.0f);
|
---|
425 | factor = .0f;
|
---|
426 |
|
---|
427 | for (int i = 0; i < m_character_list.Count(); i++)
|
---|
428 | {
|
---|
429 | PhysicsObject* PhysObj = m_character_list[i];
|
---|
430 | mat4 GroundMat = PhysObj->GetTransform();
|
---|
431 |
|
---|
432 | PhysObjBarycenter += GroundMat.v3.xyz;
|
---|
433 | factor += 1.f;
|
---|
434 | }
|
---|
435 |
|
---|
436 | PhysObjBarycenter /= factor;
|
---|
437 |
|
---|
438 | m_camera->SetTarget(m_camera->GetTarget() + (seconds / (seconds + 0.18f)) * (PhysObjBarycenter - m_camera->GetTarget()));
|
---|
439 | vec3 CamPosCenter = m_camera->GetTarget() + vec3(.0f, 20.0f, .0f);
|
---|
440 | m_camera->SetPosition(CamPosCenter + normalize(m_camera->GetPosition() - CamPosCenter) * 5.0f);
|
---|
441 | }
|
---|
442 | else
|
---|
443 | {
|
---|
444 | PhysObjBarycenter = vec3(.0f);
|
---|
445 | for (int i = 0; i < m_physobj_list.Count(); i++)
|
---|
446 | {
|
---|
447 | PhysicsObject* PhysObj = m_physobj_list[i];
|
---|
448 | mat4 GroundMat = PhysObj->GetTransform();
|
---|
449 |
|
---|
450 | PhysObjBarycenter += GroundMat.v3.xyz;
|
---|
451 | factor += 1.f;
|
---|
452 | }
|
---|
453 |
|
---|
454 | PhysObjBarycenter /= factor;
|
---|
455 |
|
---|
456 | m_camera->SetTarget(PhysObjBarycenter);
|
---|
457 | m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f);
|
---|
458 | }
|
---|
459 |
|
---|
460 | #if 0
|
---|
461 | ///step the simulation
|
---|
462 | if (m_bt_world)
|
---|
463 | {
|
---|
464 | //int steps = (int)(seconds / 0.005f);
|
---|
465 | //for (int i = 0; i < steps; i++)
|
---|
466 | m_bt_world->stepSimulation(seconds /*/ steps*/);
|
---|
467 | //optional but useful: debug drawing
|
---|
468 | //m_bt_world->debugDrawWorld();
|
---|
469 | }
|
---|
470 | #endif
|
---|
471 | }
|
---|
472 |
|
---|
473 | void BtPhysTest::TickDraw(float seconds)
|
---|
474 | {
|
---|
475 | WorldEntity::TickDraw(seconds);
|
---|
476 |
|
---|
477 | if (!m_ready)
|
---|
478 | {
|
---|
479 | #if 0
|
---|
480 | m_ground_mesh.MeshConvert();
|
---|
481 | m_rigid_mesh[0].MeshConvert();
|
---|
482 | m_rigid_mesh[1].MeshConvert();
|
---|
483 | m_rigid_mesh[2].MeshConvert();
|
---|
484 | m_rigid_mesh[3].MeshConvert();
|
---|
485 | m_rigid_mesh[4].MeshConvert();
|
---|
486 | m_rigid_mesh[5].MeshConvert();
|
---|
487 | #endif
|
---|
488 | /* FIXME: this object never cleans up */
|
---|
489 | m_ready = true;
|
---|
490 | }
|
---|
491 |
|
---|
492 | //Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
|
---|
493 |
|
---|
494 | #if 0
|
---|
495 | vec3 BarycenterLocation = vec3(.0f);
|
---|
496 | float BarycenterFactor = 0.0f;
|
---|
497 | for(int i=0;i<gNumObjects;i++)
|
---|
498 | {
|
---|
499 | mat4 m(1.0f);
|
---|
500 | btMatrix3x3 rot; rot.setIdentity();
|
---|
501 | btCollisionObject* colObj = m_bt_world->getCollisionObjectArray()[i];
|
---|
502 | btRigidBody* body = btRigidBody::upcast(colObj);
|
---|
503 | if(body && body->getMotionState())
|
---|
504 | {
|
---|
505 | btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
|
---|
506 | myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(&m[0][0]);
|
---|
507 | rot = myMotionState->m_graphicsWorldTrans.getBasis();
|
---|
508 | }
|
---|
509 | else
|
---|
510 | {
|
---|
511 | colObj->getWorldTransform().getOpenGLMatrix(&m[0][0]);
|
---|
512 | rot = colObj->getWorldTransform().getBasis();
|
---|
513 | }
|
---|
514 | if (i > 0)
|
---|
515 | {
|
---|
516 | BarycenterLocation += m.v3.xyz;
|
---|
517 | BarycenterFactor += 1.0f;
|
---|
518 | }
|
---|
519 | if (i == 0)
|
---|
520 | m_ground_mesh.Render(m);
|
---|
521 | else
|
---|
522 | m_rigid_mesh[i % 6].Render(m);
|
---|
523 | }
|
---|
524 | if (BarycenterFactor > .0f)
|
---|
525 | {
|
---|
526 | BarycenterLocation /= BarycenterFactor;
|
---|
527 |
|
---|
528 | m_camera->SetTarget(BarycenterLocation);
|
---|
529 | m_camera->SetPosition(BarycenterLocation + vec3(-20.0f, 8.0f, .0f));
|
---|
530 | }
|
---|
531 | #endif
|
---|
532 | }
|
---|
533 |
|
---|
534 | BtPhysTest::~BtPhysTest()
|
---|
535 | {
|
---|
536 | Ticker::Unref(m_camera);
|
---|
537 |
|
---|
538 | while (m_constraint_list.Count())
|
---|
539 | {
|
---|
540 | EasyConstraint* CurPop = m_constraint_list.Last();
|
---|
541 | m_constraint_list.Pop();
|
---|
542 | CurPop->RemoveFromSimulation(m_simulation);
|
---|
543 | delete CurPop;
|
---|
544 | }
|
---|
545 | while (m_ground_list.Count())
|
---|
546 | {
|
---|
547 | PhysicsObject* CurPop = m_ground_list.Last();
|
---|
548 | m_ground_list.Pop();
|
---|
549 | CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
|
---|
550 | Ticker::Unref(CurPop);
|
---|
551 | }
|
---|
552 | while (m_character_list.Count())
|
---|
553 | {
|
---|
554 | PhysicsObject* CurPop = m_character_list.Last();
|
---|
555 | m_character_list.Pop();
|
---|
556 | CurPop->GetCharacter()->RemoveFromSimulation(m_simulation);
|
---|
557 | Ticker::Unref(CurPop);
|
---|
558 | }
|
---|
559 | while (m_platform_list.Count())
|
---|
560 | {
|
---|
561 | PhysicsObject* CurPop = m_platform_list.Last();
|
---|
562 | m_platform_list.Pop();
|
---|
563 | CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
|
---|
564 | Ticker::Unref(CurPop);
|
---|
565 | }
|
---|
566 | while (m_physobj_list.Count())
|
---|
567 | {
|
---|
568 | PhysicsObject* CurPop = m_physobj_list.Last();
|
---|
569 | m_physobj_list.Pop();
|
---|
570 | CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
|
---|
571 | Ticker::Unref(CurPop);
|
---|
572 | }
|
---|
573 | Ticker::Unref(m_simulation);
|
---|
574 |
|
---|
575 | #if 0
|
---|
576 | //Exit Physics
|
---|
577 | {
|
---|
578 | //cleanup in the reverse order of creation/initialization
|
---|
579 | //remove the rigidbodies from the dynamics world and delete them
|
---|
580 | for (int i = m_bt_world->getNumCollisionObjects() - 1; i >= 0 ;i--)
|
---|
581 | {
|
---|
582 | btCollisionObject* obj = m_bt_world->getCollisionObjectArray()[i];
|
---|
583 | btRigidBody* body = btRigidBody::upcast(obj);
|
---|
584 | if (body && body->getMotionState())
|
---|
585 | delete body->getMotionState();
|
---|
586 |
|
---|
587 | m_bt_world->removeCollisionObject(obj);
|
---|
588 | delete obj;
|
---|
589 | }
|
---|
590 |
|
---|
591 | //delete collision shapes
|
---|
592 | for (int j = 0; j < m_bt_collision_shapes.Count(); j++)
|
---|
593 | {
|
---|
594 | btCollisionShape* shape = m_bt_collision_shapes[j];
|
---|
595 | delete shape;
|
---|
596 | }
|
---|
597 | m_bt_collision_shapes.Empty();
|
---|
598 |
|
---|
599 | delete m_bt_world;
|
---|
600 | delete m_bt_solver;
|
---|
601 | delete m_bt_broadphase;
|
---|
602 | delete m_bt_dispatcher;
|
---|
603 | delete m_bt_collision_config;
|
---|
604 | }
|
---|
605 | #endif
|
---|
606 | }
|
---|
607 |
|
---|
608 | int main(int argc, char **argv)
|
---|
609 | {
|
---|
610 | Application app("BtPhysTest", ivec2(1280, 720), 60.0f);
|
---|
611 |
|
---|
612 | #if defined _MSC_VER && !defined _XBOX
|
---|
613 | _chdir("..");
|
---|
614 | #elif defined _WIN32 && !defined _XBOX
|
---|
615 | _chdir("../..");
|
---|
616 | #endif
|
---|
617 |
|
---|
618 | new BtPhysTest(argc > 1);
|
---|
619 | app.ShowPointer(false);
|
---|
620 |
|
---|
621 | app.Run();
|
---|
622 |
|
---|
623 | return EXIT_SUCCESS;
|
---|
624 | }
|
---|
625 |
|
---|