I started to play around a little bit with the phyic engine.
Rigth now i try to change a static object to a dynamic object. My problem is when i switch the RigidBodyControls of the object, it starts to act “weird”.
The first thing my code does is to set up some cube which are stacked on each other. All of them have static RigidBodyControl on them.
When I shoot a cube it removes the static RigidBodyControl with a dynamic one (mass > 0).
For instance if I shoot some cubes positioned on the top I thought they will just stay where they are, but insteat they get stuck in the cubes underneath them.
I found out if I shoot a cube which falls down, it changes its direction and rotation for a short moment before it starts to fall down again. Has it to do something with the ray I shot at the object?
Here is my code:
[java]
public void simpleInitApp() {
viewPort.setBackgroundColor(new ColorRGBA(new Random().nextFloat(), new Random().nextFloat(), new Random().nextFloat(), 1f));
this.castables = new Node(“Castables”);
flyCam.setMoveSpeed(100f);
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
float a = 2.0f;
cube = new Box(Vector3f.ZERO, a, a, a);
int maxX = 5;
int maxY = 5;
int maxZ = 5;
for (int z = 0; z < maxZ; z++) {
for (int y = 0; y < maxY; y++) {
for (int x = 0; x < maxX; x++) {
placeCube(new Vector3f(-x * a * 2, -y * a * 2, -z * a * 2),
1,
new ColorRGBA(new Random().nextFloat(), new Random().nextFloat(), new Random().nextFloat(), 1f));
}
}
}
placeCube(new Vector3f(-3 * 2 * 2, +8 * 2 * 2, -3 * 2 * 2),
1,
new ColorRGBA(new Random().nextFloat(), new Random().nextFloat(), new Random().nextFloat(), 1f));
rootNode.attachChild(this.castables);
initCrossHairs();
this.initKeys();
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
}
…
private Geometry placeCube(Vector3f pos, float scale, ColorRGBA color) {
Geometry g = new Geometry("cube", cube);
g.scale(scale);
g.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"));
g.getMaterial().setColor("Color", color);
g.addControl(new RigidBodyControl(0f));
bulletAppState.getPhysicsSpace().add(g);
g.getControl(RigidBodyControl.class).setPhysicsLocation(pos);
this.castables.attachChild(g);
return g;
}
[/java]