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 :)