object 3d look at

Started by fireside, November 26, 2008, 05:31:45 AM

Previous topic - Next topic

fireside

#15
Thanks for the help.  I should have read the docs, but you know how that goes. ;D  Things like this would be nice to put in a wiki, if we had a wiki.  ;D
click here->Fireside 7 Games<-

fireside

I had this code working fine as a regular app like hello world, however, when I converted it to an applet nothing seems to work right.  The collision point seems to be off, not overly far, but off, and the lookAt function no longer works properly.
click here->Fireside 7 Games<-

EgonOlsen

The mathematics don't care about the target platform. This can only be a problem with the screen coordinates being off either in the applet or in the application IHMO.

fireside

I guess I'll try to build two simpler applications and try to figure out what's going on.  I'm thoroughly confused right now. ???
click here->Fireside 7 Games<-

paulscode

Wow, that really is strange.  I do know that for applications, you sometimes have to take into account the window titlebar and left border width (for example like in the paint() method), whereas for applets, the coordinates are exact (i.e. 0,0 is the top-left pixel of the applet).

However, that wouldn't explain the problem with your lookAt method, unless you were looking at something you got from another method which produced the wrong coordinate to look at.

I would test the lookAt method seperately.  Place two objects in the world, then have one of them "lookAt" the other one's getTransformedCenter().  See if it works the same in both an applet and in an application.  If there's no problem with that, then you know the problem is coming from somewhere in your program before lookAt() gets called.

paulscode

Oh, BTW, calculating collision points in an applet seems to be working fine for me.  For reference, here are a couple of methods I am using now that I know work in an applet:

    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 );

            // collisionPoint is the exact 3D coordinate.
            clickMap( collisionPoint );  // user clicked on the map
        }
    }


    public void move( long currentTime )
    {
        // if bullet expired or crashed, do nothing
        if( expired || crashed )
            return;

        // check if bullet should expire
        if( currentTime - creationTime >= lifeSpan )
        {
            expired = true;
            return;
        }

        // calculate how far to move the bullet
        float distance = ( currentTime - lastMoveTime ) * distancePerMilli;

        // save the current time
        lastMoveTime = currentTime;
       
        // check if the bullet should expire
        if( lastMoveTime - creationTime >= lifeSpan )
        {
            expired = true;
            return;
        }

        // check if the movement will result in a collision:
        SimpleVector position = object3D.getTransformedCenter();

        float targetDistance = world.calcMinDistance( position, direction,
                                                      100000 );

        // See if the bullet is going to collide with something:
        if( (targetDistance != Object3D.COLLISION_NONE)
            && (targetDistance <= distance) )
        {
            // collision occurred, move bullet to the collision point:
            SimpleVector smallTranslation = new SimpleVector( direction );
            smallTranslation.scalarMul( targetDistance );
            object3D.translate( smallTranslation );

            crash();

            return;
        }

        // move bullet the full distance:
        SimpleVector translation = new SimpleVector( direction );
        translation.scalarMul( distance );
        object3D.translate( translation );
    }

fireside

I did a simple lookAt test on a basic applet and it seems to be working fine.  I'll have to slowly build up from this one one step at a time I guess.  Thanks for the posting the code.
click here->Fireside 7 Games<-