/* (non-Javadoc)
   * @see com.jme.input.action.MouseInputAction#performAction(float)
   */
  public void performAction(InputActionEvent evt) {
    shotTime += evt.getTime();
    if (MouseInput.get().isButtonDown(0) && shotTime > 0.1f) {
      shotTime = 0;
      Ray ray =
          new Ray(
              camera.getLocation(),
              camera.getDirection()); // camera direction is already normalized
      PickResults results = new BoundingPickResults();
      results.setCheckDistance(true);
      scene.findPick(ray, results);

      hits += results.getNumber();
      hitItems = "";
      if (results.getNumber() > 0) {
        for (int i = 0; i < results.getNumber(); i++) {
          hitItems +=
              results.getPickData(i).getTargetMesh().getName()
                  + " "
                  + results.getPickData(i).getDistance();
          if (i != results.getNumber() - 1) {
            hitItems += ", ";
          }
        }
      }
      shots++;
      results.clear();
      text.print("Hits: " + hits + " Shots: " + shots + " : " + hitItems);
    }
  }
示例#2
0
 /** {@inheritDoc} */
 public Spatial getIntersectObject(
     Ray ray, Node root, Class<? extends Spatial> reference, boolean iterate) {
   PickResults results = new TrianglePickResults();
   results.setCheckDistance(true);
   root.findPick(ray, results);
   if (iterate) {
     for (int i = 0; i < results.getNumber(); i++) {
       Spatial collision =
           this.validateClass(root, results.getPickData(i).getTargetMesh(), reference);
       if (collision != null) return collision;
     }
   } else if (results.getNumber() > 0) {
     return this.validateClass(root, results.getPickData(0).getTargetMesh(), reference);
   }
   return null;
 }