private void moveObject(Vector3f delta) {
   //            Vector3f delta = new Vector3f(0, 0, -0.2f);
   Quaternion rotation =
       ClientContextJME.getViewManager().getCameraTransform().getRotation(null);
   delta = rotation.mult(delta);
   applyDelta(delta, new Quaternion());
 }
Exemple #2
0
  protected void simpleInitGame() {
    if (modelToLoad == null) {
      modelToLoad =
          TestMaxJmeWrite.class.getClassLoader().getResource("jmetest/data/model/char.3ds");
    }
    try {
      MaxToJme C1 = new MaxToJme();
      ByteArrayOutputStream BO = new ByteArrayOutputStream();
      C1.convert(new BufferedInputStream(modelToLoad.openStream()), BO);
      Node r1 =
          (Node) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
      // Node r = new Node("parent stuff");
      // r.attachChild(C1.get(new BufferedInputStream(modelToLoad.openStream()), BO));
      // r.setLocalScale(.1f);
      r1.setLocalScale(.1f);
      if (r1.getChild(0).getControllers().size() != 0) r1.getChild(0).getController(0).setSpeed(20);

      Quaternion temp = new Quaternion();
      temp.fromAngleAxis(FastMath.PI / 2, new Vector3f(-1, 0, 0));
      rootNode.setLocalRotation(temp);
      r1.setLocalTranslation(new Vector3f(10, 0, 0));
      // rootNode.attachChild(r);
      rootNode.attachChild(r1);
    } catch (IOException e) {
      logger.log(Level.SEVERE, "Failed to load Max file", e);
    }
  }
  @Override
  public void apply(float dt, Particle particle, int index) {

    if (particle.getStatus() == Particle.Status.Alive
        && floor.pseudoDistance(particle.getPosition()) <= 0) {

      float t =
          (floor.getNormal().dot(particle.getPosition()) - floor.getConstant())
              / floor.getNormal().dot(particle.getVelocity());
      Vector3f s = particle.getPosition().subtract(particle.getVelocity().mult(t));

      this.normal.normalizeLocal();
      Vector3f v1 = this.normal.cross(s.subtract(pos));
      Vector3f v2 = this.normal.cross(v1);
      v1.normalizeLocal();
      v2.normalizeLocal();

      Vector3f newVel = new Vector3f(particle.getVelocity());
      newVel.y *= -bouncyness;
      Quaternion q = new Quaternion();
      q.fromAxes(v1, this.normal, v2);
      newVel = q.mult(newVel);

      particle.setVelocity(newVel);
    } // if
  }
 @Override
 public void compute(ProcessorArmingCollection collection) {
   degrees += increment;
   quaternion.fromAngles(0.0f, degrees, 0.0f);
   quaternion.mult(up, upOut);
   quaternion.mult(position, positionOut);
 }
  /**
   * Computes the new transform for this interpolator for a given alpha value.
   *
   * @param alphaValue alpha value between 0.0 and 1.0
   * @param transform object that receives the computed transform for the specified alpha value
   * @since Java 3D 1.3
   */
  public void computeTransform(float alphaValue, Matrix4f transform) {
    transform.loadIdentity();

    // compute the current value of u from alpha and the
    // determine lower and upper knot points
    computePathInterpolation(alphaValue);

    // Determine the segment within which we will be interpolating
    currentSegmentIndex = this.lowerKnot - 1;

    // if we are at the start of the curve
    if (currentSegmentIndex == 0 && currentU == 0f) {

      iQuat.set(keyFrames[1].quat);
      iPos.set(keyFrames[1].position);
      iScale.set(keyFrames[1].scale);

      // if we are at the end of the curve
    } else if (currentSegmentIndex == (numSegments - 1) && currentU == 1.0) {

      iQuat.set(keyFrames[upperKnot].quat);
      iPos.set(keyFrames[upperKnot].position);
      iScale.set(keyFrames[upperKnot].scale);

      // if we are somewhere in between the curve
    } else {

      // Get a reference to the current spline segment i.e. the
      // one bounded by lowerKnot and upperKnot
      currentSegment = cubicSplineCurve.getSegment(currentSegmentIndex);

      // interpolate quaternions
      currentSegment.getInterpolatedQuaternion(currentU, iQuat);

      // interpolate position
      currentSegment.getInterpolatedPositionVector(currentU, iPos);

      // interpolate position
      currentSegment.getInterpolatedScale(currentU, iScale);
    }

    // Alway normalize the quaternion
    iQuat.normalize();
    iQuat.toRotationMatrix(tMat);

    // Set the translation components.
    tMat.m03 = iPos.x;
    tMat.m13 = iPos.y;
    tMat.m23 = iPos.z;
    rotation.set(tMat);

    // construct a Transform3D from:  axis * rotation * axisInverse
    //	transform.multLocal(axis);
    transform.multLocal(rotation);
    transform.scale(new Vector3f(iScale));
    //	transform.multLocal(axisInverse);

  }
  protected void handleRotate(Vector3f start, Vector3f end, float direction) {
    LOGGER.warning(
        "Handling rotate, start: "
            + start
            + ""
            + "\nlast: "
            + lastTranslation
            + ""
            + "\nend: "
            + end);
    Vector3f increment = end.subtract(lastTranslation);
    increment.y = 0;
    float length = increment.length();
    float magic = 3f; // ~ 180 degrees
    float percent = length / magic;

    // if the difference is negative, we need to rotate in the opposite
    // direction. Caclulate the difference after taking into account
    // the direction of the camera
    //        CellTransform viewTransform = ViewManager.getViewManager().getCameraTransform();
    //        Quaternion viewRotation = viewTransform.getRotation(null);
    //        increment = viewRotation.mult(increment);
    //
    //        LOGGER.warning("viewTransform: " + viewTransform + ", increment: " +
    //                       increment);
    //
    //        if (increment.x < 0 || increment.z < 0) {
    //            percent = -percent;
    //        }

    // take direction into account
    percent *= direction;

    Quaternion rotation = new Quaternion();
    rotation.fromAngles(0f, percent * FastMath.PI, 0f);

    // rotation is the total rotation since we started moving the mouse,
    // so calculate just the delta to apply
    Quaternion deltaRotation = rotation.mult(lastRotation.inverse());

    LOGGER.warning(
        "Percent: "
            + percent
            + " = "
            + toAnglesString(rotation)
            + ". Last = "
            + toAnglesString(lastRotation)
            + ". Delta = "
            + toAnglesString(deltaRotation));

    applyDelta(Vector3f.ZERO, deltaRotation);
  }
  /**
   * Interpolates unspecified rot values for objectIndex from start to end.
   *
   * @param objectIndex Index to interpolate.
   * @param startRotIndex Starting rot index.
   * @param endRotIndex Ending rot index.
   */
  private void fillQuats(int objectIndex, int startRotIndex, int endRotIndex) {
    keyframes.get(startRotIndex).look[objectIndex].getRotation(unSyncbeginRot);
    keyframes.get(endRotIndex).look[objectIndex].getRotation(unSyncendRot);
    float startTime = keyframes.get(startRotIndex).time;
    float endTime = keyframes.get(endRotIndex).time;
    float delta = endTime - startTime;
    Quaternion tempQuat = new Quaternion();

    for (int i = startRotIndex + 1; i < endRotIndex; i++) {
      float thisTime = keyframes.get(i).time;
      tempQuat.slerp(unSyncbeginRot, unSyncendRot, (thisTime - startTime) / delta);
      keyframes.get(i).look[objectIndex].setRotationQuaternion(tempQuat);
    }
  }
  public static void deepRotate(Spatial s, int x, int y, int z) {
    Quaternion q = s.getLocalRotation();
    //  only transform objects with non-zero rotation
    if (q.x != 0 || q.y != 0 || q.z != 0 || q.w != 1) {
      s.setLocalRotation(q.addLocal(Rotator.fromThreeAngles(x, y, z)));
    }

    if (s instanceof Node) {
      if (((Node) s).getChildren() != null) {
        for (Spatial child : ((Node) s).getChildren()) {
          deepRotate(child, x, y, z);
        }
      }
    }
  }
