3D Mouse Picking (Trigonometry) Problem
I'm trying to use the mouse to select a point on a plane that has been rotated along the x-axis relative to the camera. I've figured out how to find the vertical position of the mouse (on the plane) and I tried to use that to find it's horizontal position but there are some errors:
In the link above, if you move to the mouse up and down in the center of the window, you'll see that the mouse stays centered in the cursor (the red box). But, if you move it up and down along either side of the window, you'll see that the cursor seems to move in an arc rather than a straight line beneath the mouse. The vertical position remains accurate, but the horizontal position gets screwy.
I believe the most relevant code can be found in map3d.pde within the function calcMouseAngles(). The variable cursorPosition should be the 3D coordinates of the mouse on the plane. cursorPosition.x is it's horizontal position on that plane.
I've seen other mouse picking algorithms that involves painting different shapes in different colors, and that seems unreasonable for selecting an arbitrary point on a visible surface. It seems like I'm close to a solution so I would prefer to stick to this approach. Any help would be appreciated.
- void calcMouseAngles() {
- mouseAngle.x = atan((mouseX - width / 2)/DISTANCE_OF_EYE_FROM_SCREEN); // * FOVx / width - FOVx/2;
- mouseAngle.y = atan((mouseY - height / 2)/DISTANCE_OF_EYE_FROM_SCREEN);//mouseY * FOVy / height - FOVy/2;
- ///println("mouseAngle.x: " + degrees(mouseAngle.x) + " DISTANCE_OF_EYE_FROM_SCREEN: " + DISTANCE_OF_EYE_FROM_SCREEN);
- cursorPosition.y = cameraPosition.y + tan(mouseAngle.y+cameraAngle.x) * cameraPosition.z; // (MAP_DISTANCE * 1) - ATOM_SIZE*CELL_SIZE/2;
- float distance_to_cursor = dist(cameraPosition.x, cameraPosition.y, cameraPosition.z, targetPosition.x, cursorPosition.y, targetPosition.z);
- float magic_constant = .88; //Found this through trial and error, I have no idea why it shouldn't be 1
- cursorPosition.x = targetPosition.x + tan(mouseAngle.x) * (distance_to_cursor*magic_constant);
- cursorPosition.z = targetPosition.z;//-MAP_DISTANCE;
- println("mouseAngle.x: "+degrees(mouseAngle.x)+" distance_to_cursor.y: " + distance_to_cursor);
- }