We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I have a set of points in 3D and want to use mouse to alter the position of those points. I have got the equivalent position of these points in 2D using screenX & screenY (mapping 3D into 2D), but have difficulty in mapping 2D back to 3D..
What I want is to use the mouse's position(mouseX mouseY) to determine the position of point's Z axis (height) to be visually in the same position as my mouse.
is there any function to counter-screenX & screenY...? (sort of sine - arcsine..if it can explain it less confusing..)
Let's say the position I would like to alter is
PVector pos = point.x, point.y, point.z
and I want the point.z be altered by mouseX and mouseY.. so it should be something like
if(mouseClicked)
{
point.z = counterScreen(mouseX, mouseY);.....
}
Answers
https://forum.processing.org/two/discussion/comment/87905/#Comment_87905
https://forum.processing.org/two/discussion/20260/is-mouse-over-sphere-in-3d#latest
****EDIT: Extra link Kf
@kfrajer Thanks! I found it difficult to decide z value in 3D, so I gave up and use keyboard to control z value but I used the website you referred to to get the x & y value, that's very helpful thanks a looooot!!
Y is height and Z is depth.
Image the space above a table.
Y is how far above the table, Z is back and forth
You can try
point.z = map(mouseY, 0, height, -200,200);
Or increase the z value when mouseY > pmouseY etc..
Imagine you are standing in a room looking through a window at a beautiful country scene and you see something of interest at a 3D position [x,y,z]. Now imagine a line that links [x,y,z] to your eye. It will pass through the window and if we note the position it intersects with the window we get a 2D position [sx,sy].
So far so good. Now lets try and reverse the process we draw a line from our eye to infinity through the window position [sx,sy] the 'something of interest' could be anywhere along that line. It means that it is impossible to generate a 3D position from the 2D screen position.
Unless you have a rule that provides that third dimension. For example:
Or you can create a depth map. For each point, index it under its screenX, screenY. Then look up a given screenX, screenY in your index / hashmap / whatever and return the point (if any) with the closest z (if more than one). But this is getting complex -- there may be simpler ways of accomplishing what you want depending on the scenario -- for example, if you know that points will never appear in front of each other.
That is: once you have selected a point, you could alter the depth in relative ways with the mouse, for example dragging. Since you know the absolute z in the point object, it doesn't matter that you can't input an absolute z with the mouse -- it can be relative.
Post your code please
Entire
The depth map idea is the easiest to implement, and serves the purpose well enough.
Superb explanation @quark.
in this sketch you can drag a sphere with mouse and change its x,y
when you hold a key during drag, ONLY Z changes with mouseY (up and down)