Exemple #9
0
  public SetNode(int id, String name, Vec2 pos, float rotZ) {
    ID = id;

    modelName = name;

    rotation = new Quaternion();
    rotation.fromAngleAxis(rotZ, new Vector3f(0, 0, 1));

    position = pos;
  }
  protected void simpleUpdate() {
    if (timer.getTimePerFrame() < 1) {
      angle = angle + (timer.getTimePerFrame() * 1);
      if (angle > 360) {
        angle = 0;
      }
    }

    rotQuat.fromAngleNormalAxis(angle, axis);
    t.setLocalRotation(rotQuat);
  }
 private static String toAnglesString(Quaternion q) {
   float[] angles = q.toAngles(null);
   return "(X: "
       + (FastMath.RAD_TO_DEG * angles[0])
       + ", "
       + "Y: "
       + (FastMath.RAD_TO_DEG * angles[1])
       + ", "
       + "Z: "
       + (FastMath.RAD_TO_DEG * angles[2])
       + ")";
 }
Exemple #12
0
  protected void simpleUpdate() {

    if (timer.getTimePerFrame() < 1) {
      angle = angle + (timer.getTimePerFrame() * 25);
      if (angle > 360) {
        angle = 0;
      }
    }

    rotQuat.fromAngleAxis(angle * FastMath.DEG_TO_RAD, axis);
    s.setLocalRotation(rotQuat);
  }
  protected void applyDelta(Vector3f deltaTranslation, Quaternion deltaRotation) {
    LOGGER.warning("Applying delta: " + deltaTranslation + " " + deltaRotation);

    boolean startedDrag = false;
    if (!dragging) {
      // no drag in progress. Start one now and end it after the drag
      // operation
      startDrag();
      startedDrag = true;
    }

    for (Cell cell : selected.getSelectedCells()) {
      CellTransform transform = cell.getLocalTransform();
      Vector3f translate = transform.getTranslation(null);
      Quaternion rotation = transform.getRotation(null);

      // if the cell has a parent, make sure to take the parent's
      // rotation and scale into account when applying the delta
      Vector3f localDeltaTranslation = deltaTranslation.clone();
      Cell parent = cell.getParent();
      if (parent != null) {
        CellTransform parentWorld = parent.getWorldTransform();
        Quaternion parentRotation = parentWorld.getRotation(null);
        float parentScale = parentWorld.getScaling();

        LOGGER.warning("Parent transform: " + parentWorld);

        // invert the rotation to get the child rotation
        parentRotation.inverseLocal();
        localDeltaTranslation = parentRotation.mult(deltaTranslation);
        localDeltaTranslation.multLocal(parentScale);

        LOGGER.warning("Local delta translation: " + localDeltaTranslation);
      }

      translate.addLocal(localDeltaTranslation);
      rotation.multLocal(deltaRotation);
      transform.setTranslation(translate);
      transform.setRotation(rotation);

      MovableComponent mc = getMovable(cell);
      if (mc != null) {
        mc.localMoveRequest(transform);
      }
    }
    lastTranslation.addLocal(deltaTranslation);
    lastRotation.multLocal(deltaRotation);
    // if we started a drag, remember to end it
    if (startedDrag) {
      endDrag();
    }
  }
  public ModulatorThreeNodes(
      ModulatorRenderer Renderer, String node1Name, String node2Name, String node3Name) {
    renderer = Renderer;
    rotFinal.fromAngles(0.0f, 0.0f, 0.0f);

    node1theSpatial = renderer.getNode(node1Name);

    System.out.println("node1theSpatial = " + node1theSpatial);
    al = node1theSpatial.getControllers();

    Tco = node1theSpatial.getController(0);
    //        float max = Tco.getMaxTime();
    //        float min = Tco.getMinTime();

    Tac = (AnimationController) Tco;
    Tani = Tac.getAnimation(0);
    Tkft = Tani.getKeyFrameTimes();
    if (traceLoad) System.out.println("node1Tkft size = " + Tkft.length);

    // Make storage for the transforms at each keyframe

    transX = new float[Tkft.length];
    transY = new float[Tkft.length];
    transZ = new float[Tkft.length];
    angleX = new float[Tkft.length];
    angleY = new float[Tkft.length];
    angleZ = new float[Tkft.length];
    // Make storage for the Quats that will hold the rotates at each keyframe
    node1keyFrames = new Quaternion[Tkft.length];
    // and the vectors that will hold the translates
    node1keyTrans = new Vector3f[Tkft.length];

    // There wil be multiple animations for the same node. Each animation will express one (?) of
    // the
    // directions of rotation or one of the directions of translation, or combinations. All the
    // rotations
    // for a key frame combined together will be the full rotation. All the translations for a key
    // frame
    // summed together will be the total translation for the key frame. Scaling should have the same
    // structure
    // when I get it added.
    // The result of all this will be a series of Quaternions, one for each key frame start and one
    // to finish
    // that contain the rotations for each keyframe. Also, there will be a set of vectors that
    // contain
    // the translations.

    // Step through the animation controllers to pick up the changes at each key frame
    for (int k = 0; k < al.size(); k++) {
      if (traceLoad) System.out.println("Controller = " + al.get(k).toString());

      // Get the controller and then cast it to an animation controller
      Controller co = node1theSpatial.getController(k);
      AnimationController ac = (AnimationController) co;
      // Get the animations for this controller
      ArrayList bans = ac.getAnimations();
      // Step through the animations
      for (int i = 0; i < bans.size(); i++) {
        // Get this animation
        BoneAnimation ani = ac.getAnimation(i);
        // Get the frame times for this animation
        float kft[] = ani.getKeyFrameTimes();
        if (traceLoad) {
          System.out.println("kft size = " + kft.length);

          for (int j = 0; j < kft.length; j++) {
            System.out.println("kft - " + kft[j]);
          }
        }
        ArrayList<BoneTransform> bt = ani.getBoneTransforms();
        if (traceLoad) {
          System.out.println("bt size = " + bt.size());
          for (int j = 0; j < bt.size(); j++) {
            System.out.println("bt = " + bt.get(j).toString());
          }
        }
        Quaternion qt[] = bt.get(0).getRotations();
        if (traceLoad) System.out.println("bt - rotations = " + qt.length);

        for (int j = 0; j < qt.length; j++) {
          if (traceLoad) System.out.println("rots = " + qt[j]);
          float[] fromAngles = qt[j].toAngles(null);
          if (traceLoad)
            System.out.println(
                "Angles - " + fromAngles[0] + " - " + fromAngles[1] + " - " + fromAngles[2]);

          angleX[j] = angleX[j] + fromAngles[0];
          angleY[j] = angleY[j] + fromAngles[1];
          angleZ[j] = angleZ[j] + fromAngles[2];
        }
        if (traceLoad)
          System.out.println(
              " start frame = " + ani.getStartFrame() + " - end frame = " + ani.getEndFrame());
        Vector3f v3f[] = bt.get(0).getTranslations();
        if (traceLoad) System.out.println("bt - translations = " + v3f.length);

        for (int j = 0; j < v3f.length; j++) {
          if (traceLoad) System.out.println("trans = " + v3f[j]);
          transX[j] = transX[j] + v3f[j].x;
          transY[j] = transY[j] + v3f[j].y;
          transZ[j] = transZ[j] + v3f[j].z;
        }
      }
    }
    // Build the final key frame Quats from the extracted angles and the Vectors from the extracted
    // translates
    for (int j = 0; j < Tkft.length; j++) {
      Quaternion temp = null;
      temp = new Quaternion();
      temp.fromAngles(angleX[j], angleY[j], angleZ[j]);
      if (traceLoad)
        System.out.println(
            " ********************** final angles "
                + angleX[j]
                + "--"
                + angleY[j]
                + "--"
                + angleZ[j]);
      node1keyFrames[j] = temp;
      node1keyTrans[j] = new Vector3f(transX[j], transY[j], transZ[j]);
      if (traceLoad)
        System.out.println(
            " *************** final translations "
                + transX[j]
                + " - "
                + transY[j]
                + " - "
                + transZ[j]);
    }

    // *************************************************************************************************
    // *************************************************************************************************

    node2theSpatial = renderer.getNode(node2Name);

    System.out.println("theSpatial = " + node2theSpatial);
    al = node2theSpatial.getControllers();

    Tco = node2theSpatial.getController(0);
    //        float max = Tco.getMaxTime();
    //        float min = Tco.getMinTime();

    Tac = (AnimationController) Tco;
    Tani = Tac.getAnimation(0);
    Tkft = Tani.getKeyFrameTimes();
    if (traceLoad) System.out.println("node2Tkft size = " + Tkft.length);

    // Make storage for the transforms at each keyframe

    transX = new float[Tkft.length];
    transY = new float[Tkft.length];
    transZ = new float[Tkft.length];
    angleX = new float[Tkft.length];
    angleY = new float[Tkft.length];
    angleZ = new float[Tkft.length];
    // Make storage for the Quats that will hold the rotates at each keyframe
    node2keyFrames = new Quaternion[Tkft.length];
    // and the vectors that will hold the translates
    node2keyTrans = new Vector3f[Tkft.length];

    // There wil be multiple animations for the same node. Each animation will express one (?) of
    // the
    // directions of rotation or one of the directions of translation, or combinations. All the
    // rotations
    // for a key frame combined together will be the full rotation. All the translations for a key
    // frame
    // summed together will be the total translation for the key frame. Scaling should have the same
    // structure
    // when I get it added.
    // The result of all this will be a series of Quaternions, one for each key frame start and one
    // to finish
    // that contain the rotations for each keyframe. Also, there will be a set of vectors that
    // contain
    // the translations.

    // Step through the animation controllers to pick up the changes at each key frame
    for (int k = 0; k < al.size(); k++) {
      if (traceLoad) System.out.println("Controller = " + al.get(k).toString());

      // Get the controller and then cast it to an animation controller
      Controller co = node2theSpatial.getController(k);
      AnimationController ac = (AnimationController) co;
      // Get the animations for this controller
      ArrayList bans = ac.getAnimations();
      // Step through the animations
      for (int i = 0; i < bans.size(); i++) {
        // Get this animation
        BoneAnimation ani = ac.getAnimation(i);
        // Get the frame times for this animation
        float kft[] = ani.getKeyFrameTimes();
        if (traceLoad) {
          System.out.println("kft size = " + kft.length);

          for (int j = 0; j < kft.length; j++) {
            System.out.println("kft - " + kft[j]);
          }
        }
        ArrayList<BoneTransform> bt = ani.getBoneTransforms();
        if (traceLoad) {
          System.out.println("bt size = " + bt.size());
          for (int j = 0; j < bt.size(); j++) {
            System.out.println("bt = " + bt.get(j).toString());
          }
        }
        Quaternion qt[] = bt.get(0).getRotations();
        if (traceLoad) System.out.println("bt - rotations = " + qt.length);

        for (int j = 0; j < qt.length; j++) {
          if (traceLoad) System.out.println("rots = " + qt[j]);
          float[] fromAngles = qt[j].toAngles(null);
          if (traceLoad)
            System.out.println(
                "Angles - " + fromAngles[0] + " - " + fromAngles[1] + " - " + fromAngles[2]);

          angleX[j] = angleX[j] + fromAngles[0];
          angleY[j] = angleY[j] + fromAngles[1];
          angleZ[j] = angleZ[j] + fromAngles[2];
        }
        if (traceLoad)
          System.out.println(
              " start frame = " + ani.getStartFrame() + " - end frame = " + ani.getEndFrame());
        Vector3f v3f[] = bt.get(0).getTranslations();
        if (traceLoad) System.out.println("bt - translations = " + v3f.length);

        for (int j = 0; j < v3f.length; j++) {
          if (traceLoad) System.out.println("trans = " + v3f[j]);
          transX[j] = transX[j] + v3f[j].x;
          transY[j] = transY[j] + v3f[j].y;
          transZ[j] = transZ[j] + v3f[j].z;
        }
      }
    }
    // Build the final key frame Quats from the extracted angles and the Vectors from the extracted
    // translates
    for (int j = 0; j < Tkft.length; j++) {
      Quaternion temp = null;
      temp = new Quaternion();
      temp.fromAngles(angleX[j], angleY[j], angleZ[j]);
      if (traceLoad)
        System.out.println(
            " ********************** final angles "
                + angleX[j]
                + "--"
                + angleY[j]
                + "--"
                + angleZ[j]);
      node2keyFrames[j] = temp;
      node2keyTrans[j] = new Vector3f(transX[j], transY[j], transZ[j]);
      if (traceLoad)
        System.out.println(
            " *************** final translations "
                + transX[j]
                + " - "
                + transY[j]
                + " - "
                + transZ[j]);
    }

    // *************************************************************************************************
    // *************************************************************************************************

    node3theSpatial = renderer.getNode(node3Name);

    System.out.println("node3theSpatial = " + node3theSpatial);
    al = node3theSpatial.getControllers();

    Tco = node3theSpatial.getController(0);
    //        float max = Tco.getMaxTime();
    //        float min = Tco.getMinTime();

    Tac = (AnimationController) Tco;
    Tani = Tac.getAnimation(0);
    Tkft = Tani.getKeyFrameTimes();
    if (traceLoad) System.out.println("node3Tkft size = " + Tkft.length);

    // Make storage for the transforms at each keyframe

    transX = new float[Tkft.length];
    transY = new float[Tkft.length];
    transZ = new float[Tkft.length];
    angleX = new float[Tkft.length];
    angleY = new float[Tkft.length];
    angleZ = new float[Tkft.length];
    // Make storage for the Quats that will hold the rotates at each keyframe
    node3keyFrames = new Quaternion[Tkft.length];
    // and the vectors that will hold the translates
    node3keyTrans = new Vector3f[Tkft.length];

    // There wil be multiple animations for the same node. Each animation will express one (?) of
    // the
    // directions of rotation or one of the directions of translation, or combinations. All the
    // rotations
    // for a key frame combined together will be the full rotation. All the translations for a key
    // frame
    // summed together will be the total translation for the key frame. Scaling should have the same
    // structure
    // when I get it added.
    // The result of all this will be a series of Quaternions, one for each key frame start and one
    // to finish
    // that contain the rotations for each keyframe. Also, there will be a set of vectors that
    // contain
    // the translations.

    // Step through the animation controllers to pick up the changes at each key frame
    for (int k = 0; k < al.size(); k++) {
      if (traceLoad) System.out.println("Controller = " + al.get(k).toString());

      // Get the controller and then cast it to an animation controller
      Controller co = node3theSpatial.getController(k);
      AnimationController ac = (AnimationController) co;
      // Get the animations for this controller
      ArrayList bans = ac.getAnimations();
      // Step through the animations
      for (int i = 0; i < bans.size(); i++) {
        // Get this animation
        BoneAnimation ani = ac.getAnimation(i);
        // Get the frame times for this animation
        float kft[] = ani.getKeyFrameTimes();
        if (traceLoad) {
          System.out.println("kft size = " + kft.length);

          for (int j = 0; j < kft.length; j++) {
            System.out.println("kft - " + kft[j]);
          }
        }
        ArrayList<BoneTransform> bt = ani.getBoneTransforms();
        if (traceLoad) {
          System.out.println("bt size = " + bt.size());
          for (int j = 0; j < bt.size(); j++) {
            System.out.println("bt = " + bt.get(j).toString());
          }
        }
        Quaternion qt[] = bt.get(0).getRotations();
        if (traceLoad) System.out.println("bt - rotations = " + qt.length);

        for (int j = 0; j < qt.length; j++) {
          if (traceLoad) System.out.println("rots = " + qt[j]);
          float[] fromAngles = qt[j].toAngles(null);
          if (traceLoad)
            System.out.println(
                "Angles - " + fromAngles[0] + " - " + fromAngles[1] + " - " + fromAngles[2]);

          angleX[j] = angleX[j] + fromAngles[0];
          angleY[j] = angleY[j] + fromAngles[1];
          angleZ[j] = angleZ[j] + fromAngles[2];
        }
        if (traceLoad)
          System.out.println(
              " start frame = " + ani.getStartFrame() + " - end frame = " + ani.getEndFrame());
        Vector3f v3f[] = bt.get(0).getTranslations();
        if (traceLoad) System.out.println("bt - translations = " + v3f.length);

        for (int j = 0; j < v3f.length; j++) {
          if (traceLoad) System.out.println("trans = " + v3f[j]);
          transX[j] = transX[j] + v3f[j].x;
          transY[j] = transY[j] + v3f[j].y;
          transZ[j] = transZ[j] + v3f[j].z;
        }
      }
    }
    // Build the final key frame Quats from the extracted angles and the Vectors from the extracted
    // translates
    for (int j = 0; j < Tkft.length; j++) {
      Quaternion temp = null;
      temp = new Quaternion();
      temp.fromAngles(angleX[j], angleY[j], angleZ[j]);
      if (traceLoad)
        System.out.println(
            " ********************** final angles "
                + angleX[j]
                + "--"
                + angleY[j]
                + "--"
                + angleZ[j]);
      node3keyFrames[j] = temp;
      node3keyTrans[j] = new Vector3f(transX[j], transY[j], transZ[j]);
      if (traceLoad)
        System.out.println(
            " *************** final translations "
                + transX[j]
                + " - "
                + transY[j]
                + " - "
                + transZ[j]);
    }
  }
