Hi,
I put together this small test case. I’m using two cameras, one with parallel projection and one with perspective projection. Don’t ask me about the values as they are taken from a specific scenario and don’t make much sense probably.
This test case works with nightly build from 2012-09-15 but doesn’t work with the latest build (2013-01-02).
[java]package cz.unknown.jme;
import com.jme3.app.SimpleApplication;
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.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
public class CollisionBugTest extends SimpleApplication {
private Camera perspectiveCam = null;
private ViewPort perspectiveViewPort = null;
private Node perspectiveNode = null;
@Override
public void simpleInitApp() {
perspectiveNode = new Node("Perspective node");
perspectiveCam = new Camera(settings.getWidth(), settings.getHeight());
perspectiveViewPort = renderManager.createMainView("Perspective port", perspectiveCam);
perspectiveViewPort.attachScene(perspectiveNode);
perspectiveViewPort.setClearFlags(false, false, false);
flyCam.setEnabled(false);
initCamera();
initScene();
registerInputListener();
}
/**
* @param args
*/
public static void main(String[] args) {
CollisionBugTest test = new CollisionBugTest();
test.start();
}
private void initScene() {
Box b = new Box(Vector3f.ZERO, 10000, 1, 10000);
Geometry geom = new Geometry("Plane", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
ColorRGBA color = new ColorRGBA(1, 0, 0, 0f);
mat.setColor("Color", color);
geom.setMaterial(mat);
perspectiveNode.attachChild(geom);
DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(-0.1f, -0.7f, -1).normalizeLocal());
dl.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));
perspectiveNode.addLight(dl);
}
private void initCamera() {
// parallel camera
cam.setParallelProjection(true);
cam.setFrustum(0.1f, 1000, -640, 640, 360, -360);
Vector3f loc = new Vector3f(640, 360, 2.0f);
Vector3f left = new Vector3f(-1.0f, 0.0f, 0.0f);
Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
Vector3f dir = new Vector3f(0.0f, 0.0f, -1.0f);
cam.setLocation(loc);
cam.setFrame(loc, left, up, dir);
// perspective camera
perspectiveCam.setFrustum(1000.1f, 2000, -640, 640, 360, -360);
Vector3f loc2 = new Vector3f(640, 360, 1000f);
Vector3f up2 = new Vector3f(0, 1, 0);
Vector3f lookAt = new Vector3f(640, 360, 0f);
perspectiveCam.setLocation(loc2);
perspectiveCam.lookAt(lookAt, up2);
}
@Override
public void simpleUpdate(float tpf) {
perspectiveNode.updateLogicalState(tpf);
perspectiveNode.updateGeometricState();
}
private void registerInputListener() {
inputManager.addMapping("click", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(new ActionListener() {
@Override
public void onAction(String name, boolean isPressed, float tpf) {
if("click".equals(name) && isPressed) {
CollisionResults results = new CollisionResults();
Vector2f click2d = inputManager.getCursorPosition();
Vector3f click3d = perspectiveCam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
Vector3f dir = perspectiveCam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();
printVector(click3d);
printVector(dir);
Ray ray = new Ray(click3d, dir);
perspectiveNode.collideWith(ray, results);
for (CollisionResult collisionResult : results) {
System.out.println(collisionResult.getGeometry().getName());
}
System.out.println("-------");
}
}
}, "click");
}
public static void printVector(Vector3f vect) {
System.out.println(vect.x + "/" + vect.y + "/" + vect.z);
}
}
[/java]