GhostControl & PhysicsControl attached to the same Spatial

Hi,



I suppose there is something I don’t completely understand around GhostControl’s usage:



If I add any other PhysicsControl to a Spatial who already has a GhostControl, then GhostControl stops detecting collisions.



Here is the code:



[java]public class Main extends SimpleApplication {



GhostControl gc = new GhostControl(new BoxCollisionShape(new Vector3f(1,1,1)));



public static void main(String[] args) {

Main app = new Main();

app.start();

}



@Override

public void simpleInitApp() {

BulletAppState bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);



PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());



for (int i=0; i<6; i++){



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

Geometry geom = new Geometry(“Box”, b);



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

mat.setColor(“Color”, ColorRGBA.Blue);

geom.setMaterial(mat);

geom.setLocalTranslation(0, i*3, 0);



geom.addControl(new RigidBodyControl(3));



if (i=:3) {

geom.addControl(gc);

// geom.removeControl(RigidBodyControl.class);

}



bulletAppState.getPhysicsSpace().addAll(geom);

rootNode.attachChild(geom);



}

}



@Override

public void simpleUpdate(float tpf) {

System.out.println(gc.getOverlappingCount());

}



}[/java]



If I uncomment the line which removes the RigidBodyControl, the GhostControl reports the collisions fine.



I was thinking about whether I can add more then 1 PhysicsControl to a Spatial, but I’ve seen other posts where they did add more then one and it worked for them.



Obviously I could just simply add a node to the spatial and attach the Ghostcontrol to it but I would like to know the answer anyway.



I am sure the answer is simple but I just cant find it.





Thanks!!

1 Like

Well maybe the model is larger than the ghost control so it doesn’t collide anymore?

I’ve increased the CollisionShape:

GhostControl gc = new GhostControl(new BoxCollisionShape(new Vector3f(2,2,2)));

but it didn’t change anything



It works with an AbstractControl instance but not with a PhysicsControl instance.



I have already tried adding the controls in a different order to the spatial… nothing changed.

Ah, you use the addAll function, add the controls separately.

I am just trying to make the code as simle as possible:



[java]public class Main extends SimpleApplication {



GhostControl gc = new GhostControl(new BoxCollisionShape(new Vector3f(2,2,2)));



public static void main(String[] args) {

Main app = new Main();

app.start();

}



@Override

public void simpleInitApp() {

BulletAppState bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);



PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());



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

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

mat.setColor(“Color”, ColorRGBA.Blue);



//1st

Geometry geom = new Geometry(“Box”, b);



geom.setMaterial(mat);

geom.setLocalTranslation(0, 3, 0);



geom.addControl(gc);

// geom.addControl(new RigidBodyControl(3));





//2nd

Geometry geom1 = new Geometry(“Box”, b);



geom1.setMaterial(mat);

geom1.setLocalTranslation(0, 10, 0);



geom1.addControl(new RigidBodyControl(3));





//adding them to the scene

bulletAppState.getPhysicsSpace().addAll(geom);

bulletAppState.getPhysicsSpace().addAll(geom1);

rootNode.attachChild(geom);

rootNode.attachChild(geom1);



}



@Override

public void simpleUpdate(float tpf) {

System.out.println(gc.getOverlappingCount());

}



}[/java]



The code above works.

As soon as I unncomment the line which adds RigidBodyControl to “geom”, GhostControl stops detecting collisions.



I am using latest svn build



Are you sure that I can have more then 1 PhysicsControl attached to a specific spatial?

I’ve just read your comment Normen!



Adding the the controls separately works flawlessly!



Problem solved, thanks very much!!! :slight_smile:

@nagper said:
Are you sure that I can have more then 1 PhysicsControl attached to a specific spatial?


That made me grin, in a big way. :D

Normen isn't always right, but you can bet you last pair of clean underwear on him. ;) Most times anyway. lol
@madjack said:
That made me grin, in a big way. :D

Normen isn't always right, but you can bet you last pair of clean underwear on him. ;) Most times anyway. lol


:) Ok.... silly question cancelled!

Besides, could somebody please point me how to use the addAll method wisely?



Is addAll just iterates over the nodes and adds the first PhisicsControl attached to the current node?



Or why it doesn’t detect all the controls attached to the spatial?

@nagper said:
Is addAll just iterates over the nodes and adds the first PhisicsControl attached to the current node?

Yeah thats what it does.

Ok, thanks!

@madjack said:
That made me grin, in a big way. :D

Normen isn't always right, but you can bet you last pair of clean underwear on him. ;) Most times anyway. lol

its hard to argue with normen, he is never "wrong". Either he somehow convinces the other person he is right, or the other person just gives up arguing ;)
@wezrule said:
its hard to argue with normen, he is never "wrong". Either he somehow convinces the other person he is right, or the other person just gives up arguing ;)

Nah thats not true, the episodes where I am wrong are just too short to notice as I'm normally saying that I was wrong right away when I see it (thats why nobody remembers "owning" me ;)). The other episodes are like the ones apple has where I have a good reason to do or say something but others just won't accept that (like the eclipse project file) :P With bullet in jme I have an easy time being right as I wrote all of this stuff ^^