Exemple #15
0
 public void set(Vec2 pos, float angle) {
   position = pos;
   rotation = new Quaternion();
   rotation.fromAngleAxis(angle, new Vector3f(0, 0, 1));
 }
  /**
   * updateSkin positions the vertices of the skin based on the bones and the BoneInfluences those
   * bones have on the vertices. Each vertex is placed into world space for rendering.
   */
  public synchronized void updateSkin() {
    if (cache == null || skins == null) return;

    if (skeleton != null) {
      if (skeleton.getParent() != null) {
        tmpTranslation.set(skeleton.getParent().getWorldTranslation());
        tmpRotation.set(skeleton.getParent().getWorldRotation());
        tmpScale.set(skeleton.getParent().getWorldScale());

        skeleton.getParent().getWorldTranslation().set(0, 0, 0);
        skeleton.getParent().getWorldRotation().set(0, 0, 0, 1);
        skeleton.getParent().getWorldScale().set(1, 1, 1);
        skeleton.updateWorldVectors(true);
      }

      skeleton.update();
    }

    FloatBuffer verts, norms;

    // Note that it would be simpler to .clear() then .flip() the
    // Buffers, but since we want to support the potential case of
    // leaving vertexes at the end of the buffer unmodified, we must
    // just set the write buffer and leave the limit setting alone.
    for (int index = cache.length - 1; index >= 0; --index) {
      Geometry geom = getSkin(index);
      verts = geom.getVertexBuffer();
      if (verts == null) {
        logger.log(Level.FINE, "Skipping skin ''{0}'' because verts uninitialized", geom.getName());
        continue;
      }
      verts.rewind();
      norms = geom.getNormalBuffer();
      norms.rewind();
      geom.setHasDirtyVertices(true);
      if (cache[index].length * 3 > verts.limit())
        throw new IllegalStateException(
            "Skin "
                + getName()
                + ':'
                + geom.getName()
                + " has more influences than "
                + " vertexes.  "
                + cache[index].length
                + " vs. "
                + verts.limit()
                + "/3");
      if (cache[index].length * 3 > norms.limit())
        throw new IllegalStateException(
            "Skin "
                + getName()
                + ':'
                + geom.getName()
                + " has more influences than "
                + " normals.  "
                + cache[index].length
                + " vs. "
                + norms.limit()
                + "/3");
      if (cache[index].length * 3 < verts.limit())
        logger.log(
            Level.WARNING,
            "Skin ''{0}:{1}'' has fewer " + "influences than vertexes.  {2} vs {3}/3",
            new Object[] {getName(), geom.getName(), cache[index].length, verts.limit()});
      if (cache[index].length * 3 < norms.limit())
        logger.log(
            Level.WARNING,
            "Skin ''{0}:{1}'' has fewer " + "influences than normals.  {2} vs {3}/3",
            new Object[] {getName(), geom.getName(), cache[index].length, norms.limit()});

      for (int vert = 0, max = cache[index].length; vert < max; vert++) {
        ArrayList<BoneInfluence> infs = cache[index][vert];
        if (infs == null || infs.size() < 1) {
          verts.position(verts.position() + 3);
          norms.position(norms.position() + 3);
          continue;
        }
        vertex.zero();
        normal.zero();

        for (int x = infs.size() - 1; x >= 0; --x) {
          BoneInfluence inf = infs.get(x);
          if (inf.bone != null) inf.bone.applyBone(inf, vertex, normal);
        }

        verts.put(vertex.x).put(vertex.y).put(vertex.z);
        norms.put(normal.x).put(normal.y).put(normal.z);
      }
      verts.rewind();
      norms.rewind();
    }

    if (skeleton != null && skeleton.getParent() != null) {
      skeleton.getParent().getWorldTranslation().set(tmpTranslation);
      skeleton.getParent().getWorldRotation().set(tmpRotation);
      skeleton.getParent().getWorldScale().set(tmpScale);
      skeleton.updateWorldVectors(true);
    }
  }
  protected void simpleInitGame() {
    // Setup camera
    cam.setFrustumPerspective(
        55.0f, (float) display.getWidth() / (float) display.getHeight(), 1, 1000);

    // Setup lights
    PointLight light = new PointLight();
    light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
    light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
    light.setLocation(new Vector3f(0, 30, 0));
    light.setEnabled(true);
    lightState.attach(light);

    // Add dummy objects to rootNode
    rootNode.attachChild(createObjects());

    try {
      MaxToJme C1 = new MaxToJme();
      ByteArrayOutputStream BO = new ByteArrayOutputStream();
      URL maxFile =
          TestMaxJmeWrite.class.getClassLoader().getResource("jmetest/data/model/Character.3DS");
      C1.convert(new BufferedInputStream(maxFile.openStream()), BO);
      Node r = (Node) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
      r.setLocalScale(.1f);
      if (r.getChild(0).getControllers().size() != 0) r.getChild(0).getController(0).setSpeed(20);
      Quaternion temp = new Quaternion();
      temp.fromAngleAxis(FastMath.PI / 2, new Vector3f(-1, 0, 0));
      r.setLocalRotation(temp);
      r.setLocalTranslation(new Vector3f(0, 3, 0));
      rootNode.attachChild(r);
    } catch (IOException e) {
      logger.log(Level.SEVERE, "Error loading max file", e);
    }

    // Setup renderpasses
    RenderPass rootPass = new RenderPass();
    rootPass.add(rootNode);
    pManager.add(rootPass);

    sketchRenderPass = new SketchRenderPass(cam, 2);
    if (!sketchRenderPass.isSupported()) {
      Text t = new Text("Text", "GLSL Not supported on this computer.");
      t.setRenderQueueMode(Renderer.QUEUE_ORTHO);
      t.setLightCombineMode(LightState.OFF);
      t.setLocalTranslation(new Vector3f(0, 20, 0));
      fpsNode.attachChild(t);
    } else {
      sketchRenderPass.add(rootNode);
      pManager.add(sketchRenderPass);
    }

    if (!sketchRenderPass.isSupported()) {
      Text t = new Text("Text", "GLSL Not supported on this computer.");
      t.setRenderQueueMode(Renderer.QUEUE_ORTHO);
      t.setLightCombineMode(LightState.OFF);
      t.setLocalTranslation(new Vector3f(0, 20, 0));
      fpsNode.attachChild(t);
    }

    RenderPass fpsPass = new RenderPass();
    fpsPass.add(fpsNode);
    pManager.add(fpsPass);

    // Initialize keybindings
    KeyBindingManager.getKeyBindingManager().set("1", KeyInput.KEY_1);
    KeyBindingManager.getKeyBindingManager().set("2", KeyInput.KEY_2);
    KeyBindingManager.getKeyBindingManager().set("3", KeyInput.KEY_3);
    KeyBindingManager.getKeyBindingManager().set("4", KeyInput.KEY_4);
    KeyBindingManager.getKeyBindingManager().set("5", KeyInput.KEY_5);
    KeyBindingManager.getKeyBindingManager().set("6", KeyInput.KEY_6);
    KeyBindingManager.getKeyBindingManager().set("7", KeyInput.KEY_7);
    KeyBindingManager.getKeyBindingManager().set("8", KeyInput.KEY_8);
    KeyBindingManager.getKeyBindingManager().set("9", KeyInput.KEY_9);
    KeyBindingManager.getKeyBindingManager().set("0", KeyInput.KEY_0);

    KeyBindingManager.getKeyBindingManager().set("y", KeyInput.KEY_Y);
    KeyBindingManager.getKeyBindingManager().set("h", KeyInput.KEY_H);

    KeyBindingManager.getKeyBindingManager().set("shot", KeyInput.KEY_F4);
  }
  /** Rotates the quad on which the texture is displayed. Does not change the texture itself. */
  public void rotateTexture(float angleInDegrees) {
    Quaternion q = new Quaternion();

    q.fromAngleAxis(angleInDegrees * FastMath.DEG_TO_RAD, Vector3f.UNIT_Z);
    visibleQuad.setLocalRotation(q);
  }
  public void animateNode() {
    Vector3f v3f;

    int startFrame = 0;
    int endFrame = 0;
    float zRotationInc = 0.0f;
    float xRotationInc = 0.0f;

    System.out.println("animateNode");

    v3f = node1theSpatial.getLocalTranslation();
    System.out.println("Before animation - " + v3f + " - After last run = " + v3fFinal);

    node1result = new TimelineScenario.Sequence();
    // Add each key frame info as an animation and then play the whole
    if (animationStartKeyframe == 0 && animationEndKeyframe == 0) {
      startFrame = 0;
      endFrame = Tkft.length - 1;
    } else {
      startFrame = animationStartKeyframe;
      endFrame = animationEndKeyframe - 1;
    }

    for (int j = startFrame; j < endFrame; j++) {
      t = new Timeline(this);
      // *******************************************  Node 1
      float[] fromAngles = node1keyFrames[j].toAngles(null);
      float[] fromAnglesNext = node1keyFrames[j + 1].toAngles(null);

      float[] rotFinalAngles = rotFinal.toAngles(null);

      Quaternion quat = new Quaternion();
      quat.fromAngles(
          //                    fromAngles[0] + rotFinalAngles[0] + animationStartRotation[0],
          //                    fromAngles[1] + rotFinalAngles[1] + animationStartRotation[1],
          //                    fromAngles[2] + rotFinalAngles[2] + animationStartRotation[2]);
          fromAngles[0] + animationStartRotation[0],
          fromAngles[1] + rotFinalAngles[1] + animationStartRotation[1],
          fromAngles[2] + animationStartRotation[2]);
      Quaternion quatNext = new Quaternion();
      quatNext.fromAngles(
          //                    fromAnglesNext[0] + rotFinalAngles[0] + animationStartRotation[0],
          //                    fromAnglesNext[1] + rotFinalAngles[1] + animationStartRotation[1],
          //                    fromAnglesNext[2] + rotFinalAngles[2] + animationStartRotation[2]);
          fromAnglesNext[0] + animationStartRotation[0],
          fromAnglesNext[1] + rotFinalAngles[1] + animationStartRotation[1],
          fromAnglesNext[2] + animationStartRotation[2]);
      //            System.out.println("rots - x = " +
      //                    (fromAngles[0] + rotFinalAngles[0] + animationStartRotation[0]) + " - y
      // = " +
      //                    (fromAngles[1] + rotFinalAngles[1] + animationStartRotation[1]) + " - z
      // = " +
      //                    (fromAngles[2] + rotFinalAngles[2] + animationStartRotation[2]));

      t.addPropertyToInterpolate(
          "Node1Quat", quat, quatNext, new ModulatorQuaternionInterpolator());

      ////            t.addPropertyToInterpolate("Node1Quat", node1keyFrames[j], node1keyFrames[j +
      // 1], new ModulatorQuaternionInterpolator());

      zRotationInc = (float) Math.sin(animationStartRotation[1]);
      xRotationInc = (float) Math.cos(animationStartRotation[1]);

      if (v3fFinal.x == 0.0f && v3fFinal.y == 0.0 && v3fFinal.z == 0.0) {
        Vector3f tempVec =
            new Vector3f(
                (node1keyTrans[j].x + animationStartTranslate.x),
                (node1keyTrans[j].y + animationStartTranslate.y),
                (node1keyTrans[j].z + animationStartTranslate.z));
        Vector3f tempVecPlus =
            new Vector3f(
                (node1keyTrans[j + 1].x + animationStartTranslate.x),
                (node1keyTrans[j + 1].y + animationStartTranslate.y),
                (node1keyTrans[j + 1].z + animationStartTranslate.z));
        //                System.out.println("x = " +
        //                        (node1keyTrans[j].x + animationStartTranslate.x) + " - y = " +
        //                        (node1keyTrans[j].y + animationStartTranslate.y) + " - z = " +
        //                        (node1keyTrans[j].z + animationStartTranslate.z));
        t.addPropertyToInterpolate(
            "Node1Trans", tempVec, tempVecPlus, new ModulatorVectorInterpolator());
      } else {
        Vector3f tempVec =
            new Vector3f(
                node1keyTrans[j].x
                    + v3fFinal.x
                    + (node1keyTrans[j].x * (float) Math.sin(rotFinalAngles[1])),
                node1keyTrans[j].y + v3fFinal.y,
                node1keyTrans[j].z
                    + v3fFinal.z
                    + (node1keyTrans[j].z * (float) Math.cos(rotFinalAngles[1])));
        Vector3f tempVecPlus =
            new Vector3f(
                node1keyTrans[j + 1].x
                    + v3fFinal.x
                    + (node1keyTrans[j + 1].x * (float) Math.sin(rotFinalAngles[1])),
                node1keyTrans[j + 1].y + v3fFinal.y,
                node1keyTrans[j + 1].z
                    + v3fFinal.z
                    + (node1keyTrans[j + 1].z * (float) Math.cos(rotFinalAngles[1])));
        //                System.out.println("final - x = " +
        //                        (node1keyTrans[j].x + v3fFinal.x + (node1keyTrans[j].x *
        // (float)Math.sin(rotFinalAngles[1]))) + " - y = " +
        //                        (node1keyTrans[j].y + v3fFinal.y) + " - z = " +
        //                        (node1keyTrans[j].z + v3fFinal.z + (node1keyTrans[j].z *
        // (float)Math.cos(rotFinalAngles[1]))));
        t.addPropertyToInterpolate(
            "Node1Trans", tempVec, tempVecPlus, new ModulatorVectorInterpolator());
      }
      if (j == endFrame - 1) {
        t.addCallback(
            new TimelineCallback() {

              public void onTimelineDone() {
                Vector3f v3f = node1theSpatial.getLocalTranslation();
                System.out.println("After animation from timeline done - " + v3f);
              }

              public void onTimelineStateChanged(
                  TimelineState oldState, TimelineState newState, float arg2, float arg3) {
                if (newState == TimelineState.DONE) {
                  if (animationSaveTransform) {
                    v3fFinal = node1theSpatial.getLocalTranslation();
                    rotFinal = node1theSpatial.getLocalRotation();
                  }
                  System.out.println("After animation - " + v3fFinal);
                  ClientContext.getInputManager()
                      .postEvent(new IntercellEvent("F", animationIceCode));
                }
              }

              public void onTimelinePulse(float arg0, float arg1) {}
            });
      }

      ////            t.addPropertyToInterpolate("Node1Trans", node1keyTrans[j], node1keyTrans[j +
      // 1], new ModulatorVectorInterpolator());
      //        t.setEase(new Spline(0.4f));
      t.setDuration((long) ((Tkft[j + 1] - Tkft[j]) * 1000) * animationTimeMultiplier);
      node1result.addScenarioActor(t);
    }

    node2result = new TimelineScenario.Sequence();
    // ***************************************** Node 2
    // Add each key frame info as an animation and then play the whole
    for (int j = startFrame; j < endFrame; j++) {
      t = new Timeline(this);
      t.addPropertyToInterpolate(
          "Node2Quat",
          node2keyFrames[j],
          node2keyFrames[j + 1],
          new ModulatorQuaternionInterpolator());

      ///            Vector3f tempVec = new Vector3f(node2keyTrans[j].x + animationStartTranslate.x,
      // node2keyTrans[j].y + animationStartTranslate.y, node2keyTrans[j].z +
      // animationStartTranslate.z);
      ///            Vector3f tempVecPlus = new Vector3f(node2keyTrans[j + 1].x +
      // animationStartTranslate.x, node2keyTrans[j + 1].y + animationStartTranslate.y,
      // node2keyTrans[j + 1].z + animationStartTranslate.z);
      ///            t.addPropertyToInterpolate("Node2Trans", tempVec, tempVecPlus, new
      // ModulatorVectorInterpolator());
      /// System.out.println("node2 - orig y = " + node2keyTrans[j].y + " - shift - " + tempVec.y);
      t.addPropertyToInterpolate(
          "Node2Trans", node2keyTrans[j], node2keyTrans[j + 1], new ModulatorVectorInterpolator());
      //        t.setEase(new Spline(0.4f));
      t.setDuration((long) ((Tkft[j + 1] - Tkft[j]) * 1000) * animationTimeMultiplier);
      node2result.addScenarioActor(t);
    }

    node3result = new TimelineScenario.Sequence();
    // *******************************************************  Node 3
    // Add each key frame info as an animation and then play the whole
    for (int j = startFrame; j < endFrame; j++) {
      t = new Timeline(this);
      t.addPropertyToInterpolate(
          "Node3Quat",
          node3keyFrames[j],
          node3keyFrames[j + 1],
          new ModulatorQuaternionInterpolator());

      ///            Vector3f tempVec = new Vector3f(node3keyTrans[j].x + animationStartTranslate.x,
      // node3keyTrans[j].y + animationStartTranslate.y, node3keyTrans[j].z +
      // animationStartTranslate.z);
      ///            Vector3f tempVecPlus = new Vector3f(node3keyTrans[j + 1].x +
      // animationStartTranslate.x, node3keyTrans[j + 1].y + animationStartTranslate.y,
      // node3keyTrans[j + 1].z + animationStartTranslate.z);
      ///            t.addPropertyToInterpolate("Node3Trans", tempVec, tempVecPlus, new
      // ModulatorVectorInterpolator());
      /// System.out.println("node3 - orig y = " + node3keyTrans[j].y + " - shift - " + tempVec.y);
      t.addPropertyToInterpolate(
          "Node3Trans", node3keyTrans[j], node3keyTrans[j + 1], new ModulatorVectorInterpolator());
      //        t.setEase(new Spline(0.4f));
      t.setDuration((long) ((Tkft[j + 1] - Tkft[j]) * 1000) * animationTimeMultiplier);
      node3result.addScenarioActor(t);
    }

    node1result.play();
    node2result.play();
    node3result.play();
  }
  @Override
  public void update(float time) {
    if (isActive()) {
      if (getRepeatType() == Controller.RT_WRAP) {
        if (translationDelta.x != 0 || translationDelta.y != 0 || translationDelta.z != 0) {
          currentTranslation.x += getSpeed() * time * translationDelta.x;
          currentTranslation.y += getSpeed() * time * translationDelta.y;
          currentTranslation.z += getSpeed() * time * translationDelta.z;

          if (currentTranslation.x > xRepeat || currentTranslation.x < (-1 * xRepeat)) {
            currentTranslation.x = 0;
          }

          if (currentTranslation.y > yRepeat || currentTranslation.y < (-1 * yRepeat)) {
            currentTranslation.y = 0;
          }

          if (currentTranslation.z > zRepeat || currentTranslation.z < (-1 * zRepeat)) {
            currentTranslation.z = 0;
          }
        }

        if (rotationDelta != 0) {
          currentRotationAngle += rotationDelta * getSpeed() * time;
          if (currentRotationAngle > FastMath.TWO_PI) {
            currentRotationAngle = 0;
          } else if (currentRotationAngle < 0) {
            currentRotationAngle = FastMath.TWO_PI;
          }
          currentRotation.fromAngleNormalAxis(currentRotationAngle, textureRotationAxis);
        }
      } else if (getRepeatType() == Controller.RT_CLAMP) {
        if (translationDelta.x != 0 || translationDelta.y != 0 || translationDelta.z != 0) {
          if (currentTranslation.x <= xRepeat && currentTranslation.x >= (-1 * xRepeat)) {
            currentTranslation.x += getSpeed() * time * translationDelta.x;
          }

          if (currentTranslation.y <= yRepeat && currentTranslation.y >= (-1 * yRepeat)) {
            currentTranslation.y += getSpeed() * time * translationDelta.y;
          }

          if (currentTranslation.z <= zRepeat && currentTranslation.z >= (-1 * zRepeat)) {
            currentTranslation.z += getSpeed() * time * translationDelta.z;
          }
        }

        if (rotationDelta != 0) {
          if (currentRotationAngle <= FastMath.TWO_PI) {
            currentRotationAngle += rotationDelta * getSpeed() * time;
            currentRotation.fromAngleNormalAxis(currentRotationAngle, textureRotationAxis);
          }
        }
      } else if (getRepeatType() == Controller.RT_CYCLE) {
        if (translationDelta.x != 0 || translationDelta.y != 0 || translationDelta.z != 0) {
          if (currentTranslation.x > xRepeat || currentTranslation.x < (-1 * xRepeat)) {
            translationDelta.x *= -1;
          }

          if (currentTranslation.y > yRepeat || currentTranslation.y < (-1 * yRepeat)) {
            translationDelta.y *= -1;
          }

          if (currentTranslation.z > zRepeat || currentTranslation.z < (-1 * zRepeat)) {
            translationDelta.z *= -1;
          }

          currentTranslation.x += getSpeed() * time * translationDelta.x;
          currentTranslation.y += getSpeed() * time * translationDelta.y;
          currentTranslation.z += getSpeed() * time * translationDelta.z;
        }

        if (rotationDelta != 0) {
          if (currentRotationAngle > FastMath.TWO_PI || currentRotationAngle < 0) {
            rotationDelta *= -1;
          }

          currentRotationAngle += rotationDelta * getSpeed() * time;
          currentRotation.fromAngleNormalAxis(currentRotationAngle, textureRotationAxis);
        }
      }

      for (int i = 0; i < textures.length; i++) {
        textures[i].setTranslation(currentTranslation);
        textures[i].setRotation(currentRotation);
      }
    }
  }