Skip to main content

How to get x y z coordinates over a canvas3d

1 reply [Last post]
mitrajit
Offline
Joined: 2011-12-12

Hi,

I have simple program, where user can draw some predefined 3D shapes just by entering the values and clicked on it. And also user can draw 3d shapes just by choose positions on the canvas by click on it and then click on the draw button. But I can't get the x y z positions over the canvas3d.
It is possible to get these coordinates by clicking on the shapes, but I want the coordinates of all over the canvas, not only of the shapes.

So, how can achieve this?

Please help me. Thank you.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bhupendra_aole
Offline
Joined: 2006-01-31

I believe you mean that you want to find a point on an imaginary plane that correspond to the pixel point.
The following works for me ... it returns the "world" point on a XZ plane:

	public void mouseClicked( MouseEvent me ) {
// eye position
Point3d eyePosn = new Point3d();
canvas.getCenterEyeInImagePlate( eyePosn );
// mouse position
int x = me.getX();
int y = me.getY();
Point3d mousePosn = new Point3d();
canvas.getPixelLocationInImagePlate( x, y, mousePosn );
// transform to world coords
Transform3D motion = new Transform3D();
canvas.getImagePlateToVworld( motion );
motion.transform( eyePosn );
motion.transform( mousePosn );
// eye to mouse position vector
Vector3d mouseVec = new Vector3d();
mouseVec.sub( mousePosn, eyePosn );
mouseVec.normalize();

// imaginary plane
Point3d coords[] = { new Point3d( -10000, 0, -10000 ),
new Point3d( 10000, 0, -10000 ),
new Point3d( 10000, 0, 10000 ),
new Point3d( -10000, 0, 10000 ), };
// ray
PickRay ray = new PickRay( eyePosn, mouseVec );
double dist[] = new double[1];
// ray plane intersection
System.out.println( Intersect.rayAndQuad( ray, coords, 0, dist ) );
System.out.println( dist[0] );
mousePosn.scaleAdd( dist[0], mouseVec, eyePosn );
System.out.println( mousePosn );
}

Intersect.rayAndQuad( ray, coords, 0, dist )
The above is deprecated, but you can easily find ray x plane intersection on the web.

thanks,
Bhupen