How to remove physics?

Hello,

I try to make sandbox game like Minecraft, but I don’t know, how to remove physic from destroyed cube.

[java] results = new CollisionResults();



ray = new Ray(cam.getLocation(), cam.getDirection());



rootNode.collideWith(ray, results);

if (results.getCollision(0).getDistance() < 10) {

results.getCollision(0).getGeometry().removeFromParent();

System.out.println(results.getCollision(0).getGeometry().getControl(0));

//deleting physics

results.getCollision(0).getGeometry().removeControl(brick_phy.getClass());

//deleting cube

results.getCollision(0).getGeometry().removeFromParent();

{

float x = results.getCollision(0).getContactPoint().getX();

float y = results.getCollision(0).getContactPoint().getY();

float z = results.getCollision(0).getContactPoint().getZ();



x = x / 2;

y = y / 2;

z = z / 2;



int xi = (int) x;

int yi = (int) y;

int zi = (int) z;



mapa[xi][yi][zi] = 0;

}



}



CollisionResult closest = results.getClosestCollision();



mark.setLocalTranslation(closest.getContactPoint());

rootNode.attachChild(mark);[/java]

The same way you create it, by removing it from the physics space… :roll:

See https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:physicsQuote from there:

You remove physical objects from the scene like this:

bulletAppState.getPhysicsSpace().remove(myPhysicsControl);

myModel.removeFromParent();



in your case:

replace:

results.getCollision(0).getGeometry().removeControl(brick_phy.getClass());

with:

bulletAppState.getPhysicsSpace().remove(results.getCollision(0).getGeometry().getControl(0)) ;

or

bulletAppState.getPhysicsSpace().remove(brick_phy); if brick_phy is results.getCollision(0).getGeometry()'s controller





the physicscontroller should be removed from physicsspace, not from the spatial.

little late there is guess:p

You can also setEnabled(false) to the control if you want to continue using it later, that also removes the object from the physicsspace. And when its enabled again, its location is set to the location of the spatial for convenience :slight_smile:

2 Likes

I’ll remember that :slight_smile:

Thanks you all and special thanks for makeshift.



chris_black