Mapping mouse location to universe location

I know that pickObject has a pickAll(x,y) behavior that shoots a ray from your screen’s plane into a 3d universe and returns a scene branch graph. I was wondering if there was anyway to do the opposite:
For instance, I want to click anywhere on the canvas3d and a new object will be created/drawn where i just clicked. MouseEvent can easily be used to give me the X and Y position of the place I just clicked in terms of screen coordinates, but how can I convert that into the XYZ coordinates of the universe so I can spawn an object at the place I clicked?

Thanks again for the help. I’m just getting the hang of Java3d, and it has been fun so far. I try to figure out problems on my own, but I figure it doesn’t hurt to ask the messageboard too.

Thanks!
DAT

com.sun.j3d.utils.picking.PickCanvas probably has what you want (not 100% sure about the package name - I don’t have the javadocs in front of me…).

It will automatically convert a 2D pick location into a 3D location based on your current view transform.

I’ve been looking at PickCanvas and awt’s MouseEvent, and I can’t seem to figure out a fast and easy way to convert a 2D mouse click location into a 3D location without having to write a behavior for it. PickCanvas takes in a MouseEvent. How do I actually extract that MouseEvent, for instance, if I want to get “the next click”'s positions? Can I do this without having to write a behavior?

I know this is an old post, but the basic question is that I am curious as to how to easily implement a way for me to click somewhere on a 3D Canvas and just simply System.out.println the x/y/z locations of the place I just clicked, even if the pickResult is null. The 3D coordinates are all I want.

i did that, but can’t post the sources from the office. will do that tonight, when i’ll be at home.
Anyway, it was quite easy. just did a pickClosest() and retrieved the coords of the ‘collision’ with the surface of the shape i was pointing with the mouse.

But the thing is, I am not clicking on any shape. If I have a scenegraph with no shapes or geometries, I just want to be able to click somewhere on the scenegraph and have it tell me the world’s coordinates of where I clicked. Or perhaps I misunderstood you…?

My goal is to be able to click somewhere on the 3D canvas and be able to create, for instance, a sphere where I clicked. That is why I am interested in getting the 3d world coordinates. It seems like there should be a very easy way to do this, but I can’t seem to figure it out. :confused:

mhhh. if you want the projection of the click in the world, you’d better have something to project it on. ;D
If you click on nothing, what coordinates do you expect to get? The question you might ask yourself is : what is the plane the coordinates of my click need to intersect ?
Your ray is infinite. to get a coordinate, it MUST intersect something… An imaginary plane or object does not matter., but something has to be there to cross it.

Yeah, is there any way for me to spawn an object “somewhere” along the projection?

I’m sure this has been done reasonably before. That’s what editor do right? You click on an editor’s 3d canvas and plop a shape down where you clicked.

The source for the com.sun.j3d.utils package(s) is available in a jar file that comes with the Java3D download – look in your Java root directory.

If you look at the source for the PickCanvas you can see how they are converting the 2D canvas coordinates into the 3D view-local coordinates.

That should give you the 3D plane representing your current view, with the relative Z-coordinate being zero. Then you would just pick an arbitrary z-coordinate, convert back into world coordinates, and you should have a 3D point in space to place your object.

As Brad says, you don’t have to have an object to intersect with. my ‘imaginary plane’ was there to say that.
You want to add an object, but you can’t tell where you want to add it. Obviously, if you are to add an object at a certain distance of the screen, you have to decide of the coordinate of the imaginary plane that will receive it.
You even don’t have to use a PickRay. just get the mouse coordinate in the canvas3D when the user clicks, get the camera rotation, deduct a ray, and get a point of that ray at the distance you decided to be the one.