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