Java port of Bullet

xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Java port of Bullet

Post by xexuxjy »

Depends on wether you're using motion states.

but in general
Transform t = new Transform();
t.setIndentity();
t.origin.set(x,y,z);
collisionObject.setWorldTransform(t);

where x,y,z are the coordinates you want.
laadams85
Posts: 18
Joined: Wed May 22, 2013 1:45 pm

Re: Java port of Bullet

Post by laadams85 »

I finally got my issues resolved. A lot of the problems were in the fact that jbullet currently doesn't have the BoxBox algorithm implemented. With a proper algorithm in place there are no problems with bouncy contact.
tlaukkan
Posts: 1
Joined: Wed Sep 24, 2014 4:39 pm

Re: Java port of Bullet

Post by tlaukkan »

Hi

I am trying to collect and merge together patches and contributions to jbullet. If you are interested in working together on jbullet or have changes I could merge please let me know. The jbullet fork can be found here:

https://github.com/bubblecloud/jbullet

My email:

tommi.s.e.laukkanen@gmail.com

Best regards,
Tommi
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Java port of Bullet

Post by xexuxjy »

Are you looking to bring JBullet up to the same source as the current 2.82 version, or just merge in various patches? From what I remember from looking at the time there is quite a lot of work needed to do that.
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

Re: Java port of Bullet

Post by Chaz »

Hello guys, I have many questions.
At first - sorry for my bad English.
Why a few spheres makes so big load on my processor? Watch video below.
http://youtu.be/YD-OPSBiVDE and why they are moving only in 2 axis?
How can I limit a static plane size? I tryed this code

Code: Select all

CollisionShape groundShape = new StaticPlaneShape(new Vector3f(0,1,0),0.25f);
        groundShape.setLocalScaling(new Vector3f(1,1,1));
but it does not works, my balls can rides everywhere, I guess, the static plain is everlasting.
And how I can detect the collision? I used next code

Code: Select all

BulletGlobals.setContactAddedCallback(new ContactAddedCallback() {
            @Override
            public boolean contactAdded(ManifoldPoint manifoldPoint, CollisionObject collisionObject, int i, int i2,
                                        CollisionObject collisionObject2, int i3, int i4) {
                System.out.println("collide!");
                return false;
            }
        });
