Hi guys,
I was wondering how lock() and unlock() methods should be used. I have a bunch of boxes which I add to a static node. Then I lock the static node. I would like however to detach/attach boxes to the static node later on but I am seeing some unexpected (by me) behavior. The collision geometry doesn't seem to be updated if I have used the lock() method.
I think I am missing something or maybe I shouldn't use lock() at all in this case?
Below is an example of what I mean if somebody would like to take a quick look and tell me what I am doing wrong.
Thanks a lot.
import java.util.logging.Level;
import java.util.logging.Logger;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.StaticPhysicsNode;
import com.jmex.physics.util.SimplePhysicsGame;
import com.jme.scene.shape.Box;
import com.jme.input.KeyBindingManager;
import com.jme.bounding.BoundingBox;
public class Main extends SimplePhysicsGame {
private Box[][] tiles = new Box[4][4];
private StaticPhysicsNode staticNode;
private DynamicPhysicsNode dynamicNode;
private Box cube;
protected void simpleInitGame() {
KeyBindingManager.getKeyBindingManager().set("down",KeyInput.KEY_J);
staticNode = getPhysicsSpace().createStaticNode();
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++){
tiles[i][j] = new Box("tile"+i+j,new Vector3f(),0.95f,0.25f,0.95f);
tiles[i][j].setModelBound(new BoundingBox());
tiles[i][j].updateModelBound();
tiles[i][j].getLocalTranslation().set(2*i, 0, 2*j);
staticNode.attachChild(tiles[i][j]);
}
staticNode.generatePhysicsGeometry();
// if I comment out the next 2 lines I don't have any problems
staticNode.lock();
staticNode.unlock();
dynamicNode = getPhysicsSpace().createDynamicNode();
cube = new Box("Cube",new Vector3f(), 1, 1, 1);
cube.setModelBound(new BoundingBox());
cube.updateModelBound();
dynamicNode.attachChild(cube);
dynamicNode.generatePhysicsGeometry();
dynamicNode.getLocalTranslation().set( 3, 5, 3);
dynamicNode.setAffectedByGravity(false);
rootNode.attachChild(staticNode);
rootNode.attachChild(dynamicNode);
staticNode.detachAllChildren();
staticNode.generatePhysicsGeometry();
}
@Override
protected void simpleUpdate() {
if (KeyBindingManager.getKeyBindingManager().isValidCommand("down",false)){
dynamicNode.setAffectedByGravity(true);
}
}
public static void main( String[] args ) {
Logger.getLogger( "" ).setLevel( Level.WARNING );
Main app = new Main();
app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
app.samples = 2;
app.start();
}
Main() {
stencilBits = 4;
}
}