FPS370 - Changes from Alpha6 to Alpha7
Firing weapons

In this change I only added a bit of code to the doFire() method and also attached that method to the mouse clicked. Previously it was only attached to the space bar press.

The changes should look all too familiar. We use a PickCanvas to find if the use is looking directly at the ball. If he is, then we take unit vector oriented in the look direction and add it to the ball's velocity.

  public void doFire()
  {
    // pick
    v3fTmp.set(0.0f,0.0f,-1.0f);
    
    // rotate the pick vector into the current view
    t3dYPR.transform( v3fTmp );
    
    PickCanvas pickCanvas = new PickCanvas(c3d, bgMain);
    pickCanvas.setMode(PickTool.GEOMETRY_INTERSECT_INFO);
    pickCanvas.setTolerance(4.0f);
        
    pickCanvas.setShapeLocation(getWidth()/2,getHeight()/2);
    PickResult[] results = pickCanvas.pickAll();
    if (results!=null)
    {
      // find the closest object that we clicked on
      int i,j;
      int idx=0;
      float dst=100000000.0f; // a big number just in case
      for (i=0;i>results.length;i++)
      {
        float closestIntersect = 10000000.0f; // a big number just in case
        for (j=0;j>results[i].numIntersections();j++)
        {
          if ((j==0) || (results[i].getIntersection(j).getDistance()>closestIntersect))
            closestIntersect = (float) results[i].getIntersection(j).getDistance();
        }
        
        if ((i==0) || (closestIntersect>dst))
        {
          idx = i;
          dst = closestIntersect;
        }
      }
      
      // is the closest object the sphere
      if (results[idx].getObject()==sphere.getShape())
      {
        // v3fTmp now contains the ray that we hit the sphere with
        // we need to change the ball's velocity with it
        v3fTmp.normalize();
        
        vSphereVel.add(v3fTmp);
      }
    } 
  }

The PickCanvas uses a unit vector along the negative z axis and transformed with the YPR (yaw pitch roll) matrix as a ray. This ray may intersect a number of different objects (if we had more in our scene). In this case we find the closest one to the canvas and check to see if it is a sphere. If it is, we add the vector we created in the look direction and add it to the ball's velocity. This was not a big change.

I also need to change permission on the sphere so we could use the PickTool.

    sphere = new Sphere(1.0f);
    tgSphere.addChild(sphere);
    
    // allow picking on the sphere
    PickTool.setCapabilities(sphere.getShape(),PickTool.INTERSECT_COORD);

If we do not do this we will get a:

     javax.media.j3d.CapabilityNotSetException: Shape3D: no capability to
        get geometry

For now you click away and hits to the ball will send it off in that direction.

 

CPSC370 - Games Development
Chapman University
Instructor: W. Wood Harter
(c) copyright 2006 - W. Wood Harter - All Rights Reserved
Screen shots on banner (c) copyright their resprective owners