I am working on a program where users can select a cube from many in a 3 dimensional screen, and then drag it somewhere else on the screen. I implemented 3D picking using this mechanism, and it does work correctly:
http://wiki.processing.org/w/Picking_with_a_color_buffer
The trouble is with dragging the objects. I calculate the new position as such (pseudo code):
int origX, origY; // the original mouse click location
boolean objectPicked; // was an object actually picked?
int x, y, z; // These represent the x,y,z of the object picked with the mouse
void mousePressed() {
shape = what_was_picked();
if (shape != background) {
origX = mouseX;
origY = mouseY;
objectPicked = true;
x = shape.getX();
y = shape.getY();
z = shape.getZ();
}
}
void mouseReleased() {
int x_diff = mouseX - origX;
int y_diff = mouseY - origY;
// now draw a new box at (x + x_diff, y + y_diff, z);
// as you notice, the user can only move the object by X and Y axis
}
The trouble is that this kind of works, as the new box will be in the correction direction relative to the starting point. But the new X and/or Y coordinate will be greater, or sometimes less than, where the mouse was clicked.
My guess as to why this happens is because of distortion caused by the Z-Axis, because X and Y changes are much more noticeable at close distances (where the mouse is).
Is this really the problem? If so, is there a good way to account for it? Should I change my approach to the whole problem?
As a heads up I already tried using screenX() and screenY(), but unless I used them improperly, they did not help.