How to add Libbulletjme to an existing project
Adding Libbulletjme to an existing Java project is a 7-step process:
-
Add the JVM library to the classpath.
-
Download an appropriate native library.
-
Load the native library.
-
Create and configure a physics space.
-
Create and configure collision objects and add them to the physics space.
-
Simulate the physics space.
-
Test and tune as necessary.
Add JVM library to classpath
Gradle-built projects
Add to the project’s "build.gradle" or "build.gradle.kts" file:
repositories {
mavenCentral()
}
dependencies {
implementation("com.github.stephengold:Libbulletjme:21.2.1")
}
For some older versions of Gradle,
it’s necessary to replace implementation
with compile
.
Maven-built projects
Add to the project’s "pom.xml" file:
<repositories>
<repository>
<id>mvnrepository</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.stephengold</groupId>
<artifactId>Libbulletjme</artifactId>
<version>21.2.1</version>
</dependency>
Ant-built projects
Download the Libbulletjme library from GitHub:
You’ll definitely need the class JAR ("Libbulletjme-21.2.1.jar") and probably the "-sources" and "-javadoc" JARs as well.
Open the project’s properties in the NetBeans IDE:
-
Right-click on the project (not its assets) in the "Projects" window.
-
Select Properties to open the "Project Properties" dialog.
-
Under "Categories:", click on Libraries.
-
Click on the Compile tab.
-
Add the Heart class JAR:
-
Click on the Add JAR/Folder button.
-
Navigate to the download directory.
-
Select the "Libbulletjme-21.2.1" file.
-
Click on the Open button.
-
-
(optional) Add JARs for javadoc and sources:
-
Click on the Edit button.
-
Click on the Browse… button to the right of "Javadoc:"
-
Select the "Libbulletjme-21.2.1-javadoc.jar" file.
-
Click on the Open button.
-
Click on the Browse… button to the right of "Sources:"
-
Select the "Libbulletjme-21.2.1-sources.jar" file.
-
Click on the Open button button again.
-
Click on the OK button to close the "Edit Jar Reference" dialog.
-
-
Click on the OK button to exit the "Project Properties" dialog.
Download a native library
Pre-built native libraries can be downloaded from GitHub:
You don’t need all 56 native libraries. Start with the "DebugSp" library for your development environment, for instance:
-
"Linux64DebugSp_libbulletjme.so" for Linux on x86_64
-
"Windows64DebugSp_libbulletjme.so" for 64-bit Windows
Load the native library
Load Libbulletjme’s native library before instantiating any physics objects:
import com.jme3.system.NativeLibraryLoader;
NativeLibraryLoader.loadLibbulletjme(true, downloadDirectory, "Debug", "Sp");
Create a physics space
There are many constructors and options. Here’s the simplest way:
import com.jme3.bullet.PhysicsSpace;
PhysicsSpace physicsSpace = new PhysicsSpace(PhysicsSpace.BroadphaseType.DBVT);
Create and add objects
Collision objects come in many different types:
-
bodies (
PhysicsBody
)-
soft bodies (
PhysicsSoftBody
)-
deformables (
ReducedDeformableBody
)
-
-
rigid bodies (
PhysicsRigidBody
)-
vehicles (
PhysicsVehicle
)
-
-
-
ghost objects (
PhysicsGhostObject
) -
characters (
PhysicsCharacter
) -
colliders (
MultiBodyCollider
)
Here’s a code fragment that creates 2 objects, a ghost object and a rigid body that share a common shape:
float radius = 2f;
CollisionShape sphere2 = new SphereCollisionShape(radius);
PhysicsGhostObject ghost1 = new PhysicsGhostObject(sphere2);
float mass = 1f;
PhysicsRigidBody body1 = new PhysicsRigidBody(sphere2, mass);
Collision objects aren’t simulated unless they’re added to a physics space.
The best way is to use addCollisionObject()
:
physicsSpace.addCollisionObject(ghost1);
physicsSpace.addCollisionObject(body1);
Simulate the physics space
To simulate a single 20-millisecond step:
float timeStep = 0.02f; // in seconds
int maxSteps = 0; // for a single step of the specified duration
physicsSpace.update(timeStep, maxSteps);
In real-time simulation, the interval between updates will vary. However, it’s best to use steps of equal size.
To attempt simulation of a specific time interval using the configured step size:
physicsSpace.update(intervalSeconds);
HelloLibbulletjme
HelloLibbulletjme (also in Kotlin) is a complete console application (no graphics) that serves as a starting point for using Libbulletjme.
It illustrates:
-
loading a native library from the "~/Downloads" directory
-
creating a
PhysicsSpace
-
creating 2 collision objects and adding them to the space
-
simulating 50 steps
For instance, if you install Git and a Java Development Kit, you should be able to launch tutorial apps from a command shell, like so:
|