Hey,
Today I'm trying to mouse-pick some objects. After searching through this forum and the jME examples I managed to get these two versions of code:
private void processPick() {
System.out.println("PICK!");
int mouse_x = MouseInput.get().getXAbsolute();
int mouse_y = MouseInput.get().getYAbsolute();
Vector2f screenPos = new Vector2f(mouse_x, mouse_y);
Vector3f startPoint = DisplaySystem.getDisplaySystem().getWorldCoordinates(screenPos, 0);
// Get the world location of that X,Y value - far
Vector3f endPoint = DisplaySystem.getDisplaySystem().getWorldCoordinates(screenPos, 1);
Ray ray = new Ray(startPoint, endPoint.subtract(startPoint));
TrianglePickResults results = new TrianglePickResults();
//results.setCheckDistance(true);
results.clear();
vehicles.findPick(ray, results);
if(results.getNumber() > 0) {
System.out.println("NUMBER: "+results.getNumber()+" OBJ: "+results.getPickData(0).getTargetMesh().getName());
}
}
And number two:
private void processPick2() {
System.out.println("PICK!");
Vector2f screenPos = new Vector2f();
//Get the position that the mouse is pointing to
screenPos.set(MouseInput.get().getXAbsolute(), MouseInput.get().getYAbsolute());
// Get the world location of that X,Y value
Vector3f worldCoords = display.getWorldCoordinates(screenPos, 1.0f);
// Create a ray starting from the camera, and going in the direction
// of the mouse's location
final Ray mouseRay = new Ray(cam.getLocation(), worldCoords
.subtractLocal(cam.getLocation()));
mouseRay.getDirection().normalizeLocal();
TrianglePickResults results = new TrianglePickResults();
results.setCheckDistance(true);
vehicles.findPick(mouseRay,results);
if(results.getNumber() > 0) {
System.out.println("NUMBER: "+results.getNumber()+" OBJ: "+results.getPickData(0).getTargetMesh().getName());
}
results.clear();
}
In both examples, the field vehicles is a Node which contains an Entity with a simple Box as Spatial.
In the first version, my vehicle is selected practically every time. I have to move my cursor to the other side of the map in order to be able to make a pick without hitting my object.
In the second version, I nearly can't pick my object. Sometimes, if I place the cursor far above the actual geometry I'm able to pick it, but this is not a constant event.
Maybe it's good to know that I'm currently using lex' strategicHandler in my application. I don't really know if it's affecting the way of picking something. If I compare the absolute x and y values of my mouse to those I'm getting from some jME example pick (therefore without strategicHandler), I can't notice much difference.. (+/- 30-60)
Thanking you in anticipation