2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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:
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.
18 ///////////////////////////////////////////////////////////////////////
21 #include "LinearMath/btScalar.h"
22 #include "LinearMath/btMinMax.h"
24 #include "DemoApplication.h"
25 #include "DemoEntries.h"
26 #include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
28 #include "GLDebugDrawer.h"
30 #include "LinearMath/btQuickprof.h"
33 ///////////////////////////////////////////////////////////////////////
38 static NSPoint MousePosition;
39 static NSSize ViewSize;
40 static int NumIterations = 10;
41 static BOOL DisableDeactivation = NO;
42 static BOOL DrawAABBs = NO;
43 static BOOL DebugDraw = NO;
44 static BOOL SplitImpulse = NO;
45 static BOOL DrawContacts = NO;
47 ///////////////////////////////////////////////////////////////////////
50 #pragma mark BTDemoEntry
52 @interface BTDemoEntry : NSObject {
54 DemoApplication *demo;
57 - (id) initWithDemoEntry: (btDemoEntry*) constructor;
58 - (DemoApplication*) demo;
63 @implementation BTDemoEntry
65 - (id) initWithDemoEntry: (btDemoEntry*) constructor
67 if ( self = [super init] )
78 if ( demo ) delete demo;
82 - (DemoApplication*) demo
91 if (demo->getDynamicsWorld()->getDebugDrawer())
92 delete demo->getDynamicsWorld()->getDebugDrawer();
98 demo = ctor->createFcn();
101 if (demo->getDynamicsWorld())
103 demo->getDynamicsWorld()->setDebugDrawer(new GLDebugDrawer());
106 #ifndef BT_NO_PROFILE
107 CProfileManager::Reset();
108 #endif //BT_NO_PROFILE
113 ///////////////////////////////////////////////////////////////////////
119 @implementation BTDemo
121 + (NSArray*) demoNames
123 static NSMutableArray *DEMOS = nil;
126 DEMOS = [[NSMutableArray alloc] init];
128 btDemoEntry* e = g_demoEntries;
131 [DEMOS addObject: [NSString stringWithUTF8String: e->name]];
139 + (BOOL) isDemo: (NSString *) demoName
141 return [[self demoNames] containsObject: demoName];
144 + (BTDemo*) demoWithName: (NSString *) demoName
146 if ( [BTDemo isDemo: demoName] )
148 return [[[BTDemo alloc] initWithDemoName: demoName] autorelease];
154 - (id) initWithDemoName: (NSString *) demoName
156 if ( self = [super init] )
158 _demoName = [demoName copy];
160 // now walk the constructor list and find this demo
162 btDemoEntry* e = g_demoEntries;
165 NSString *name = [NSString stringWithCString: e->name];
166 if ( [name isEqualToString: demoName] )
168 _demo = [[BTDemoEntry alloc] initWithDemoEntry: e];
190 [self contextWillResize];
191 [self contextResized: ViewSize];
192 [self contextDidResize];
195 - (NSString*) demoName
201 #pragma mark BTOpenGLDisplayDelegate
203 - (void) contextCreated
206 - (void) contextWillBeDestroyed
209 - (void) contextWillResize
212 - (void) contextResized: (NSSize) newSize
215 glViewport( 0, 0, (int) newSize.width, (int) newSize.height );
217 glMatrixMode(GL_PROJECTION);
220 [_demo demo]->reshape( (int) newSize.width, (int) newSize.height );
223 - (void) contextDidResize
226 - (void) contextStateInvalidated
229 - (void) display: (float) deltaT
231 DemoApplication *demo = [_demo demo];
235 glClearColor( 1, 0.5, 1, 1 );
236 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
240 if (demo->getDynamicsWorld())
244 demo->getDynamicsWorld()->getSolverInfo().m_splitImpulse=1;
247 demo->getDynamicsWorld()->getSolverInfo().m_splitImpulse=0;
252 demo->setDebugMode(demo->getDebugMode() |btIDebugDraw::DBG_DrawAabb);
256 demo->setDebugMode(demo->getDebugMode() & (~btIDebugDraw::DBG_DrawAabb));
261 demo->setDebugMode(demo->getDebugMode() |btIDebugDraw::DBG_DrawWireframe);
265 demo->setDebugMode(demo->getDebugMode() & (~btIDebugDraw::DBG_DrawWireframe));
270 demo->setDebugMode(demo->getDebugMode() |btIDebugDraw::DBG_DrawContactPoints);
274 demo->setDebugMode(demo->getDebugMode() & (~btIDebugDraw::DBG_DrawContactPoints));
277 if (DisableDeactivation)
279 demo->setDebugMode(demo->getDebugMode() |btIDebugDraw::DBG_NoDeactivation);
283 demo->setDebugMode(demo->getDebugMode() & (~btIDebugDraw::DBG_NoDeactivation));
286 if (demo->getDynamicsWorld() && demo->getDynamicsWorld()->getWorldType() == BT_DISCRETE_DYNAMICS_WORLD)
288 btDiscreteDynamicsWorld* discreteWorld = (btDiscreteDynamicsWorld*) demo->getDynamicsWorld();
289 discreteWorld->getSolverInfo().m_numIterations = NumIterations;
294 demo->clientMoveAndDisplay();
298 demo->displayCallback();
303 - (void) keyPressed: (unsigned char) key
305 [_demo demo]->keyboardCallback(key, (int)MousePosition.x, (int)MousePosition.y );
308 - (void) keyReleased: (unsigned char) key
311 - (void) specialKeyPressed: (unsigned) key
313 [_demo demo]->specialKeyboard(key,(int)MousePosition.x, (int)MousePosition.y );
316 - (void) specialKeyReleased: (unsigned) key
318 [_demo demo]->specialKeyboardUp(key,(int)MousePosition.x, (int)MousePosition.y );
321 - (void) mouseButtonPressed: (unsigned) mouseButton
323 [_demo demo]->mouseFunc( mouseButton, GLUT_DOWN, (int)MousePosition.x, (int)MousePosition.y );
326 - (void) mouseButtonReleased: (unsigned) mouseButton
328 [_demo demo]->mouseFunc( mouseButton, GLUT_UP, (int)MousePosition.x, (int)MousePosition.y );
331 - (void) mouseMoved: (NSPoint) delta
333 [_demo demo]->mouseMotionFunc((int)MousePosition.x, (int)MousePosition.y );
336 - (void) newMousePosition: (NSPoint) newMousePosition
338 // Need to invert Y, since DemoApplication assumes origin at top-left
339 MousePosition = NSMakePoint( newMousePosition.x, ViewSize.height - newMousePosition.y );
342 - (void) scrollWheel: (NSPoint) delta
346 #pragma mark Global Simulation Properties
348 + (void) setIterations: (unsigned) iterations
350 NumIterations = iterations;
353 + (unsigned) iterations
355 return NumIterations;
358 + (unsigned) minIterations { return 1; }
359 + (unsigned) maxIterations { return 1000; }
362 + (void) setDisableDeactivation: (BOOL) disableDeactivation
364 DisableDeactivation = disableDeactivation;
367 + (BOOL) disableDeactivation
369 return DisableDeactivation;
372 + (void) setDrawAABBs: (BOOL) drawAABBs
374 DrawAABBs = drawAABBs;
382 + (void) setDebugDraw: (BOOL) debugDraw
384 DebugDraw = debugDraw;
392 + (void) setSplitImpulse: (BOOL) splitImpulse
394 SplitImpulse = splitImpulse;
397 + (BOOL) splitImpulse
402 + (void) setDrawContacts: (BOOL) drawContacts
404 DrawContacts = drawContacts;
407 + (BOOL) drawContacts