Author Topic: object 3d look at  (Read 11924 times)

Offline fireside

  • double
  • *****
  • Posts: 607
    • View Profile
Re: object 3d look at
« Reply #15 on: November 28, 2008, 04:44:27 pm »
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
« Last Edit: November 28, 2008, 06:28:47 pm by fireside »
click here->Fireside 7 Games<-

Offline fireside

  • double
  • *****
  • Posts: 607
    • View Profile
Re: object 3d look at
« Reply #16 on: December 03, 2008, 09:55:56 pm »
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<-

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: object 3d look at
« Reply #17 on: December 03, 2008, 10:42:31 pm »
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.

Offline fireside

  • double
  • *****
  • Posts: 607
    • View Profile
Re: object 3d look at
« Reply #18 on: December 03, 2008, 10:57:04 pm »
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<-

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: object 3d look at
« Reply #19 on: December 03, 2008, 11:12:43 pm »
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.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: object 3d look at
« Reply #20 on: December 03, 2008, 11:31:01 pm »
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:

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

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

Code: [Select]
    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 );
    }

Offline fireside

  • double
  • *****
  • Posts: 607
    • View Profile
Re: object 3d look at
« Reply #21 on: December 04, 2008, 01:56:34 am »
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<-