Hello,
I have a small test case, to experiment with physics.
I create floor (box mesh) and make it solid in the simpleInitApp()
I also added some logic, that when i click the mouse button, a sphere is shot in the direction of the camera.
This is all working fine, the sphere is bouncing on the floor etc.
But then I tried to move the floor creation to a separate appstate, but now the sphere is going through the floor.
Can someone explain me what I am doing wrong?
[java]
public class Particles extends SimpleApplication {
public static void main(String[] args) {
Particles particles = new Particles();
particles.start();
}
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(50f);
// Let there be light
DirectionalLight sun = new DirectionalLight();
sun.setColor(ColorRGBA.White);
sun.setDirection(Vector3f.UNIT_Y.negate());
AmbientLight ambientLight = new AmbientLight();
ambientLight.setColor(ColorRGBA.White);
rootNode.addLight(sun);
rootNode.addLight(ambientLight);
// Add user mappings
inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "shoot");
stateManager.attach(new FloorState());
}
private ActionListener actionListener = new ActionListener() {
@Override
public void onAction(String s, boolean b, float v) {
if (s.equals("shoot") && !b) {
shootFireball();
}
}
};
private void shootFireball() {
// Fireball
Spatial fireball = assetManager.loadModel("textures/fireball/fireball.obj");
fireball.setLocalScale(0.25f);
fireball.setLocalTranslation(cam.getLocation());
rootNode.attachChild(fireball);
fireball.addControl(new FireballControl(cam.getDirection()));
}
}
[/java]
[java]
public class FloorState extends AbstractAppState {
private BulletAppState bulletAppState;
private RigidBodyControl solidPhysics;
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
// Create a physical game
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
// Create a solid control with a mass of zero
solidPhysics = new RigidBodyControl(0f);
// Mesh
Box boxMesh = new Box(Vector3f.ZERO, 1, 1, 1);
// Floor material
Material floorMaterial = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
floorMaterial.setColor("Color", ColorRGBA.Blue);
// Wall material
Material wallMaterial = floorMaterial.clone();
wallMaterial.setColor("Color", ColorRGBA.Green);
// Container for solid models
Node solidNodes = new Node("solid");
// Floor spatial
Geometry floor = new Geometry("floor", boxMesh);
floor.setMaterial(floorMaterial);
floor.setLocalScale(25f, 0f, 25f);
// Wall spatial
Geometry wall = new Geometry("wall", boxMesh);
wall.setMaterial(wallMaterial);
wall.setLocalScale(5f, 0f, 5f);
// Rotate 90°
Quaternion rotate90 = new Quaternion();
rotate90.fromAngleAxis(90 * FastMath.DEG_TO_RAD, Vector3f.UNIT_X);
wall.setLocalTranslation(0f, 5f, 0f);
wall.setLocalRotation(rotate90);
// Add models to the container
solidNodes.attachChild(floor);
solidNodes.attachChild(wall);
// Make the container solid
solidNodes.addControl(solidPhysics);
// Add the solid physics to the physics space
bulletAppState.getPhysicsSpace().add(solidPhysics);
// Attach solidnodes to rootnode
((SimpleApplication) app).getRootNode().attachChild(solidNodes);
}
}
[/java]
kr,
Remy