views:

50

answers:

1

My goal is to move a shape in the virtual world in such a way so that it ends up where the mouse pointer is on the canvas.

What i have: -mouse position (x,y) on a Canvas3D object -Point3d object of where a pick ray starting from the Canvas3D viewport intersects with the first scene object. (point in 3D space of where i want to start the drag)

What i want: -Some way to translate the Point3d's coordinates so that the initial point of intersection (the Point3d object) is always overlapping the the mouse position on the canvas (same as when i used the pick ray to determine what the user clicked on from the Canvas3D object).

Thanks!

+1  A: 

It sounds as if you want to use the plane which is parallel to the background plane and contains the intersection point with the object. You can use this plane even when the mouse moves beyond the actual background as it's just a mathematical concept which stretches to infinity.

I'm not a Java programmer so I can't give you code but I am a mathematician so here's equation you need ;)

Let P denote the original intersection point and call the background plane unit normal n. This normal is also the normal of our plane of interest. Let R denote a point on the ray and l denote it's unit direction vector.

Then the equation of the plane is (x-P).n = 0 for a point x in the plane (the . denotes dot product of two vectors). The equation of a point on the ray is x = R + t*l where t is any real number. The ray therefore intersects the plane when

(t*l + R - P).n = 0

i.e. when

t = (P - R).n / ( l.n )

This gives you a t value to plug back into your ray equation to give the intersection point.

Troubadour
Ok, but can you provide me some code given that i know the intersection point with the shape (Point3d intersectionWithShape) and the start and end x,y position on the canvas3D, call them Point start and Point end? Also I have the plane of the background, right now it's along the x-y plane and it's at z=0, but it could move in the future.
Coder
Do you have the means to calculate the 3D ray from your 2D co-ordinate?
Troubadour
If you mean the pick ray from the canvas then yes. That is how i obtained the intersection point with the shape in my scene.
Coder
Cool, thanks. I've added the relevant maths for you. It should easy to implement in Java I would think.
Troubadour
Thanks a lot, i will try to implement that in Java, though I can't right now. Also, I'm not sure if i will be able to given that the pick ray, intersection code is handled for me through the Java3D API, so if anyone knows how to use Java3D method to do this that would be great. In the mean time, as soon as i get a chance i will try to do it with the math you provided above, I hope it will work :). Thanks a lot!
Coder