Custom Camera and Picking


I’m using a camera that is lightly adapted from

This is a scene editor type camera that is more appropriate for our use of JME in a visualization type application rather than a game. The camera itself does exactly what we want it to. However there is a problem with picking. I’m using some of the tutorial code for picking, namely:

CollisionResults results = new CollisionResults();
// Convert screen click to 3d position
Vector2f click2d = app.getInputManager().getCursorPosition();
Vector3f click3d = app.getCamera()
.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
Vector3f dir = app.getCamera()
.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d)
// Aim the ray from the clicked spot forwards.
Ray ray = new Ray(click3d, dir);
// Collect intersections between ray and all nodes in results list.
((SimpleApplication)app).getRootNode().collideWith(ray, results);
// (Print the results so we see what is going on:)
for (int i = 0; i < results.size(); i++) {
// (For each hit, we know distance, impact point, geometry.)
float dist = results.getCollision(i).getDistance();
final Vector3f pt = results.getCollision(i).getContactPoint();
String target = results.getCollision(i).getGeometry().getName();
System.out.println(“Selection #” + i + ": " + target + " at " + pt + “, " + dist
+ " WU away.”);

This works correctly if just zoom but as soon as I rotate or pan the scene the picking code no longer works. The camera app state code sets a rotation etc. on the camera and this is then incorporated into the camera’s view matrix and ultimately then into the camera’s viewProjectionMatrix. Camera.getWorldCoordinates uses that matrix so I can’t figure out why rotation / pan would be causing the problem. Any suggestions appreciated.



The code looks ok on quick inspection. You may want to put together a simple test case to illustrate the problem. When that code works then you can figure out what’s different… and in the slight chance that it doesn’t then you can post it here and we can point to any issues.

Just to make sure, are you absolutely sure the AbstractCameraController use the app.getCamera camera?

@nehon said: Just to make sure, are you absolutely sure the AbstractCameraController use the app.getCamera camera?

Thanks for replying. Yes, it does use the app.getCamera camera. I’ll put together a simple test case.

After some test case experimenting I discovered that app.getInputManager().getCursorPosition() when used with an AwtPanel returns the coordinates with the origin as the upper left corner rather than the lower left. This makes sense given that its an awt rather than opengl type display. Once I changed the picking code to invert the Y coordinate everything works as expected. Thanks again for the help.