Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

[java] Java3D unprojection

Started by Lord_Evil Dec 17, 2008 at 9:21 AM 1 replies 2.1k views
Original Post
Lord_Evil
Lord_Evil
Hi, I have another question on Java3D. For my camera class I want to provide a screenToWorld() method that takes the mouse position and a depth and returns the corresponding world coordinates. The problem is that I can't find any proper methods/functionality in Java3D to do this. There is a getInverseVworldProjection(...) method in Canvas3D but the returned matrix seems very odd. As an example, if I transform (0,0,1) with that matrix I get something like (0, 10000, 10000) with my camera being located at (0,20,20) looking at (0,0,0) and far plane distance = 100). If the camera moves the values for y and z seem to change exponentially. I found a workaround that does the job but I'd like to have a proper calculation. The workaround is as follows:

public Point3d screenToWorld(Point2d pPoint, double pDepth) {
        //get the only canvas
        Canvas3D tCanvas3D = view.getCanvas3D(0);
        
        //get eye position
        Point3d tEyePos = new Point3d();
        tCanvas3D.getCenterEyeInImagePlate(tEyePos);
        
        //get the screen coords of the point
        Point3d tMouseCoords = new Point3d();
        tCanvas3D.getPixelLocationInImagePlate(pPoint, tMouseCoords);

        //project eye and screen pos to world
        Transform3D tTransform = new Transform3D();
        tCanvas3D.getImagePlateToVworld(tTransform);
        tTransform.transform(tMouseCoords);
        tTransform.transform(tEyePos);

        //create a direction vector from eye to screen with the desired length (depth * distance of far plane)  
        Vector3d tDir = new Vector3d(tMouseCoords);
        tDir.sub(tEyePos);
        tDir.normalize();
        tDir.scale(pDepth * view.getBackClipDistance());
        
        //world pos is eye + direction vector
        Point3d tWorldCoords = new Point3d(tEyePos);
        tWorldCoords.add(tDir);

        return tWorldCoords;
    }


Does anyone know how to correctly and efficiently unproject screen coordinates in Java3D?
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Momoko_Fan
Momoko_Fan
Considering Java3D is pretty much dead (sun stopped supporting it) I think you'll have trouble getting answers for your question.. If you want you could take a look at newer and more supported engines like jMonkeyEngine, jPCT, or Xith3D.
Lord_Evil
Lord_Evil
Well, is it really dead? The lates version I downloaded was 1.5.2 which seems to be quite new (didn't find a release date though).

But thanks for the suggestions, I'll have a look at those.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.