Hi,
To determine where the player has clicked at, I used this code before, which works fine:
[java] public static CollisionResults mouseClick(Spatial s) {
Vector2f mouseCoords = inputManager.getCursorPosition();
Vector3f mouse3d = cam.getWorldCoordinates(new Vector2f(mouseCoords.x, mouseCoords.y), 0f).clone();
Vector3f dir = cam.getWorldCoordinates(new Vector2f(mouseCoords.x, mouseCoords.y), 1f).subtractLocal(mouse3d).normalizeLocal();
Ray ray = new Ray(mouse3d, dir);
CollisionResults results = new CollisionResults();
s.collideWith(ray, results);
return results;
}
[/java]
The game got a top-down perspektive similiar to a strategy game like age of empires, where the player can click on things.
I wanted to do the same thing with the physics raytest, now and ended up with this method, but it doesn’t work:
[java] public static List mouseClickPhysic(){
Vector2f mouseCoords = inputManager.getCursorPosition();
Vector3f mouse3d = cam.getWorldCoordinates(new Vector2f(mouseCoords.x, mouseCoords.y), 0f).clone();
Vector3f dir = cam.getWorldCoordinates(new Vector2f(mouseCoords.x, mouseCoords.y), 1f).subtractLocal(mouse3d).normalizeLocal();
List rayTest = physics.getPhysicsSpace().rayTest(mouse3d,dir);
return rayTest;
}[/java]
Almost all of my world objects got a rigidbodycontrol and an collider and everything related to physics works fine, but not the Raytest. I always get 0 results when I click in the world, but when I move to the bottom of the world and go outside of it(which is possible because the game is unfinished) and click in the blue background, I suddenly get results with the ground, which isn’t there. All of the collision shapes are in their right places(I checked it with physics.getPhysicsSpace().enableDebug(assetManager)).
Where did I made a mistake? Thanks for your help :).