Skip to content

Latest commit

 

History

History
 
 

bullet

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

JavaCPP Presets for Bullet Physics SDK

Gitter Maven Central Sonatype Nexus (Snapshots)
Build status for all platforms: bullet Commercial support: xscode

Introduction

This directory contains the JavaCPP Presets module for:

Please refer to the parent README.md file for more detailed information about the JavaCPP Presets.

Documentation

Java API documentation is available here:

Special Mappings

Mappings of btAlignedObjectArray's instances for privitime types:

C++ Java
btAlignedObjectArray<bool> btBoolArray
btAlignedObjectArray<char> btCharArray
btAlignedObjectArray<int> btIntArray
btAlignedObjectArray<unsigned int> btUIntArray
btAlignedObjectArray<btScalar> btScalarArray

Name of a Java class, corresponding to an instance of btAlignedObjectArray for a composite type, is constructed by adding Array suffix to the name of the composite type, e.g. btAlignedObjectArray<btQuaternion> maps to btQuaternionArray.

Sample Usage

Here is a simple example of Bullet Physics SDK ported to Java and based on code found from:

We can use Maven 3 to download and install automatically all the class files as well as the native binaries. To run this sample code, after creating the pom.xml and SimpleBox.java source files below, simply execute on the command line:

 $ mvn compile exec:java

The pom.xml build file

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.bytedeco.bullet</groupId>
    <artifactId>samples</artifactId>
    <version>1.5.11</version>
    <properties>
        <exec.mainClass>SimpleBox</exec.mainClass>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>bullet-platform</artifactId>
            <version>3.25-1.5.11</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>.</sourceDirectory>
    </build>
</project>

The SimpleBox.java source file

import org.bytedeco.javacpp.*;
import org.bytedeco.bullet.BulletCollision.*;
import org.bytedeco.bullet.BulletDynamics.*;
import org.bytedeco.bullet.LinearMath.*;

public class SimpleBox {

    private static btDefaultCollisionConfiguration m_collisionConfiguration;
    private static btCollisionDispatcher m_dispatcher;
    private static btBroadphaseInterface m_broadphase;
    private static btConstraintSolver m_solver;
    private static btDiscreteDynamicsWorld m_dynamicsWorld;

    public static void main(String[] args)
    {
        createEmptyDynamicsWorld();

        btBoxShape groundShape = new btBoxShape(new btVector3(50, 50, 50));

        btTransform groundTransform = new btTransform();
        groundTransform.setIdentity();
        groundTransform.setOrigin(new btVector3(0, -50, 0));

        createRigidBody(0, groundTransform, groundShape);

        btBoxShape colShape = new btBoxShape(new btVector3(1, 1, 1));
        float mass = 1.0f;

        colShape.calculateLocalInertia(mass, new btVector3(0, 0, 0));

        btTransform startTransform = new btTransform();
        startTransform.setIdentity();
        startTransform.setOrigin(new btVector3(0, 3, 0));
        btRigidBody box = createRigidBody(mass, startTransform, colShape);

        for (int i = 0; i < 10; ++ i)
        {
            m_dynamicsWorld.stepSimulation(0.1f, 10, 0.01f);
            btVector3 position = box.getWorldTransform().getOrigin();
            System.out.println(position.y());
        }

        System.out.println(
            "\n" +
            "This sample simulates falling of a rigid box, followed by \n" +
            "an inelastic collision with a ground plane.\n" +
            "The numbers show height of the box at each simulation step. \n" +
            "It should start around 3.0 and end up around 1.0.\n");
    }

    private static void createEmptyDynamicsWorld()
    {
        m_collisionConfiguration = new btDefaultCollisionConfiguration();
        m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
        m_broadphase = new btDbvtBroadphase();
        m_solver = new btSequentialImpulseConstraintSolver();
        m_dynamicsWorld = new btDiscreteDynamicsWorld(
            m_dispatcher, m_broadphase, m_solver, m_collisionConfiguration);
        m_dynamicsWorld.setGravity(new btVector3(0, -10, 0));
    }

    private static btRigidBody createRigidBody(
        float mass,
        btTransform startTransform,
        btCollisionShape shape)
    {
        boolean isDynamic = (mass != 0.f);

        btVector3 localInertia = new btVector3(0, 0, 0);
        if (isDynamic)
            shape.calculateLocalInertia(mass, localInertia);

        btDefaultMotionState motionState = new btDefaultMotionState(
            startTransform, btTransform.getIdentity());

        btRigidBody.btRigidBodyConstructionInfo cInfo =
            new btRigidBody.btRigidBodyConstructionInfo(
                mass, motionState, shape, localInertia);

        btRigidBody body = new btRigidBody(cInfo);

        body.setUserIndex(-1);
        m_dynamicsWorld.addRigidBody(body);

        return body;
    }
}

See the samples subdirectory for more.