Detaching child and collision (solved)

Hi.



I’ve got a character and a node “trap” with 20 hierarchical nodes(OBtrap.001, OBTrap.002, OBTrap.003…). I want OBTrap.*** has been detached when character collides with it. But when the character collides with any node, so about 4 nodes detaches instead of one. I did not use bullet physics but possibly you recommend to use it?



This is my code based on TesttriangleCollision and HelloPicking:



[java]

Spatial character;

Node trap;









@Override

public void simpleUpdate(float tpf) {



rootNode.updateGeometricState();











CollisionResults results = new CollisionResults();





BoundingVolume bv = character.getWorldBound();

trap.collideWith(bv, results);











for (int i = 0; i < results.size(); i++) {

if (results.size() > 0) {



String closest = results.getCollision(i).getGeometry().getName();

Spatial u = trap.detachChildAt(i);





System.out.println("Collision " + u);





}

}





}





[/java]





This is my log when the character collides with only one node:

08.01.2011 2:22:24 com.jme3.scene.Node detachChildAt

INFO: Child removed.

Collision OBtrap.014 (Node)

Collision OBtrap.016 (Node)

08.01.2011 2:22:24 com.jme3.scene.Node detachChildAt

INFO: Child removed.

Collision OBtrap.011 (Node)

08.01.2011 2:22:24 com.jme3.scene.Node detachChildAt

INFO: Child removed.

Collision OBtrap.013 (Node)

08.01.2011 2:22:24 com.jme3.scene.Node detachChildAt

INFO: Child removed.

Collision OBtrap.017 (Node)



I did not use bullet as i’ve been doing simple scroll-shooter(flying raven). But possibly you will recommend to use bullet to get right collision results?

I don’t understand exactly what it is you want to do, or what the expected results are.

Do you want only one node to be detached per collision? Does it seems like you’re only colliding with one node, but you get several detachements?

In that case it could be that the bounding volume is not updated/correct. Also, if you want to force it to only drop one node, you don’t need to parse all results, but rather only get the first one.



I think Bullet is probably overkill for that type of project, but i’m not that familiar with it either, so i can’t really say.



Edit: Oh, and i see now that you detach your node based on the index of the collision. If you do that, the first collision will always detach the first child of the node.

You’re already getting the name of the colliding geometry. One way you could do it is to name the geometries of the nodes (i’m assuming you have one per node), the same name as the node, ie OBTrap001, etc. Then rather than using trap.detachChildAt(), use trap.detachChild(closest).

Hi Rikard, Thank you for reply. You answered exactly right about collision and detaching. I get you about indexes and names. But there is one thing about names. I use 20 links to object (not copies) for traps, so names of geometry are same.



http://img545.imageshack.us/img545/4182/92911567.jpg - this is my hierarchy.



Could you recommend how to solve such a thing?

A bit risky for NPE’s, but you could try to get the parent of the colliding geometry, ie:



[java]

Spatial closest = results.getCollision(0).getGeometry();

if(closest.getParent() != null) closest.getParent();



[/java]



or, you could get the collision point, and then see which node’s bounds volume that contains it.

RESPECT RESPECT RESPECT!!! THANK YOU RICKARD A LOT!!! You saved me!!!



http://www.youtube.com/watch?v=SUTan093nEw - my game :slight_smile:



[java] @Override

public void simpleUpdate(float tpf) {

rootNode.updateGeometricState();



CollisionResults results = new CollisionResults();

BoundingVolume bv = character.getWorldBound();

trap.collideWith(bv, results);

boolean u = false;



if (results.size() > 0) {

Spatial closest = results.getCollision(0).getGeometry();

if(closest.getParent() != null)

u = closest.getParent().removeFromParent();



System.out.println("Collision " + u);



}

}[/java]

1 Like