Bullet Physics and jBullet Physics Engine
Bullet Physics
Bullet Physics is an open-source collision detection, rigid body and soft body dynamics library. It can be used as the physics engine in a complex 3D game or animation, complimentary the game engine. The mother site is http://bulletphysics.org/wordpress/. The main task of physics engine is perform collision detection, resolve collisions and other constraints, and update the states for all the objects.
How to Install and Get Started
Step 1: Download and Unzip - From http://bulletphysics.org ⇒ Download ⇒ Select the ZIP source file ⇒ Download and unzip into a directory of your choice, e.g., d:\myproject. Bullet physics shall be unzipped into "d:\myproject\bullet-2.xx". I shall refer to this directory as the Bullet Physics installed directory.
Step 2: Read - Read the "User Manual" available at the installed directory.
Step3: Install CMake - We need CMake to build the Bullet Physics. Get Cmake from http://cmake.org ⇒ Download ⇒ Choose "Binary distributions" ⇒ Window ZIP ⇒ Unzip in a directory of your choice (e.g., d:\myproject).
Step 4: Build Bullet Physics using CMake
- Run "
cmake-gui.exe" (under the CMake "bin" directory). - In "Where is the source code", click "Browse source" and select the Bullet Physics installed directory (NOT src).
- In "Where to build the binaries", select a directory (e.g.,
d:\myproject\bullet-physics-2.xx-cmake_vc9) ⇒ Configure. - In "Specify the generator for this project", choose your target platform, e.g., "Visual Studio 9 2008" ⇒ Finish.
- Set your "
CMAKE_INSTALL_PREFIX", e.g.,d:\myproject\bullet-physics-2.xx-cmake_vc9, "LIBRARY_OUTPUT_DIRECTORY",d:\myproject\bullet-physics-2.xx-cmake_vc9\lib⇒ Configure ⇒ Generate ⇒ Close CMake. - In CMake build target (
bullet-physics-2.xx-cmake_vc9), start "BULLET_PHYSICS.sln" (double-click to launch the VC) ⇒ Build ⇒ Build Solution. The ".lib" will built underbullet-physics-2.xx-cmake_vc9\bin\debug.
Step 5: Write a Hello-world Bullet Physics Program
- Create a new solution/project: Launch VC ⇒ File ⇒ New Project ⇒ In "Project Types", select "Win32" ⇒ In "Templates", select "Win32 Console Application" ⇒ In "Location", select your working directory ⇒ In "Solution Name", enter "TestBulletPhysics", and check "Create directory for solution" ⇒ In "Name", enter "Helloworld" ⇒ Next ⇒ In "Application Type", choose "Console application" ⇒ In "Additional options", choose "Empty project".
- Create a new source file: Right-click on "Source Files" ⇒ Add ⇒ New Item... ⇒ In "Name", enter "Helloworld.cpp". Look for "Helloworld.cpp" from the demos (under "
bullet-2.xx\Demos\HelloWorld"), and copy all the codes. - Specify the include-path for header "
btBulletDynamicsCommon.h": Right-click on the project ⇒ Properties ⇒ Configuration Properties ⇒ C/C++ ⇒ General ⇒ Additional Include Directories ⇒ Enter bullet's source directory (e.g.,d:\myproject\bullet-2.77\src- you can see that the header needed is kept here). - Include the bullet's libraries (
BulletCollision,BulletDynamicsandLinearMath): Two ways:- Add the CMake generated project files: File ⇒ Add ⇒ Existing Project... ⇒ select CMake target's
src/BulletCollision/BulletCollision.vcproj,src/BulletDynamics/BulletDynamics.vcprojandsrc/LinearMath/LinearMath.vcproj⇒ Right-click on the project "Helloworld" ⇒ Project Dependencies ⇒ Check the above three projects. - Use the libraries generated: Right-click on the project ⇒ Properties ⇒ Configuration Properties ⇒ Linker ⇒ General ⇒ In "Additional Library directories", enter
d:\myproject\bullet-2.xx-cmake-vc9\lib\Debug(the libraries built in the earlier step) ⇒ In "Use Library Dependency Inputs", select "Yes". In Linker ⇒ Input ⇒ In "Additional Dependencies", enter "BulletCollision.libBulletDynamics.libLinearMath.lib" (VS requires ".lib" extension; Unixes do not).
- Add the CMake generated project files: File ⇒ Add ⇒ Existing Project... ⇒ select CMake target's
- Build the project, and run the program.
Try other demos source provided, in particular, BasicDemo, which requires many more libraries.
jBullet
jBullet is a Java port of Bullet Physics. The mother site is at http://jbullet.advel.cz.
How to Install and Get Started
Step 1: Download and Unzip - Download jBullet from http://jbullet.advel.cz, and upzip into a directory of your choice, e.g., d:\myproject. jBullet shall be unzipped into d:\myproject\jbullet-2010xxxx.
Step 2: Try the Demo: To run the demo program in Eclipse:
- Create a New Project ⇒ Create a package called
com.bulletphysics.demos, copy all the demo codes (all the directories underd:\myproject\jbullet-2010xxxx\src\com\bulletphysics\demos) under this package. - Include External JAR Files: Right-click on the project ⇒ Build Path ⇒ Add External Archives ⇒ Navigate to select the distributed JAR files under the "
lib" directory (d:\myproject\jbullet-20101010\lib). You should includeasm-all-3.1.jar,stack-alloc.jar,jinput.jar,lwjgl.jar,lwjgl_util.jar,swing-layout-1.0.3.jarandvecmath.jar. - Include Native Libraries for
lwjgl.jar: Right-click on thelwjgl.jar⇒ Build Path ⇒ Configure Build Path... ⇒ Openlwjgl.jarnode ⇒ Select "Native Library Location" ⇒ Edit ⇒ In "Location Path", enter "d:/project/jbullet-20101010/lib/lwjgl/win32".
Using jStackAlloc
jBullet author also created a library called jStackAlloc, which allocates objects on the method's stack instead of program heap. This improves real-time performance by reducing the frequency of garbage collection. jStackAlloc uses a JDK 1.5 feature called Instrument to modify the bytecodes.
Writing a Program that uses jStackAlloc:
- Create a project. Add External Archive
jstack-alloc.jar,asm-all-3.1.jar,vecmath.jarandant.jar(select from the Eclipse plug-in). - Write a test program as follows:
The above program allocate1 2 3 4 5 6 7 8 9 10 11 12 13 14
package mytest; import javax.vecmath.Vector3f; import cz.advel.stack.Stack; public class TestJStackAlloc { public static void main(String[] args) { Vector3f tmp = Stack.alloc(Vector3f.class); tmp.set(1, 2, 3); System.out.println(tmp.x); System.out.println(tmp.y); System.out.println(tmp.z); } }Vector3f(of javax.vecmath) in stack, instead of the heap. You can also use your own class instead ofVector3f(with some restrictions on the class design - see jStackAlloc documentation). - You cannot run the program directly, as jStackAlloc need to perform so-called instrumentation, which modifies the byte-code. You can then run the modified bytecodes. Write a ANT build file (
build.xml) as follows, which invoke the Instrumentation task on the compiled classes and then run from the instrumented-classes.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?xml version="1.0" encoding="UTF-8"?> <project name="javabullet" default="run" basedir="."> <path id="myclasspath"> <pathelement path="bin"/> <pathelement location="d:\myproject\jbullet-20101010\lib\jstackalloc\stack-alloc.jar/" /> <pathelement location="d:\myproject\jbullet-20101010\lib\ASM3.1\asm-all-3.1.jar" /> <pathelement location="d:\myproject\jbullet-20101010\lib\vecmath\vecmath.jar" /> </path> <target name="instrument-classes" > <taskdef name="instrument-stack" classname="cz.advel.stack.instrument.InstrumentationTask" classpathref="myclasspath" > </taskdef> <instrument-stack dest="bin" packageName="mytest"> <fileset dir="bin" includes="**/*.class" /> </instrument-stack> </target> <target name="run" depends="instrument-classes"> <java classname="mytest.TestJStackAlloc" classpathref="myclasspath" fork="true" failonerror="true" > </java> </target> </project>
Using jBullet on Android
JBullet works on Android.
[TODO] Helloworld
[TODO] BasicDemo
REFERENCES & RESOURCES
- Bullet Physics mother site at http://bulletphysics.org/wordpress/.
- "Bullet Physics SDK Manual", bundled with the download.
- JBullet mother site at http://jbullet.advel.cz/.
Latest version tested: Bullet Physics 2.77, JBullet 20101010
Last modified: November, 2010