Sphere body spinning on contact?

Terazilla
Posts: 2
Joined: Mon Jan 02, 2012 9:19 pm

Sphere body spinning on contact?

Post by Terazilla »

I'm working on a simple project that basically just needs a few bouncing basketballs to behave well. I'm currently using jbullet for this, and for the most part everything looks and behaves pretty well. The ball spawns and bounces appropriately, everything's at the correct scale, gravity works fine, etc. I'm moving it through the environment by applying impulses based on accelerometer input from an Android phone.

However, the ball never seems to rotate. I can see it spin (and have proper english when it bounces) if I set angular velocity, but on its own it never rotates at all. I would expect that if the object has lateral motion, and comes into contact with a wall or floor, it would appropriately spin based on the position of that contact.

Is there something I need to enable/disable for this? Does a sphere body never do this? I've been looking all morning for what I might have missed.

For reference, here's the setup code for the world and ball:

WORLD:

Code: Select all

DefaultCollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);
Vector3f worldAabbMin = new Vector3f(-10,-10,-10);
Vector3f worldAabbMax = new Vector3f(10,10,10);
AxisSweep3 overlappingPairCache = new AxisSweep3(worldAabbMin, worldAabbMax);
SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver();

dynamicWorld = new DiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
dynamicWorld.setGravity(new Vector3f(0,0,-10.0f)); // earth gravity is 9.8 m/s squared
dynamicWorld.getDispatchInfo().allowedCcdPenetration = 0f;

CollisionShape collisionShape;
RigidBodyConstructionInfo rbInfo;
RigidBody body;
		
Vector3f localInertia = new Vector3f(0, 0, 0);
float mass = 0f; // mass of 0 means Bullet will treat it as a non-moving obstacle-like thing with infinite mass
		
// Floor, upwards facing
collisionShape = new StaticPlaneShape(new Vector3f(0,0,1), 0);
		
// put the plane right at 0
Transform floorTransform = new Transform();
floorTransform.setIdentity();	
DefaultMotionState floorMotionState = new DefaultMotionState(floorTransform);
		
rbInfo = new RigidBodyConstructionInfo(mass, floorMotionState, collisionShape, localInertia);
rbInfo.restitution = 0.65f; // how 'bouncy' the surface is
rbInfo.friction = 2f;

body = new RigidBody(rbInfo);
dynamicWorld.addRigidBody(body);
BALL:

Code: Select all

CollisionShape ballShape = new SphereShape(0.12f); //radius of a basketball in m 

Transform startTransform = new Transform();
startTransform.setIdentity();
startTransform.origin.set( new Vector3f( this.origin.x, this.origin.y, this.origin.z) );

float mass = 0.55f; // Wikipedia says a basketball should be minimally 550 grams in weight!
Vector3f localInertia = new Vector3f(0, 0, 0);
DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo( mass, myMotionState, ballShape, localInertia );
rbInfo.restitution = 0.95f; // Restitution of 1.0 is, in theory, perfectly bouncy with no loss of energy
rbInfo.friction = 0.1f;
johnwalt
Posts: 1
Joined: Tue Jan 03, 2012 5:14 am

Re: Sphere body spinning on contact?

Post by johnwalt »

friction between ball and obstackle?
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Sphere body spinning on contact?

Post by Erwin Coumans »

You should initialize localInertia for dynamic (mass>0) objects, check out any of the Bullet demos.

Code: Select all

	btVector3 localInertia(0,0,0);
		if (isDynamic)
			colShape->calculateLocalInertia(mass,localInertia);

Thanks,
Erwin
Terazilla
Posts: 2
Joined: Mon Jan 02, 2012 9:19 pm

Re: Sphere body spinning on contact?

Post by Terazilla »

Erwin: That was it! Thanks very much sir!