Hello,
Can someone tell me why the ray that the actionListener casts does not hit the bounding volume of the box? Here’s the code:
[java]
import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingSphere;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
public class BoundingSphereInnerRayIntersection extends SimpleApplication {
Box box;
Node boxNode;
Geometry boxGeometry;
Material boxMaterial;
BoundingSphere boxBoundingSphere;
Geometry mark;
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals(“castray”) && !keyPressed) {
CollisionResults results = new CollisionResults();
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
rootNode.collideWith(ray, results);
System.out.println("
Collisions? " + results.size() + "
");
for (int i = 0; i < results.size(); i++) {
float dist = results.getCollision(i).getDistance();
Vector3f pt = results.getCollision(i).getContactPoint();
String hit = results.getCollision(i).getGeometry().getName();
System.out.println("* Collision #" + i);
System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away.");
}
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
mark.setLocalTranslation(closest.getContactPoint());
rootNode.attachChild(mark);
} else {
// No hits? Then remove the red mark.
rootNode.detachChild(mark);
}
}
}
};
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(40);
inputManager.addMapping("castray", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "castray");
Sphere sphere = new Sphere(30, 30, 0.2f);
mark = new Geometry("mark", sphere);
Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mark_mat.setColor("Color", ColorRGBA.Red);
mark.setMaterial(mark_mat);
box = new Box(Vector3f.ZERO, 1, 1, 1);
boxNode = new Node();
boxGeometry = new Geometry("box", box);
boxMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
boxMaterial.setColor("Color", ColorRGBA.randomColor());
boxGeometry.setMaterial(boxMaterial);
boxBoundingSphere = new BoundingSphere(10f, box.getCenter());
box.setBound(boxBoundingSphere);
box.updateBound();
boxNode.attachChild(boxGeometry);
rootNode.attachChild(boxNode);
}
@Override
public void simpleUpdate(float tpf) {
}
public static void main(String[] args) {
BoundingSphereInnerRayIntersection b = new BoundingSphereInnerRayIntersection();
b.start();
}
}
[/java]
EDIT:
I read the Documentation and saw that BoundingVolume implements Collidable. Then i changed the code to the following:
[java]
boxBoundingSphere.collideWith(ray, results);
[/java]