But it does not works =(
Here is all code

Code: Select all

import com.bulletphysics.BulletGlobals;
import com.bulletphysics.ContactAddedCallback;
import com.bulletphysics.collision.broadphase.BroadphaseInterface;
import com.bulletphysics.collision.broadphase.BroadphaseProxy;
import com.bulletphysics.collision.broadphase.CollisionFilterGroups;
import com.bulletphysics.collision.broadphase.DbvtBroadphase;
import com.bulletphysics.collision.dispatch.CollisionConfiguration;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.dispatch.CollisionFlags;
import com.bulletphysics.collision.dispatch.CollisionObject;
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
import com.bulletphysics.collision.narrowphase.ManifoldPoint;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.SphereShape;
import com.bulletphysics.collision.shapes.StaticPlaneShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.DynamicsWorld;
import com.bulletphysics.dynamics.InternalTickCallback;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.dynamics.constraintsolver.ConstraintSolver;
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.Transform;
import com.jogamp.opengl.util.*;
import com.jogamp.opengl.util.gl2.GLUT;

import java.awt.Cursor;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.vecmath.Matrix4f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;

public class Main implements GLEventListener {

    private double theta = 0;
    private double s = 0;
    private double c = 0;
    private GLU glu;
    private GLUT glut;
    private GL2 gl;
    private static double y;
    private static float cameraPosX,cameraPosY=5,cameraPosZ=-100, cameraRotX,cameraRotY,cameraRotZ,
            mouseSpeedX,mouseSpeedY,mouseLastPosX,mouseLastPosY,
            cameraAngleX,cameraAngleY,cameraAngleZ,ballPosition;
    /*JBullet*/
    private static DynamicsWorld dynamicsWorld;
    private static Set<RigidBody> balls = new HashSet<RigidBody>();
    private static RigidBody groundRigidBody,ballRigidBody;
    private static Transform groundTransform;
    private static MotionState ballMotionState;
    private static CollisionShape ballShape;

    private static void setUpPhysics()
    {
        BulletGlobals.setContactAddedCallback(new ContactAddedCallback() {
            @Override
            public boolean contactAdded(ManifoldPoint manifoldPoint, CollisionObject collisionObject, int i, int i2,
                                        CollisionObject collisionObject2, int i3, int i4) {
                System.out.println("collide!");
                return false;
            }
        });
        BroadphaseInterface broadphase = new DbvtBroadphase();
        CollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
        CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);
        ConstraintSolver solver = new SequentialImpulseConstraintSolver();
        dynamicsWorld=new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
        dynamicsWorld.setGravity(new Vector3f(0,-9.81f,0));
        CollisionShape groundShape = new StaticPlaneShape(new Vector3f(0,1,0),0.25f);
        groundShape.setLocalScaling(new Vector3f(1,1,1));
        ballShape = new SphereShape(1);
        groundTransform=new Transform(new Matrix4f(new Quat4f(0,0,0.0f,1),new Vector3f(0,0,0),1.0f));
        MotionState groundMotionState = new DefaultMotionState();
        RigidBodyConstructionInfo groundBodyConstructionInfo = new RigidBodyConstructionInfo(0, groundMotionState, groundShape, new Vector3f(0,0,0));
        groundBodyConstructionInfo.restitution=0.25f;
        groundRigidBody = new RigidBody(groundBodyConstructionInfo);
        groundRigidBody.setCollisionFlags(groundRigidBody.getCollisionFlags() | CollisionFlags.KINEMATIC_OBJECT);
        dynamicsWorld.addRigidBody(groundRigidBody);

        ballShape = new SphereShape(1);
        ballMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0,0,0,1),new Vector3f(0,40,0),1.0f)));
        RigidBodyConstructionInfo ballBodyConstructionInfo = new RigidBodyConstructionInfo(1,ballMotionState,ballShape, new Vector3f(0,0,0));
        ballShape.calculateLocalInertia(1.0f,new Vector3f(0,0,0));
        ballBodyConstructionInfo.restitution=0.5f;
        ballBodyConstructionInfo.angularDamping=0.95f;
        ballRigidBody = new RigidBody(ballBodyConstructionInfo);
        ballRigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
        balls.add(ballRigidBody);
        dynamicsWorld.addRigidBody(ballRigidBody);

    }

    private static void AddBall()
    {
        SphereShape ballShape = new SphereShape(1);
        MotionState ballMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0,0,0,1),new Vector3f(0,40,0),1.0f)));
        RigidBodyConstructionInfo ballBodyConstructionInfo = new RigidBodyConstructionInfo(1,ballMotionState,ballShape, new Vector3f(0,0,0));
        ballShape.calculateLocalInertia(1.0f,new Vector3f(0,0,0));
        ballBodyConstructionInfo.restitution=0.5f;
        ballBodyConstructionInfo.angularDamping=0.95f;
        RigidBody ballRigidBody = new RigidBody(ballBodyConstructionInfo);
        ballRigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
        balls.add(ballRigidBody);
        dynamicsWorld.addRigidBody(ballRigidBody);
    }

    public static void StartGL()
    {
        GLProfile glp = GLProfile.getDefault();
        GLCapabilities caps = new GLCapabilities(glp);
        final GLCanvas canvas = new GLCanvas(caps);

        Frame frame = new Frame("AWT Window Test");
        frame.setSize(1024, 720);
        frame.add(canvas);
        frame.setVisible(true);


        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        canvas.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == 38) {
                    y += 0.1;
                }
                if (e.getKeyCode() == 40) {
                    y -= 0.1;
                }

                switch (e.getKeyChar()) {
                case 'w':
                    cameraPosZ += 1f;
                    cameraRotZ += 1f;
                    break;
                case 's':
                    cameraPosZ -= 1f;
                    cameraRotZ -= 1f;
                    break;
                case 'a':
                    cameraPosX += 1f;
                    cameraRotX+=1f;
                    break;
                case 'd':
                    cameraPosX -= 1f;
                    cameraRotX-=1f;
                    break;
                case 'e':
                    cameraPosY += 1f;
                    cameraRotY+=1f;
                    break;
                case 'q':
                    cameraPosY -= 1f;
                    cameraRotY-=1f;
                    break;
                case ']' :
                    Transform transform=new Transform();
                    ballMotionState.getWorldTransform(transform);
                    transform.origin.setX(transform.origin.x-1);
                    ballMotionState.setWorldTransform(transform);
                    ballRigidBody.setMotionState(ballMotionState);
                    break;
                case '[' :
                    Transform transform1=new Transform();
                    ballMotionState.getWorldTransform(transform1);
                    transform1.origin.setX(transform1.origin.x+1);
                    ballMotionState.setWorldTransform(transform1);
                    ballRigidBody.setMotionState(ballMotionState);
                    break;
                case 'f' :
                    AddBall();
                }
            }
        });
        canvas.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                mouseLastPosX=e.getX();
                mouseLastPosY=e.getY();
                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB),new Point(0,0),"Hide"));
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                canvas.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        canvas.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                mouseSpeedX=e.getX()-mouseLastPosX;
                mouseSpeedY=e.getY()-mouseLastPosY;

                mouseLastPosX=e.getX();
                mouseLastPosY=e.getY();

                cameraAngleX+=mouseSpeedX/5;
                cameraAngleY+=mouseSpeedY/5;

                cameraRotX=(float)(Math.sin(Math.toRadians(cameraAngleX)))+cameraPosX;
                cameraRotY=(float)(Math.sin(Math.toRadians(cameraAngleY)))+cameraPosY;
                cameraRotZ=(float)(Math.cos(Math.toRadians(cameraAngleX)))+cameraPosZ;

                System.out.print(Math.toRadians(cameraAngleX)+ " : " + cameraAngleY+"\n");

            }

            @Override
            public void mouseMoved(MouseEvent e) {

            }
        });

        canvas.addGLEventListener(new Main());
        FPSAnimator animator = new FPSAnimator(canvas, 60);
        animator.start();
    }

    public static void main(String[] args) throws Exception{
        setUpPhysics();
        StartGL();
    }

    @Override
    public void init(GLAutoDrawable glAutoDrawable) {
        glu=GLU.createGLU(glAutoDrawable.getGL().getGL2());
        glut=new GLUT();
        gl=glAutoDrawable.getGL().getGL2();
       // gl.glPolygonMode(gl.GL_FRONT_AND_BACK,gl.GL_LINE);
    }

    @Override
    public void dispose(GLAutoDrawable glAutoDrawable) {

    }

    @Override
    public void display(GLAutoDrawable glAutoDrawable) {
        update();
        render(glAutoDrawable);
    }

    private void render(GLAutoDrawable drawable)
    {
        gl.glEnable(gl.GL_DEPTH_TEST);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glClearColor(0.2f, 0.4f, 0.7f, 0);
        gl.glMatrixMode(gl.GL_MODELVIEW);
        gl.glLoadIdentity();
        glu.gluLookAt(cameraPosX, cameraPosY, cameraPosZ, cameraRotX, cameraRotY, cameraRotZ, 0, 1, 0);

        for(int i =0; i<balls.size()+1; i++)
        {
            gl.glPushMatrix();
        }
        int x=0;
        for(RigidBody ball : balls)
        {
            gl.glPopMatrix();
            Vector3f ballPosition = ball.getWorldTransform(new Transform()).origin;
            gl.glTranslatef(ballPosition.x,ballPosition.y,ballPosition.z);
            gl.glColor3f(0, 1, 0);
            glut.glutSolidSphere(1,100,100);
            x++;

        }

        gl.glColor3f(0.5f,0,0);
        gl.glPopMatrix();
        float[] m= new float[16];
        groundRigidBody.getWorldTransform(new Transform()).getOpenGLMatrix(m);
        FloatBuffer matrix = FloatBuffer.wrap(m);
        gl.glMultMatrixf(matrix);
        gl.glBegin(gl.GL_POLYGON);
            gl.glVertex3f(-50,0,-50);
             gl.glVertex3f(-50,0,50);
              gl.glVertex3f(50,0,50);
              gl.glVertex3f(50,0,-50);
        gl.glEnd();

    }

    private void update()
    {
        theta += 1;
        c = Math.cos(theta);
        dynamicsWorld.stepSimulation(1.0f / 60.0f);
    }

    @Override
    public void reshape(GLAutoDrawable glAutoDrawable, int i, int i2, int w, int h) {
        gl.glViewport(0,0,w,h);
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(40,(float)w/h,0.1f,1000);
        gl.glMatrixMode(gl.GL_MODELVIEW);
    }
}
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

