[Solved] Problem with physics (bodies not moving)

ThePatchou
Posts: 3
Joined: Sat Jan 18, 2014 4:10 pm

[Solved] Problem with physics (bodies not moving)

Post by ThePatchou »

Hello everyone !

First, sorry for my english, I'm not very good. :(

To begin, I'm creating a little game for Android using Java with an library called libGDX. I use a port of the Bullet library to use it on Android.

The problem is that no bodies are moving in my world. I mean that everything stay fixed, nothing moves.

That is my code about the world witch contains the entities and the bullet classes :

Code: Select all

private List<Entity> entities;
	private btDynamicsWorld dynamicsWorld;
	private btBroadphaseInterface broadphase = new btDbvtBroadphase();
	private btDefaultCollisionConfiguration config = new btDefaultCollisionConfiguration();
	private btCollisionDispatcher dispatcher = new btCollisionDispatcher(config);
	private btSequentialImpulseConstraintSolver solver = new btSequentialImpulseConstraintSolver();
	
	public World(Vector3 gravity) {
		broadphase = new btDbvtBroadphase();
		config = new btDefaultCollisionConfiguration();
		dispatcher = new btCollisionDispatcher(config);
		solver = new btSequentialImpulseConstraintSolver();
		
		dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, config);
		dynamicsWorld.setGravity(gravity);
		
		entities = new ArrayList<Entity>();
	}
	
	public void addEntity(Entity entity) {
		entities.add(entity);
		dynamicsWorld.addRigidBody(entity.getBody());
	}
	
	public void removeEntity(Entity entity) {
		entities.remove(entity);
		dynamicsWorld.removeRigidBody(entity.getBody());
	}
	
	public void update() {
		dynamicsWorld.stepSimulation(0.001f);
		
		for (Entity entity : entities) {
			entity.updateTransform();
		}
	}
	
	public void render(ModelBatch batch, Environment environment) {
		if (environment != null) {
			for (Entity entity : entities)
				batch.render(entity.getModelInstance(), environment);
		} else {
			for (Entity entity : entities)
				batch.render(entity.getModelInstance());
		}
	}
	
	public void dispose() {
		dynamicsWorld.dispose();
		broadphase.dispose();
		config.dispose();
		dispatcher.dispose();
		solver.dispose();
		
		for (Entity entity : entities)
			entity.dispose();
		entities.clear();
	}
}
And that is the entity class.

Code: Select all

package ca.thepatchou.theball.entities;

import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.bullet.collision.btCollisionShape;
import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody;
import com.badlogic.gdx.physics.bullet.dynamics.btRigidBodyConstructionInfo;
import com.badlogic.gdx.physics.bullet.linearmath.btMotionState;
import com.badlogic.gdx.utils.Disposable;

public abstract class Entity implements Disposable {
	
	protected class MotionState extends btMotionState {
		
		public void setWorldTransform(Matrix4 matrix) {
			modelInstance.transform.set(matrix);
		}
		
		public void getWorldTransform(Matrix4 matrix) {
			matrix.set(modelInstance.transform);
		}
	}
	
	private ModelInstance modelInstance;
	private btRigidBody body;
	private Material material;
	
	public Entity() {
		
	}
	
	public Entity create(Vector3 position, Quaternion rotation, float mass, Material material, Model model, btCollisionShape shape) {
		this.material = material;
		
		shape.calculateLocalInertia(mass, new Vector3(0, 0, 0));
		
		modelInstance = new ModelInstance(model);
		modelInstance.transform.set(new Matrix4().translate(position).rotate(rotation));
		
		btRigidBodyConstructionInfo info = new btRigidBodyConstructionInfo(0, new MotionState(), shape, new Vector3());
		body = new btRigidBody(info);
		
		return this;
	}
	
	public void updateTransform() {
		modelInstance.transform.set(body.getWorldTransform());
	}
	
	public void dispose() {
		modelInstance.model.dispose();
		body.getMotionState().dispose();
		body.dispose();
	}
	
	public ModelInstance getModelInstance() {
		return modelInstance;
	}
	
