Box not rotating correctly

herrmon
Posts: 4
Joined: Tue Sep 24, 2013 9:21 am

Box not rotating correctly

Post by herrmon »

Hey, I'm just getting into jBullet and tried a simple application with a falling Box on the ground. Unfortunately, the rotation is somehow incorrect and the Box seems to be "stuck" in the ground.

Image

The quaternion for the rotation i get from the Transform object in my MotionState has the values:

x: -5.559519E-9
y: 0.6384396
z: 7.559638E-8
w: 0.7696719

.. so i think the problem should not be in the graphics positioning but somewhere in the physics.

Here is my MotionState:

Code: Select all

public class StaticObjectMotionState extends MotionState{

	Transform transform;
	StaticObject object;
	
	public StaticObjectMotionState(Transform initTransform, StaticObject object)
	{
		transform = initTransform;
		this.object = object;
	}
	
	@Override
	public Transform getWorldTransform(Transform transform) {
		transform.set(this.transform);
		return this.transform;
	}

	@Override
	public void setWorldTransform(Transform transform) {
		object.setPosition(new Vector3f(transform.origin.x, transform.origin.y, transform.origin.z));
		javax.vecmath.Quat4f quat = new javax.vecmath.Quat4f();
		transform.getRotation(quat);
		object.setRotation(new Quaternion(quat.x, quat.y, quat.z, quat.w));
		this.transform.set(transform);
	}

}
Physics initialization:

Code: Select all

public class Physics {
	
	private final static Physics instance = new Physics();
	
	private DynamicsWorld dynamicsWorld;
	
	public Physics()
	{
		DefaultCollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
		CollisionDispatcher collisionDispatcher = new CollisionDispatcher(collisionConfiguration);
		
		SequentialImpulseConstraintSolver constraintSolver = new SequentialImpulseConstraintSolver();
		
		Vector3f worldAabbMin = new Vector3f(-10000, -10000, -10000);
		Vector3f worldAabbMax = new Vector3f(10000, 10000, 10000);
		int maxProxies = 1024;
		AxisSweep3 overlappingPairCache =
				new AxisSweep3(worldAabbMin, worldAabbMax, maxProxies);
		
		dynamicsWorld = new DiscreteDynamicsWorld(collisionDispatcher, overlappingPairCache, constraintSolver, collisionConfiguration);
		dynamicsWorld.setGravity(new Vector3f(0,-1,0));
	}
	
	public void simulateStep(float delta)
	{
		dynamicsWorld.stepSimulation(delta);
	}
	
	public static Physics getInstance()
	{
		return instance;
	}
	
	public DynamicsWorld getDynamicsWorld()
	{
		return dynamicsWorld;
	}
}
Ground:

Code: Select all

		collisionShape = new StaticPlaneShape(new javax.vecmath.Vector3f(0,1,0), 1);
		Transform groundTransform = new Transform();
		groundTransform.setIdentity();
		groundTransform.origin.set(new javax.vecmath.Vector3f(0.f, -10.f, 0.f));
		StaticObjectMotionState motionState = new StaticObjectMotionState(groundTransform, this);
		//DefaultMotionState groundMotionState = new DefaultMotionState(groundTransform);
		RigidBodyConstructionInfo construction = new RigidBodyConstructionInfo(0, motionState, collisionShape, new javax.vecmath.Vector3f(0f,0f,0f));
		rigidBody = new RigidBody(construction);
Box:

Code: Select all

		collisionShape = new BoxShape(new javax.vecmath.Vector3f(2.0f,1.0f,1.0f));
		Transform motionTransform = new Transform();
		motionTransform.setIdentity();
		motionTransform.origin.set(new javax.vecmath.Vector3f(0, 10, 0));
		StaticObjectMotionState motionState = new StaticObjectMotionState(motionTransform, this);
		//DefaultMotionState groundMotionState = new DefaultMotionState(motionTransform);
		javax.vecmath.Vector3f fallInertia = new javax.vecmath.Vector3f(0,0,0);
		collisionShape.calculateLocalInertia(1, fallInertia);
		RigidBodyConstructionInfo fallRigidBodyCI = new RigidBodyConstructionInfo(1, motionState, collisionShape, fallInertia);
		rigidBody = new RigidBody(fallRigidBodyCI);
I just can't find the problem. Am I missing something?
Thank you already in advance,
Greetings!
herrmon
Posts: 4
Joined: Tue Sep 24, 2013 9:21 am

Re: Box not rotating correctly

Post by herrmon »

Update:

Even with a BoxShape size of 1.0f, 1.0f, 1.0f the Box isn't falling correctly. It's also uneven on the ground, but only that little that I didn't notice before.

Even with the GLDebugDrawer provided with the "BasicDemo" demo of the jBullet package, the box is NOT falling correctly, so the problem is definitly related to the physics, not the graphics.

Image

Any ideas what the problem could be?
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Box not rotating correctly

Post by xexuxjy »

Can you post your full code anywhere? in particular

StaticObjectMotionState

which you seem to be using for the non-static box as well as the ground shape?

Thanks.
herrmon
Posts: 4
Joined: Tue Sep 24, 2013 9:21 am

Re: Box not rotating correctly

Post by herrmon »

Thanks for your answer!

The full source code of the StaticObjectMotionState is posted above in the first post along with every other physics-related code in the program.
I can of course post any other code you want - but I think that wouldn't be helpful.

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

Re: Box not rotating correctly

Post by xexuxjy »

I tried your sample (though using default motion states rather than your version , just so i could use the standard renderer).
I did notice that a 1x2x1 box behaved very oddly on a static plane ground shape (ended up resting on it's edge). If I made the ground shape a box then the collisions looked a lot better.
Unfortunately the JBullet stuff is pretty old, so fixing the static plane issue may be tricky

Can you try it with a box ground shape and see if that helps?
herrmon
Posts: 4
Joined: Tue Sep 24, 2013 9:21 am

Re: Box not rotating correctly

Post by herrmon »

Thanks for your help!

Even with a Box shape I get the same (or similar) results. Maybe it;s really a jBullet issue.