/* Bullet Continuous Collision Detection and Physics Library Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 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. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ #include "btBulletDynamicsCommon.h" #include "LinearMath/btQuickprof.h" #include "LinearMath/btIDebugDraw.h" #include "../CommonInterfaces/CommonParameterInterface.h" #include //printf debugging #include class btCollisionShape; #include "CommonRigidBodyMTBase.h" #include "MultiThreadedDemo.h" #include "LinearMath/btAlignedObjectArray.h" #include "btBulletCollisionCommon.h" #define BT_OVERRIDE /// MultiThreadedDemo shows how to setup and use multithreading class MultiThreadedDemo : public CommonRigidBodyMTBase { static const int kUpAxis = 1; btRigidBody* localCreateRigidBody(btScalar mass, const btTransform& worldTransform, btCollisionShape* colSape); btVector3 m_cameraTargetPos; float m_cameraPitch; float m_cameraYaw; float m_cameraDist; void createStack( const btVector3& pos, btCollisionShape* boxShape, const btVector3& halfBoxSize, int size ); void createSceneObjects(); void destroySceneObjects(); public: BT_DECLARE_ALIGNED_ALLOCATOR(); MultiThreadedDemo( struct GUIHelperInterface* helper ); virtual ~MultiThreadedDemo() {} virtual void stepSimulation( float deltaTime ) BT_OVERRIDE { if ( m_dynamicsWorld ) { // always step by 1/60 for benchmarking m_dynamicsWorld->stepSimulation( 1.0f / 60.0f, 0 ); } } virtual void initPhysics() BT_OVERRIDE; virtual void resetCamera() BT_OVERRIDE { m_guiHelper->resetCamera( m_cameraDist, m_cameraPitch, m_cameraYaw, m_cameraTargetPos.x(), m_cameraTargetPos.y(), m_cameraTargetPos.z() ); } }; MultiThreadedDemo::MultiThreadedDemo(struct GUIHelperInterface* helper) : CommonRigidBodyMTBase( helper ) { m_cameraTargetPos = btVector3( 0.0f, 0.0f, 0.0f ); m_cameraPitch = 90.0f; m_cameraYaw = 30.0f; m_cameraDist = 48.0f; helper->setUpAxis( kUpAxis ); } static btScalar gSliderStackRows = 8.0f; static btScalar gSliderStackColumns = 6.0f; void MultiThreadedDemo::initPhysics() { createEmptyDynamicsWorld(); m_dynamicsWorld->setGravity( btVector3( 0, -10, 0 ) ); { SliderParams slider( "Stack rows", &gSliderStackRows ); slider.m_minVal = 1.0f; slider.m_maxVal = 20.0f; slider.m_clampToNotches = false; m_guiHelper->getParameterInterface()->registerSliderFloatParameter( slider ); } { SliderParams slider( "Stack columns", &gSliderStackColumns ); slider.m_minVal = 1.0f; slider.m_maxVal = 20.0f; slider.m_clampToNotches = false; m_guiHelper->getParameterInterface()->registerSliderFloatParameter( slider ); } createSceneObjects(); m_guiHelper->createPhysicsDebugDrawer( m_dynamicsWorld ); } btRigidBody* MultiThreadedDemo::localCreateRigidBody(btScalar mass, const btTransform& startTransform, btCollisionShape* shape) { btRigidBody* body = createRigidBody(mass, startTransform, shape); if ( mass > 0.0f ) { // prevent bodies from sleeping to make profiling/benchmarking easier body->forceActivationState( DISABLE_DEACTIVATION ); } return body; } void MultiThreadedDemo::createStack( const btVector3& center, btCollisionShape* boxShape, const btVector3& halfBoxSize, int size ) { btTransform trans; trans.setIdentity(); float halfBoxHeight = halfBoxSize.y(); float halfBoxWidth = halfBoxSize.x(); for ( int i = 0; isetLinearVelocity( btVector3( 0, 0, -10 ) ); } m_guiHelper->autogenerateGraphicsObjects( m_dynamicsWorld ); } CommonExampleInterface* MultiThreadedDemoCreateFunc( struct CommonExampleOptions& options ) { return new MultiThreadedDemo(options.m_guiHelper); }