(JME3) Remove an object from the Physics Space?

I am trying to make some kind of combination of the Picking tutorial and Collision/Physics tutorial. When you hit a object it be picked up and disappear from the scene. That works good, but you can still collide with where the object have been, so I want to remove or disable the collision thing for that object. Should I use a list of PhysicsNodes then? Removing the geometry from the Physics Space gives errors.



Here is some relevant code:



The creation code of a pickable box:

private void CreateBox(String name, Vector3f v, ColorRGBA color)

{

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

Geometry geom = new Geometry(name, b);

Material mat = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

mat.setColor(“m_Color”, color);

geom.setMaterial(mat);



PhysicsNode pn = new PhysicsNode(geom, new BoxCollisionShape(new Vector3f(1, 1, 1)), 0);

pn.setLocalTranslation(v);

pn.updateGeometricState();

rootNode.attachChild(pn);

getPhysicsSpace().add(pn);

}




Where I try to remove the objects:

private ActionListener actionListener = new ActionListener()

{

@Override

public void onAction(String binding, boolean value, float tpf)

{

//Move the player blah blah…



if (binding.equals(“Shoot”) && !value)

{

CollisionResults res = new CollisionResults();



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



rootNode.collideWith(r, res);



if (res.size() > 0)

{

// The closest collision point is what was truly hit:

CollisionResult closest = res.getClosestCollision();

pickup.attachChild(closest.getGeometry());

getPhysicsSpace().remove(pickup.getChild(0)); //Gives error!

rootNode.updateGeometricState();

}

}

}

};

The geometry that you get from picking is not the PhysicsNode. You’ll have to getParent() until you find the parent PhysicsNode that you can remove from the physicsspace.

Hope this helps,

Normen

Thanks, but the debugger tell me that getParent() have a PhysicsNode, but I got NullPointerException…

I dont understand what you are saying, but PhysicsNodes can be removed from the physicsspace. In your code you do some funny attachChild business, whats that supposed to be for? If you attach the spatial to another node it has another parent…

Oh, I thought I changed that. It was just something I tried. First I add the geometry into the pickupnode, that is a PhysicsNode and then I tried to remove what I added into the pickupnode from the physicsspace…

Only PhysicsNodes can be added or removed from the PhysicsSpace. So if your geometry is the child of a physics node then you have to remove that parent physics node from the physics space…

I think I got it! If I remove the node from the physicsspace before I add it to the pickup node it works. When I add it to the node, the parent changes!



So I replaced this code:

Code:
if (res.size() > 0) { // The closest collision point is what was truly hit: CollisionResult closest = res.getClosestCollision(); pickup.attachChild(closest.getGeometry()); getPhysicsSpace().remove(pickup.getChild(0)); //Gives error! rootNode.updateGeometricState(); }
With this:
Code:
if (res.size() > 0) { // The closest collision point is what was truly hit: CollisionResult closest = res.getClosestCollision(); PhysicsNode phy = (PhysicsNode)closest.getGeometry().getParent(); getPhysicsSpace().remove(phy); pickup.attachChild(phy); rootNode.updateGeometricState(); }

Thank you! :) Next problem is... how to add them again. I guess I either have to recreate the node or use lists.