I am trying to detect collision between two geometries using collideWith method. But when I move one of the geometry, the collision check still takes place at the old position. It’s as if the bounding box is not getting updated when I move the geometry using setLocalTranslation.
Here I am checking the collision between the blue and yellow cube. The blue cube turns red if there is a collision. I am moving the yellow cube around by pressing space bar. But even if the yellow box is moved, the blue box is detecting collision with yellow box at the location the yellow box was created(Vector3f.ZERO).
How do I solve the problem? I want blue cube to collide with the yellow cube at different positions.
Here is the code I am running,
import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.Trigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.shape.Box;
public class TestApplication extends SimpleApplication {
Trigger moveCube = new KeyTrigger(KeyInput.KEY_SPACE);
public static void main(String[] args) {
TestApplication app = new TestApplication();
app.showSettings = false;
app.start();
}
Geometry blue, yellow;
int position = 0;
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(25);
cam.setLocation(new Vector3f(-3.5037613f, 3.4540722f, 10.01301f));
cam.setRotation(new Quaternion(0.007143439f, 0.987654f, -0.1491698f, 0.047297053f));
inputManager.addMapping("moveCube", moveCube);
inputManager.addListener(listener, "moveCube");
blue = makeCube("blue", new Box(3f, .25f, .2f), Vector3f.ZERO, ColorRGBA.Blue);
blue.setLocalTranslation(-3, 0, 0);
rootNode.attachChild(blue);
yellow = makeCube("yellow", new Box(.5f,.5f,.5f), Vector3f.ZERO, ColorRGBA.Yellow);
rootNode.attachChild(yellow);
}
ActionListener listener = new ActionListener() {
@Override
public void onAction(String name, boolean isPressed, float tpf) {
if(isPressed)
position++;
switch (position % 4){
case 0:
yellow.setLocalTranslation(0,0,0);
break;
case 1:
yellow.setLocalTranslation(-2,2,0);
break;
case 2:
yellow.setLocalTranslation(-4,0,0);
break;
case 3:
yellow.setLocalTranslation(-2,-2,0);
break;
}
}
};
CollisionResults collisionResults = new CollisionResults();
CollisionResult result;
public void simpleUpdate(float tpf) {
blue.rotate(0, 0, tpf);
collisionResults.clear();
blue.collideWith(yellow.getModelBound(), collisionResults);
if(collisionResults.size() > 0){
result = collisionResults.getClosestCollision();
System.out.println("Collision with " + result.getGeometry().getName());
blue.getMaterial().setColor("Color", ColorRGBA.Red);
} else {
blue.getMaterial().setColor("Color", ColorRGBA.Blue);
}
}
public Geometry makeCube(String name, Mesh mesh, Vector3f location, ColorRGBA colorId){
Geometry geom = new Geometry(name, mesh);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", colorId);
geom.setMaterial(mat);
geom.setLocalTranslation(location);
return geom;
}
}