	public Matrix4 getTransform() {
		Matrix4 matrix = new Matrix4();
		body.getMotionState().getWorldTransform(matrix);
		return matrix;
	}
	
	public btRigidBody getBody() {
		return body;
	}
	
	public Material getMaterial() {
		return material;
	}
}
It creates the entity in the "create" method. It instanciates all bullet objects to make the entity working.

This is the override code for creating the sphere and the bullet sphere shape :

Code: Select all

	public Entity create(Vector3 position, Quaternion rotation, float mass, Material material, float radius) {
		Model model = new ModelBuilder().createSphere(radius * 2, radius * 2, radius * 2, 20, 20, GL20.GL_LINES, material, Usage.Position | Usage.Normal);
		btCollisionShape shape = new btSphereShape(radius);
		
		return super.create(position, rotation, mass, material, model, shape);
	}

Code: Select all

world = new World(new Vector3(0f, -10f, 0f));
		world.addEntity(new StaticPlaneEntity().create(new Material(ColorAttribute.createDiffuse(Color.BLUE)), 0f));
		world.addEntity(player = (SphereEntity) new SphereEntity().create(new Vector3(0f, 4f, 0f), new Quaternion(), 2f, new Material(ColorAttribute.createDiffuse(Color.RED)), 3f));
This little code creates the entities (a static plane entity and a sphere). It is located at my game class.

Do you guys have any ideas about why the ball is sticked and not moving ?

If you want more code or if I should post my problem somewhere else, just tell me. :)
Thank you !

Note : I did some Google search and I follow your hello world tutorial, but nothing worked, everything stay the same.
Last edited by ThePatchou on Wed Jan 22, 2014 10:38 pm, edited 1 time in total.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Problem with physics (bodies not moving)

Post by Basroil »

Looks like you calculated the inertia to "new Vector3" rather than making a new Vector3 and passing that. In the constructor info you again call "new Vector3", so you end up having a sphere with infinite inertia, no wonder it doesn't want to move! :wink:

Just make the vector first, pass it to inertia calculation, then use the same vector in constructor info.
ThePatchou
Posts: 3
Joined: Sat Jan 18, 2014 4:10 pm

Re: Problem with physics (bodies not moving)

Post by ThePatchou »

Hello !
I tried to make what you suggested me, but my ball is still not moving.
I change this in my code :

Code: Select all

		Vector3 inertia = new Vector3(0f, 0f, 0f); // The inertia value.
		
		shape.calculateLocalInertia(mass, inertia);
		
		modelInstance = new ModelInstance(model);
		modelInstance.transform.set(new Matrix4().translate(position).rotate(rotation));
		
		btRigidBodyConstructionInfo info = new btRigidBodyConstructionInfo(0, new MotionState(), shape, inertia);
		body = new btRigidBody(info);
I create a Vector3 and I use it for the inertia calculation and I pass it in the CI too.
I tried also some little things to make sure I wasn't doing a mistake (like making sure that my ball isn't on the ground already).
Thank you for taking some little time to help me. :)
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Problem with physics (bodies not moving)

Post by Basroil »

Now you set it to infinite mass! You really like making things infinite :lol:

Just make sure you pass positive non-zero mass into construction info, since zero signifies infinite (mass or inertia).
ThePatchou
Posts: 3
Joined: Sat Jan 18, 2014 4:10 pm

Re: Problem with physics (bodies not moving)

Post by ThePatchou »

Hello !
Now I see some debug values changing ! My ball is now moving slowy but correctly. :)

Well, about the infinite mass, I was thinking to something : if the ball has infinite mass, then it should pass thought everything, no ?

Thank you anyway, I can (finally) continue my game after one month of searchs. :D
(maybe I will post my game at this forum one day...)

Edit : Well, my ball is passing in the ground, but I'm going to fix it myself. Thanks again :)
c6burns
Posts: 149
Joined: Fri May 24, 2013 6:08 am

Re: Problem with physics (bodies not moving)

Post by c6burns »

ThePatchou wrote:Well, about the infinite mass, I was thinking to something : if the ball has infinite mass, then it should pass thought everything, no ?
No :P