GhostControl: What am I doing wrong?

Hi, I’m new to JmonkeyEngine.



I want my program to detect overlapping geometries. When two geometries overlap I’m only going to change the color of the geometries to indicate the collision, and not do any other physics stuff. Geometries will be moved by setLocalTranslation and setLocalRotation. Maybe I should use something other then GhostControl for detecting the overlaps?



The test program that i wrote is this:



[java]import com.jme3.scene.Node;

import com.jme3.app.Application;

import com.jme3.app.SimpleApplication;

import com.jme3.bullet.BulletAppState;

import com.jme3.bullet.PhysicsSpace;

import com.jme3.bullet.collision.shapes.BoxCollisionShape;

import com.jme3.bullet.control.GhostControl;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;



public class TestGhostOverlap extends SimpleApplication {



protected Node modelRoot;

protected Geometry geom1;



protected Geometry redBox, greenBox, debugBox;

protected GhostControl redGhost, greenGhost;



BulletAppState bulletAppState;



public PhysicsSpace getPhysicsSpace(){

return bulletAppState.getPhysicsSpace();

}



public TestGhostOverlap() {

super();

}



@Override

public void initialize() {

bulletAppState = new BulletAppState();

bulletAppState.startPhysics();

super.initialize();

stateManager.attach(bulletAppState);

}



public static void main(String[] args) {

Application app = new TestGhostOverlap();

app.start();

}



@Override

public void simpleInitApp() {

modelRoot = new Node();

rootNode.attachChild(modelRoot);



Box b1 = new Box(Vector3f.ZERO, 2, 0.2f, 0.2f);

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

setMaterial(geom1, ColorRGBA.Brown, “Common/MatDefs/Misc/SolidColor.j3md”);

modelRoot.attachChild(geom1);



Box ghostBox = new Box(new Vector3f(0, 0, 0), 0.4f, 1, 1);

redBox = new Geometry(“redBox”, ghostBox);

setMaterial(redBox, ColorRGBA.Red, “Common/MatDefs/Misc/WireColor.j3md”);

modelRoot.attachChild(redBox);



greenBox = new Geometry(“greenBox”, ghostBox);

setMaterial(greenBox, ColorRGBA.Green, “Common/MatDefs/Misc/WireColor.j3md”);

modelRoot.attachChild(greenBox);



redGhost = new GhostControl(new BoxCollisionShape(new Vector3f(0.4f, 1, 1)));

redBox.addControl(redGhost);

redGhost.setPhysicsSpace(getPhysicsSpace());

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

rdebMat.setColor(“Color”, ColorRGBA.Red);

redGhost.attachDebugShape(rdebMat);



greenGhost = new GhostControl(new BoxCollisionShape(new Vector3f(0.4f, 1, 1)));

greenBox.addControl(greenGhost);

greenGhost.setPhysicsSpace(getPhysicsSpace());

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

gdebMat.setColor(“Color”, ColorRGBA.Green);

greenGhost.attachDebugShape(gdebMat);



debugBox = new Geometry(“debugBox”, ghostBox);

setMaterial(debugBox, ColorRGBA.Red, “Common/MatDefs/Misc/WireColor.j3md”);

rootNode.attachChild(debugBox);

}



private void setMaterial(Geometry geometry, ColorRGBA color, String type) {

Material material = new Material(assetManager, type);

material.setColor(“Color”, color);

geometry.setMaterial(material);

}



@Override

public void simpleUpdate(float tpf) {

float curTime = timer.getTimeInSeconds();

modelRoot.rotate(0, 0, (float) Math.sin((double) curTime) / 1600);



greenBox.setLocalTranslation(2 * (float) Math.cos((double) curTime), 0, 0);



if (greenGhost.getOverlappingCount() > 0) {

geom1.getMaterial().setColor(“Color”, ColorRGBA.Yellow);

}

else {

geom1.getMaterial().setColor(“Color”, ColorRGBA.Brown);

}

fpsText.setText("Overlapping objects: " + greenGhost.getOverlappingObjects().toString());

}

}[/java]



I’m checking for collision between “greenGhost” and “redGhost”. It seems that the area of detection somehow is too big, so that a collision is detected also when the objects are close to eachother, but not actually overlapping. This is only a problem when i rotate the node to which both objects are attached. It may be that I must explicitly update the positions of the ghosts… how do I do this? (Calling the update() function in GhostControl doesn’t help)



Thank you for any help.

GhostControl will only check for overlaps of physics enabled objects. Use BoundingVolume.collideWith() for geometry based collision checks.

Ok. Thank you :). BoundingVolume.collideWith() seems better for what I’m trying to accomplish.



In my program there will be a lot of geometries that might possibly intersect. Can CollideWith be used to check for collision with any other Collidable object, without having to specify which objects i wish to check against?



public int collideWith(Collidable other, CollisionResults results) requests that I enter the other Collidable object. I want to check against all Collidable objects.

Do rootNode.collideWith(myTestShape) and you get all collisions in the world, yeah.