jPCT - a 3d engine for Java => Support => Topic started by: fireside on November 14, 2008, 11:10:48 pm

Title: shortest distance
Post by: fireside on November 14, 2008, 11:10:48 pm
On one of the threads there was mentioned a shortest distance function, however, I can't seem to find it in the docs.  I did a search, but it's not working or I'm not remembering correctly.
Title: Re: shortest distance
Post by: EgonOlsen on November 14, 2008, 11:21:23 pm
Shortest distance between what?
Title: Re: shortest distance
Post by: fireside on November 15, 2008, 12:40:22 am
From a point to a point, I think.  We were talking about mouse picking and polygons and how to get the exact position by using shortest distance.  Now that i think about it, though, I guess I wouldn't know the point.  I would only know the polygon and be able to find the position of the vertices.  Anyway, it was your idea so i was hoping you could explain it again.

Also, what about reproject2d3d?  Would that work to find the point of contact?
Title: Re: shortest distance
Post by: paulscode on November 15, 2008, 02:30:11 am
I am using the following code to interact with Object3Ds via the mouse.  Maybe this is what you need.  Requires the Object3D you want to be able to click on to use setCollisionMode( Object3D.COLLISION_CHECK_OTHERS )

Code: [Select]
    public void mousePressed( MouseEvent e )
        // Get the mouse's coordinates on the applet window:
        int x = e.getX();
        int y = e.getY();
        // Get the 3D coordinates in Camera space:
        SimpleVector position = new SimpleVector( Interact2D.reproject2D3D(
                                                       camera, buffer, x, y ) );
        // Convert the coordinates to World space:
        position.matMul( camera.getBack().invert3x3() );
        position.add( camera.getPosition() );
        // Determine the direction from the camera position to the click point:
        SimpleVector direction = position.calcSub( camera.getPosition() ).normalize();
        // Calculate the distance to whatever was clicked on:
        float distance = world.calcMinDistance( position, direction, 10000 );
        // Check if we clicked on something:
        if( distance != Object3D.COLLISION_NONE )
            // Calculate the exact 3D coordinates for the point that was clicked:
            SimpleVector collisionPoint = new SimpleVector( direction );
            collisionPoint.scalarMul( distance );
            collisionPoint.add( position );

            // TODO: do something.  collisionPoint is the exact 3D coordinate.

Title: Re: shortest distance
Post by: fireside on November 15, 2008, 03:07:27 am
Hey thanks.  I'll play around with it.  It's a good starting point anyway since I have no idea what I'm doing right now.