StaticPlaneShape and SphereShape strange behaviour

Post Reply
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

StaticPlaneShape and SphereShape strange behaviour

Post by Chaz »

Hello sirs. I'm new to Bullet, and actually use JBullet. But I guess, that my problem is shared for different Bullet version.
In my world there are only one StaticPlaneShape and many SphereShape which i created in any time by pressing 'f' key.
At some point StaticPlaneShape starts to behaving strangely. It just moving with some Spheres, you could watch the video below.
http://youtu.be/cUpEYoh9h0w
And one more problem.
Why all Spheres does not falling down? Instead of it, they are balancing on eachother. And only when I move the lower Sphere, they are doing something like fall. But actually they does not moves in Z direction, only X and Y.
So why it is?
Here is main parts of code.

Code: Select all


public class Main implements GLEventListener {

    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()
    {

        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);
        balls.add(ballRigidBody);
        dynamicsWorld.addRigidBody(ballRigidBody);

        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;
            }
        });

    }

    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) {
                switch (e.getKeyChar()) {
                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.addGLEventListener(new Main());
        FPSAnimator animator = new FPSAnimator(canvas, 60);
        animator.start();
    }

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

    @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,10,10);
            x++;

        }

        gl.glColor3f(0.5f,0,0);
        gl.glPopMatrix();
        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()
    {
        dynamicsWorld.stepSimulation(1.0f / 60.0f);
    }
}
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

Re: StaticPlaneShape and SphereShape strange behaviour

Post by Chaz »

Guys, sorry. I found cause of the strange behaviour of StaticPlane. Problem was in my OpenGL code.
But with rest of problems - still need your help.
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: StaticPlaneShape and SphereShape strange behaviour

Post by xexuxjy »

Been a while since I looked at JBullet, but I think your code for CalculateLocalInertia on your shapes is wrong.
You need to pass in a Vector3f to that call so that the values can be set, that should then be passed to the RigidBodyConstructionInfo.
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

Re: StaticPlaneShape and SphereShape strange behaviour

Post by Chaz »

xexuxjy wrote:Been a while since I looked at JBullet, but I think your code for CalculateLocalInertia on your shapes is wrong.
You need to pass in a Vector3f to that call so that the values can be set, that should then be passed to the RigidBodyConstructionInfo.
You mean like this?

Code: Select all

Vector3f localInertia = new Vector3f(0,0,0);
        ballShape.calculateLocalInertia(1.0f,localInertia);
        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);
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: StaticPlaneShape and SphereShape strange behaviour

Post by xexuxjy »

Yep, though should be :

RigidBodyConstructionInfo ballBodyConstructionInfo = new RigidBodyConstructionInfo(1,ballMotionState,ballShape,localInertia);

I think.
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

Re: StaticPlaneShape and SphereShape strange behaviour

Post by Chaz »

xexuxjy, unfortunately, it did not give the desired effect
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: StaticPlaneShape and SphereShape strange behaviour

Post by xexuxjy »

Did it change your simulation at all? and just checking you made the change in the addBall function as well?
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

Re: StaticPlaneShape and SphereShape strange behaviour

Post by Chaz »

xexuxjy wrote:Did it change your simulation at all? and just checking you made the change in the addBall function as well?
No, nothing has changed. Here is AddBall func

Code: Select all

 private static void AddBall()
    {
        SphereShape ballShape = new SphereShape(1);
        Vector3f localInertia = new Vector3f(0,0,0);
        ballShape.calculateLocalInertia(1.0f,localInertia);
        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, localInertia);
        ballBodyConstructionInfo.restitution=0.5f;
        ballBodyConstructionInfo.angularDamping=0.95f;
        RigidBody ballRigidBody = new RigidBody(ballBodyConstructionInfo);
        ballRigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
        balls.add(ballRigidBody);
        dynamicsWorld.addRigidBody(ballRigidBody);
    }
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: StaticPlaneShape and SphereShape strange behaviour

Post by xexuxjy »

hmm. running out of ideas I'm afraid. Can you try leaving the default angularDamping and restitution values alone and see if that helps?
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

Re: StaticPlaneShape and SphereShape strange behaviour

Post by Chaz »

does not helped
p.s. about which problem we are talking?
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: StaticPlaneShape and SphereShape strange behaviour

Post by xexuxjy »

I was looking at the :
"Why all Spheres does not falling down? Instead of it, they are balancing on eachother. And only when I move the lower Sphere, they are doing something like fall. But actually they does not moves in Z direction, only X and Y.
So why it is?"

part and checking the behaviour in your video.
Chaz
Posts: 44
Joined: Mon Jan 12, 2015 1:36 pm

Re: StaticPlaneShape and SphereShape strange behaviour

Post by Chaz »

all right then
Post Reply