Problem with collision using the mouse position (solved)

Hi,

i’m trying to use collision via the position of the mouse (or in other words, i like to pick the object the player selects by clicking on it), but for some reason, no matter where i click, it always ends up near the heart of the coordinate system. see this picture for the problem.

[java]CollisionResults results = new CollisionResults();

Vector2f screenPos = new Vector2f(Mouse.getX(), Mouse.getY());

Vector3f startPoint = guiViewPort.getCamera().getWorldCoordinates(screenPos, 0); //DisplaySystem.getDisplaySystem().getWorldCoordinates(screenPos, 0);

Vector3f endPoint = guiViewPort.getCamera().getWorldCoordinates(screenPos, 1); //DisplaySystem.getDisplaySystem().getWorldCoordinates(screenPos, 1);

Ray ray = new Ray(startPoint, endPoint.subtract(startPoint));

//Ray ray = new Ray(cam.getLocation(), cam.getDirection());

System.out.println("X: " + Mouse.getX() + “, Y: " + Mouse.getY());

results.clear();

rootNode.collideWith(ray, results);

// 4. Print the results.

System.out.println(”


Collisions? " + results.size() + "
");
for (int i = 0; i < results.size(); i++) {
// For each hit, we know distance, impact point, name of geometry.
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) {
mark.setLocalTranslation(results.getClosestCollision().getContactPoint());
rootNode.attachChild(mark);
}[/java]
(if i use //Ray ray = new Ray(cam.getLocation(), cam.getDirection()); it works ofcourse, but that's not what i want^^)
pretty much all from the HelloPicking tutorial, and some hints on how calc the mouse position from here. What am i doing wrong? :D

EDIT: found a solution after switching some google-search keywords around..funny how these work sometimes.
anyway, i found this, which appears to be working just fine. it would be really nice if that piece of code (or a link) would be added to the guide for collision and intersection :)

Thanks for that link! I had been using something I came up with myself that isn’t quite as pretty.

pre type="php"
Vector2f v2f = getInputManager().getCursorPosition();


Vector3f v1 = getCamera().getWorldCoordinates(v2f, 0),


v2 = getCamera().getLocation().add(getCamera().getDirection().normalize()


.mult(1000.0f)).add(getCamera().getUp().normalize().mult((v1.y-getCamera().getLocation().y)*1000.0f))


.add(getCamera().getLeft().mult(-1.0f).normalize().mult((v1.x-getCamera().getLocation().x)*1000.0f));


Ray ray = new Ray(v1, v2.subtract(v1));
/pre



(edit)

Oh, and by the way, yes mine does assume the camera is not rotated - up points in (0,1,0), left (-1,0,0)

wlfbck said:
EDIT: found a solution after switching some google-search keywords around..funny how these work sometimes.
anyway, i found this, which appears to be working just fine. it would be really nice if that piece of code (or a link) would be added to the guide for collision and intersection :)


You should always check all files in the jme3test package. Its the preferred way to get some source code examples for things discussed in the doc.