Re: Java port of Bullet

Post by Chaz »

all right guys, problem with perfomance was in rendering xD
avejidah
Posts: 4
Joined: Fri May 08, 2015 5:01 pm

Re: Java port of Bullet

Post by avejidah »

Is there a way of restricting jBullet to 2D? I see in Bullet (http://www.bulletphysics.org/mediawiki- ... e_Snippets) that RigidBody has methods setAngularFactory and setLinearFactor, each taking vector parameters. In jBullet, however, these methods take floats, so I can't restrict rotation/translation to two dimensions. I've tried using a HingeConstraint to accomplish this, but I can't make it work quite right (I'll make a second post regarding that attempt). I've also tried setting the linear/angular velocity to 0 every time I step the simulation, but over time - I'm guessing do to interpolation - my objects begin to rotate slightly.

Thank you!

xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Java port of Bullet

Post by xexuxjy »

Looks like the standard JBullet predates the linearFactor calls, shouldn't be a big deal to add them in though...
avejidah
Posts: 4
Joined: Fri May 08, 2015 5:01 pm

Re: Java port of Bullet

Post by avejidah »

Thanks for the response, xexuxjy. I kept playing with the HingeConstraint and got it to work acceptably for my situation. The linear/angular factor would have been great in this case, though!
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Java port of Bullet

Post by xexuxjy »

Out of interest, which version of JBullet are you using? If you're building from the github one (https://github.com/bubblecloud/jbullet) I can probably submit a patch to enabled the setLinear/Angular Factor code in case it's helpful in the future
avejidah
Posts: 4
Joined: Fri May 08, 2015 5:01 pm

Re: Java port of Bullet

Post by avejidah »

Hi xexuxjy. I'm using version 2.7.2 from http://jbullet.advel.cz/, not the version from github. For the project I'm currently working on the issue is resolved, but I really appreciate your response and offer to submit a patch!
elect
Posts: 8
Joined: Tue Aug 25, 2015 6:50 am

Re: Java port of Bullet

Post by elect »

Hi, I am evaluating porting bullet 3 to java (using also jocl), is there anyone who also is interested so that we may unite all our efforts?
immortius
Posts: 6
Joined: Sat Aug 22, 2015 4:12 am

Re: Java port of Bullet

Post by immortius »

elect, have you considered a native wrapper rather than a port (like gdxBullet)? Obviously pros and cons with either approach, but I worry about ports not being maintained, like what happened with jBullet.
elect
Posts: 8
Joined: Tue Aug 25, 2015 6:50 am

Re: Java port of Bullet

Post by elect »

immortius wrote:elect, have you considered a native wrapper rather than a port (like gdxBullet)? Obviously pros and cons with either approach, but I worry about ports not being maintained, like what happened with jBullet.
I know, that's the problem when you try to do all alone.

I thing putting everything on github, a nice wiki and doing a campaing among those who are interested might lead to a small community who will mantain it during time
deepthought
Posts: 3
Joined: Mon Apr 29, 2013 6:26 pm

Re: Java port of Bullet

Post by deepthought »

immortius wrote:elect, have you considered a native wrapper rather than a port (like gdxBullet)? Obviously pros and cons with either approach, but I worry about ports not being maintained, like what happened with jBullet.
I think this would be less of an issue with bullet3, since most of the code is OpenCL, and would not need to be ported
Post Reply