Problem with switching from static to dynamic

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]

second part of the code…:
[java]
private void initKeys() {
actionListener = new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals(“Shoot”) && !isPressed) {
CollisionResults results = new CollisionResults();
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
castables.collideWith(ray, results);
if (results.size() > 0) {
Geometry victim = results.getClosestCollision().getGeometry();
victim.removeControl(victim.getControl(0));
bulletAppState.getPhysicsSpace().removeAll(victim);
victim.addControl(new RigidBodyControl(10f));
victim.getControl(RigidBodyControl.class).setFriction(0.6f);

                    bulletAppState.getPhysicsSpace().add(victim);    
                    victim.getControl(RigidBodyControl.class).clearForces();
                }
            }                
        }
    };
    inputManager.addMapping("Shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    inputManager.addListener(actionListener, "Shoot");
}

[/java]

Why do you remove the control to add a new one ?
Why don’t you simply change the mass of your control ?

As for your question, the ray is not physical. It should not cast any force to the cubes.

The reason of your weird movement is not clear. Maybe you have some overlap in your cubes ?
Maybe you should set collisions groups on your cubes ? I have add a strange behavior with Linux, where there was no collision if I didn’t set the collision with group 1… Same code worked nicely on windows…

hm when I add “1f” mass nothing happens, if I add “100f” the cube just vanishs. O.o

[java]
private void initKeys() {
actionListener = new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals(“Shoot”) && !isPressed) {
CollisionResults results = new CollisionResults();
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
castables.collideWith(ray, results);
if (results.size() > 0) {
Geometry victim = results.getClosestCollision().getGeometry();
victim.getControl(RigidBodyControl.class).setMass(1f);
// victim.removeControl(victim.getControl(0));
// bulletAppState.getPhysicsSpace().removeAll(victim);
// victim.addControl(new RigidBodyControl(10f));
// victim.getControl(RigidBodyControl.class).setFriction(0.6f);
//
// bulletAppState.getPhysicsSpace().add(victim);
// victim.getControl(RigidBodyControl.class).clearForces();
}
}
}
};
inputManager.addMapping(“Shoot”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, “Shoot”);
}
[/java]

I tested my previous code with a single cube. If i shoot it, it sligthly rotates and moves in a direction befor it starts falling.

Replace the " if(results.size()>0){ … }" thing with:

[java]
if (results.size() > 0) {

                Geometry victim = results.getClosestCollision().getGeometry();
                
                bulletAppState.getPhysicsSpace().remove(victim.getControl(0));
                
                victim.removeControl(victim.getControl(0));
                victim.addControl(new RigidBodyControl(1f));
                
                ((RigidBodyControl)victim.getControl(0)).setFriction(0.5f);
                
                bulletAppState.getPhysicsSpace().add(victim.getControl(0));
                
                /*Cubes around the "victim" won't notice that it has been shot, they all have to get activated, otherwise it can happen, that already hit objects float and dont fall*/
                for(int i = 0; i<castables.getChildren().size(); i++){
                    ((RigidBodyControl)castables.getChild(i).getControl(0)).activate();
                }
            }  

[/java]

You also have to create some space between the cubes:

[java]
placeCube(new Vector3f(-x * a * 2 +0.05fx, -y * a * 2 +0.05fy, -z * a * 2 +0.05f*z),
1,
new ColorRGBA(new Random().nextFloat(), new Random().nextFloat(), new Random().nextFloat(), 1f));
[/java]

And you should check out for a good value of the friction, maybe it is too high.

Also start with a simpler ase if that does not help, maybee two cubes, and see if you figure out strange bahaviour then…