diff options
author | erwincoumans <erwin.coumans@gmail.com> | 2015-04-16 09:55:32 -0700 |
---|---|---|
committer | erwincoumans <erwin.coumans@gmail.com> | 2015-04-16 09:55:32 -0700 |
commit | a1bf9c55567a1e9be4fffcfa5d31c6dd59f99c94 (patch) | |
tree | efa6ca02d9659b9d390c136013adbb72bcaa4e3d /examples/HelloWorld | |
parent | d9feaf2d2add5a838130a93dcc51a561d72c0c10 (diff) | |
download | bullet3-a1bf9c55567a1e9be4fffcfa5d31c6dd59f99c94.tar.gz |
add initial examples, replacing the 'Demos/Demos3'. Will make it work cross-platform, OpenGL3/OpenGL2 and add more examples to it.
Diffstat (limited to 'examples/HelloWorld')
-rw-r--r-- | examples/HelloWorld/CMakeLists.txt | 29 | ||||
-rw-r--r-- | examples/HelloWorld/HelloWorld.cpp | 180 | ||||
-rw-r--r-- | examples/HelloWorld/premake4.lua | 22 |
3 files changed, 231 insertions, 0 deletions
diff --git a/examples/HelloWorld/CMakeLists.txt b/examples/HelloWorld/CMakeLists.txt new file mode 100644 index 000000000..3c15ad1d7 --- /dev/null +++ b/examples/HelloWorld/CMakeLists.txt @@ -0,0 +1,29 @@ +# HelloWorld is a minimal sample creating, stepping and deleting a Bullet dynamics world + +INCLUDE_DIRECTORIES( +${BULLET_PHYSICS_SOURCE_DIR}/src +) + +LINK_LIBRARIES( + BulletDynamics BulletCollision LinearMath +) + +IF (WIN32) + ADD_EXECUTABLE(AppHelloWorld + HelloWorld.cpp + ${BULLET_PHYSICS_SOURCE_DIR}/build3/bullet.rc + ) +ELSE() + ADD_EXECUTABLE(AppHelloWorld + HelloWorld.cpp + ) +ENDIF() + + + + +IF (INTERNAL_ADD_POSTFIX_EXECUTABLE_NAMES) + SET_TARGET_PROPERTIES(AppHelloWorld PROPERTIES DEBUG_POSTFIX "_Debug") + SET_TARGET_PROPERTIES(AppHelloWorld PROPERTIES MINSIZEREL_POSTFIX "_MinsizeRel") + SET_TARGET_PROPERTIES(AppHelloWorld PROPERTIES RELWITHDEBINFO_POSTFIX "_RelWithDebugInfo") +ENDIF(INTERNAL_ADD_POSTFIX_EXECUTABLE_NAMES)
\ No newline at end of file diff --git a/examples/HelloWorld/HelloWorld.cpp b/examples/HelloWorld/HelloWorld.cpp new file mode 100644 index 000000000..f1934b269 --- /dev/null +++ b/examples/HelloWorld/HelloWorld.cpp @@ -0,0 +1,180 @@ +/* +Bullet Continuous Collision Detection and Physics Library +Copyright (c) 2003-2007 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. +*/ + +///-----includes_start----- +#include "btBulletDynamicsCommon.h" +#include <stdio.h> + +/// This is a Hello World program for running a basic Bullet physics simulation + +int main(int argc, char** argv) +{ + ///-----includes_end----- + + int i; + ///-----initialization_start----- + + ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration. + btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); + + ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) + btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); + + ///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep. + btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase(); + + ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) + btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver; + + btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration); + + dynamicsWorld->setGravity(btVector3(0,-10,0)); + + ///-----initialization_end----- + + ///create a few basic rigid bodies + btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.))); + + //keep track of the shapes, we release memory at exit. + //make sure to re-use collision shapes among rigid bodies whenever possible! + btAlignedObjectArray<btCollisionShape*> collisionShapes; + + collisionShapes.push_back(groundShape); + + btTransform groundTransform; + groundTransform.setIdentity(); + groundTransform.setOrigin(btVector3(0,-56,0)); + + { + btScalar mass(0.); + + //rigidbody is dynamic if and only if mass is non zero, otherwise static + bool isDynamic = (mass != 0.f); + + btVector3 localInertia(0,0,0); + if (isDynamic) + groundShape->calculateLocalInertia(mass,localInertia); + + //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects + btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform); + btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia); + btRigidBody* body = new btRigidBody(rbInfo); + + //add the body to the dynamics world + dynamicsWorld->addRigidBody(body); + } + + + { + //create a dynamic rigidbody + + //btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1)); + btCollisionShape* colShape = new btSphereShape(btScalar(1.)); + collisionShapes.push_back(colShape); + + /// Create Dynamic Objects + btTransform startTransform; + startTransform.setIdentity(); + + btScalar mass(1.f); + + //rigidbody is dynamic if and only if mass is non zero, otherwise static + bool isDynamic = (mass != 0.f); + + btVector3 localInertia(0,0,0); + if (isDynamic) + colShape->calculateLocalInertia(mass,localInertia); + + startTransform.setOrigin(btVector3(2,10,0)); + + //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects + btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); + btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia); + btRigidBody* body = new btRigidBody(rbInfo); + + dynamicsWorld->addRigidBody(body); + } + + + +/// Do some simulation + + + ///-----stepsimulation_start----- + for (i=0;i<100;i++) + { + dynamicsWorld->stepSimulation(1.f/60.f,10); + + //print positions of all objects + for (int j=dynamicsWorld->getNumCollisionObjects()-1; j>=0 ;j--) + { + btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j]; + btRigidBody* body = btRigidBody::upcast(obj); + if (body && body->getMotionState()) + { + btTransform trans; + body->getMotionState()->getWorldTransform(trans); + printf("world pos = %f,%f,%f\n",float(trans.getOrigin().getX()),float(trans.getOrigin().getY()),float(trans.getOrigin().getZ())); + } + } + } + + ///-----stepsimulation_end----- + + //cleanup in the reverse order of creation/initialization + + ///-----cleanup_start----- + + //remove the rigidbodies from the dynamics world and delete them + for (i=dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--) + { + btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i]; + btRigidBody* body = btRigidBody::upcast(obj); + if (body && body->getMotionState()) + { + delete body->getMotionState(); + } + dynamicsWorld->removeCollisionObject( obj ); + delete obj; + } + + //delete collision shapes + for (int j=0;j<collisionShapes.size();j++) + { + btCollisionShape* shape = collisionShapes[j]; + collisionShapes[j] = 0; + delete shape; + } + + //delete dynamics world + delete dynamicsWorld; + + //delete solver + delete solver; + + //delete broadphase + delete overlappingPairCache; + + //delete dispatcher + delete dispatcher; + + delete collisionConfiguration; + + //next line is optional: it will be cleared by the destructor when the array goes out of scope + collisionShapes.clear(); + + ///-----cleanup_end----- +} + diff --git a/examples/HelloWorld/premake4.lua b/examples/HelloWorld/premake4.lua new file mode 100644 index 000000000..b3f1028ab --- /dev/null +++ b/examples/HelloWorld/premake4.lua @@ -0,0 +1,22 @@ + +project "AppHelloWorld" + +if _OPTIONS["ios"] then + kind "WindowedApp" +else + kind "ConsoleApp" +end + +includedirs {"../../src"} + +links { + "BulletDynamics","BulletCollision", "LinearMath" +} + +language "C++" + +files { + "**.cpp", + "**.h", +} + |