|
Author |
Topic: Translating (2D) mouse coordinates to 3D (Read 7672 times) |
|
pitaru
|
Translating (2D) mouse coordinates to 3D
« on: Feb 10th, 2003, 1:01am » |
|
The following example demonstrates a hack for translating (2D) mouse coordinates to a 3D environment. If you are familiar with this problem, than skip the problem description, and read 'The Solution'. Otherwise, let me explain the problem: ***************** The Problem ***************** Look at the following file [ http://www.pitaru.com/proce55ing/mouse_3d/problem/ ], which enables you to draw a 'wire' object in a 3D environment. You'll notice that moving the mouse creates the 'wire', and dragging the mouse (w/btn pressed) rotates the object in 3D. Play with this applet for a while, and it becomes apparent that while we can view the object in 3D, we can only draw on a 2D plain. This produces two problems: 1) After we rotate the object, the 'wire' is not drawn from the mouse curser, but appears in a different location altogether. 2) Our 'wire' object remain flat. It would be nice to have the mouse always 1:1 with the newly rotated space (problem 2). This will also enable us to draw in the new plain and sculpt a 3D object(problem 3). In other words, we would need to translate the 2D coordinate system of the mouse to the rotated 3D space. Sadly, the full answer involves inverting the matrices used to project things on-screen. These matrices are 'behind the scenes' helpers that enable us to use functions such as transform() , rotate() etc. The math is complex, and a pretty heavy-duty operation. If we only had a hack... ****************** The Solution ****************** Enter Ben Fry with the following thought: Instead of rotating a static object, why don't we morph the object to look 'as if' it was rotated. In other words, imagine an apple that magically morphs to give the illusion that it is being spun, but actually never moves. This idea could very much solves our problem: If we never actually rotate the space, than the mouse curser will always be in sync with the drawn 'wire'. The Implementation*: *This implementation is specific to the original system at hand [ http://www.pitaru.com/proce55ing/mouse_3d/problem/ ]. Before you continue, make sure to understand the functionality of the source code [ http://www.pitaru.com/proce55ing/mouse_3d/problem/recorder5_t4.java ] . 1. Draw points at z=0 by dragging the mouse around. (already happening in original code) 2. Rotate the object by dragging the mouse (already happening in original code) 3. As soon as the user starts trying to draw points again (on mouse release), transform all the points by the matrix, and then set angleX and angleY to zero. 4. back to #1 so for the step in #3, we'd have a loop that takes your already drawn xpoints and ypoints and uses something like the following: float x = xpoints[i]; float y = ypoints[i]; float z = zpoints[i]; // "transform" by the model matrix // m00,m01 etc. are automatically produced by the bApplet class. float ax = g.m00*x + g.m01*y + g.m02*z + g.m03; float ay = g.m10*x + g.m11*y + g.m12*z + g.m13; float az = g.m20*x + g.m21*y + g.m22*z + g.m23; xpoints[i] = ax; ypoints[i] = ay; zpoints[i] = az; *after* the loop, set angleX and angleY to zero, and the already drawn points should all stay put, and you can now draw on the z=0 plane. **************** The Result **************** http://www.pitaru.com/proce55ing/mouse_3d/solution/
|
|
|
|
Martin
|
Re: Translating (2D) mouse coordinates to 3D
« Reply #1 on: Feb 10th, 2003, 6:48am » |
|
this is sweet! i could imagine this applied to physical computing. thanks for this amit.
|
|
|
|
fry
|
Re: Translating (2D) mouse coordinates to 3D
« Reply #2 on: Mar 5th, 2003, 10:45pm » |
|
a simpler version of the following: Code:float ax = g.m00*x + g.m01*y + g.m02*z + g.m03; float ay = g.m10*x + g.m11*y + g.m12*z + g.m13; float az = g.m20*x + g.m21*y + g.m22*z + g.m23; |
| is now implemented in rev 0052. you can now use: Code:float ax = objectX(x, y, z); float ay = objectY(x, y, z); float az = objectZ(x, y, z); |
|
|
|
|
|
mflux
|
Re: Translating (2D) mouse coordinates to 3D
« Reply #4 on: Mar 5th, 2005, 12:02am » |
|
Now make a NURBs modeling program with this Very cool! Thanx Amit + Ben!
|
|
|
|
thesimplicity
|
Re: Translating (2D) mouse coordinates to 3D
« Reply #5 on: Mar 22nd, 2005, 1:56pm » |
|
on Mar 4th, 2005, 10:54am, fjen wrote:http://www.pitaru.com/processing/mouse_3d/solution/ |
| Weird... when I run the source code from that in p5 (6, it streaks the wire when I drag it. I can't seem to find why this is happening from in P5 and not in the browser version.
|
|
|
|
|