Exemplo n.º 1
0
  /**
   * Calculates the minimum bounding sphere of 4 points. Used in welzl's algorithm.
   *
   * @param O The 1st point inside the sphere.
   * @param A The 2nd point inside the sphere.
   * @param B The 3rd point inside the sphere.
   * @param C The 4th point inside the sphere.
   * @see #calcWelzl(java.nio.FloatBuffer)
   */
  private void setSphere(final Vector3 O, final Vector3 A, final Vector3 B, final Vector3 C) {
    final Vector3 a = A.subtract(O, null);
    final Vector3 b = B.subtract(O, null);
    final Vector3 c = C.subtract(O, null);

    final double Denominator =
        2.0
            * (a.getX() * (b.getY() * c.getZ() - c.getY() * b.getZ())
                - b.getX() * (a.getY() * c.getZ() - c.getY() * a.getZ())
                + c.getX() * (a.getY() * b.getZ() - b.getY() * a.getZ()));
    if (Denominator == 0) {
      _center.set(0, 0, 0);
      setRadius(0);
    } else {
      final Vector3 o =
          a.cross(b, null)
              .multiplyLocal(c.lengthSquared())
              .addLocal(c.cross(a, null).multiplyLocal(b.lengthSquared()))
              .addLocal(b.cross(c, null).multiplyLocal(a.lengthSquared()))
              .divideLocal(Denominator);

      setRadius(o.length() * radiusEpsilon);
      O.add(o, _center);
    }
  }
Exemplo n.º 2
0
  public void reconstruct(final Vector3 top, final Vector3 bottom, final double radius) {
    // our temp vars
    final Vector3 localTranslation = Vector3.fetchTempInstance();
    final Vector3 capsuleUp = Vector3.fetchTempInstance();

    // first make the capsule the right shape
    height = top.distance(bottom);
    this.radius = radius;
    setGeometryData();

    // now orient it in space.
    localTranslation.set(_localTransform.getTranslation());
    top.add(bottom, localTranslation).multiplyLocal(.5);

    // rotation that takes us from 0,1,0 to the unit vector described by top/center.
    top.subtract(localTranslation, capsuleUp).normalizeLocal();
    final Matrix3 rotation = Matrix3.fetchTempInstance();
    rotation.fromStartEndLocal(Vector3.UNIT_Y, capsuleUp);
    _localTransform.setRotation(rotation);

    Vector3.releaseTempInstance(localTranslation);
    Vector3.releaseTempInstance(capsuleUp);
    Matrix3.releaseTempInstance(rotation);

    updateWorldTransform(false);
  }
Exemplo n.º 3
0
  /**
   * Calculates the minimum bounding sphere of 3 points. Used in welzl's algorithm.
   *
   * @param O The 1st point inside the sphere.
   * @param A The 2nd point inside the sphere.
   * @param B The 3rd point inside the sphere.
   * @see #calcWelzl(java.nio.FloatBuffer)
   */
  private void setSphere(final Vector3 O, final Vector3 A, final Vector3 B) {
    final Vector3 a = A.subtract(O, null);
    final Vector3 b = B.subtract(O, null);
    final Vector3 acrossB = a.cross(b, null);

    final double Denominator = 2.0 * acrossB.dot(acrossB);

    if (Denominator == 0) {
      _center.set(0, 0, 0);
      setRadius(0);
    } else {

      final Vector3 o =
          acrossB
              .cross(a, null)
              .multiplyLocal(b.lengthSquared())
              .addLocal(b.cross(acrossB, null).multiplyLocal(a.lengthSquared()))
              .divideLocal(Denominator);
      setRadius(o.length() * radiusEpsilon);
      O.add(o, _center);
    }
  }
Exemplo n.º 4
0
  /**
   * Update the vertices for this particle, taking size, spin and viewer into consideration. In the
   * case of particle type ParticleType.GeomMesh, the original triangle normal is maintained rather
   * than rotating it to face the camera or parent vectors.
   *
   * @param cam Camera to use in determining viewer aspect. If null, or if parent is not set to
   *     camera facing, parent's left and up vectors are used.
   */
  public void updateVerts(final Camera cam) {
    final double orient = parent.getParticleOrientation() + values[VAL_CURRENT_SPIN];
    final double currSize = values[VAL_CURRENT_SIZE];

    if (type == ParticleSystem.ParticleType.GeomMesh
        || type == ParticleSystem.ParticleType.Point) {; // nothing to do
    } else if (cam != null && parent.isCameraFacing()) {
      final ReadOnlyVector3 camUp = cam.getUp();
      final ReadOnlyVector3 camLeft = cam.getLeft();
      final ReadOnlyVector3 camDir = cam.getDirection();
      if (parent.isVelocityAligned()) {
        bbX.set(_velocity).normalizeLocal().multiplyLocal(currSize);
        camDir.cross(bbX, bbY).normalizeLocal().multiplyLocal(currSize);
      } else if (orient == 0) {
        bbX.set(camLeft).multiplyLocal(currSize);
        bbY.set(camUp).multiplyLocal(currSize);
      } else {
        final double cA = MathUtils.cos(orient) * currSize;
        final double sA = MathUtils.sin(orient) * currSize;
        bbX.set(camLeft)
            .multiplyLocal(cA)
            .addLocal(camUp.getX() * sA, camUp.getY() * sA, camUp.getZ() * sA);
        bbY.set(camLeft)
            .multiplyLocal(-sA)
            .addLocal(camUp.getX() * cA, camUp.getY() * cA, camUp.getZ() * cA);
      }
    } else {
      bbX.set(parent.getLeftVector()).multiplyLocal(0);
      bbY.set(parent.getUpVector()).multiplyLocal(0);
    }

    final Vector3 tempVec3 = Vector3.fetchTempInstance();
    final FloatBuffer vertexBuffer = parent.getParticleGeometry().getMeshData().getVertexBuffer();
    switch (type) {
      case Quad:
        {
          _position.subtract(bbX, tempVec3).subtractLocal(bbY);
          BufferUtils.setInBuffer(tempVec3, vertexBuffer, startIndex + 0);

          _position.subtract(bbX, tempVec3).addLocal(bbY);
          BufferUtils.setInBuffer(tempVec3, vertexBuffer, startIndex + 1);

          _position.add(bbX, tempVec3).addLocal(bbY);
          BufferUtils.setInBuffer(tempVec3, vertexBuffer, startIndex + 2);

          _position.add(bbX, tempVec3).subtractLocal(bbY);
          BufferUtils.setInBuffer(tempVec3, vertexBuffer, startIndex + 3);
          break;
        }
      case GeomMesh:
        {
          final Quaternion tempQuat = Quaternion.fetchTempInstance();
          final ReadOnlyVector3 norm = triModel.getNormal();
          if (orient != 0) {
            tempQuat.fromAngleNormalAxis(orient, norm);
          }

          for (int x = 0; x < 3; x++) {
            if (orient != 0) {
              tempQuat.apply(triModel.get(x), tempVec3);
            } else {
              tempVec3.set(triModel.get(x));
            }
            tempVec3.multiplyLocal(currSize).addLocal(_position);
            BufferUtils.setInBuffer(tempVec3, vertexBuffer, startIndex + x);
          }
          Quaternion.releaseTempInstance(tempQuat);
          break;
        }
      case Triangle:
        {
          _position
              .subtract(3 * bbX.getX(), 3 * bbX.getY(), 3 * bbX.getZ(), tempVec3)
              .subtractLocal(bbY);
          BufferUtils.setInBuffer(tempVec3, vertexBuffer, startIndex + 0);

          _position.add(bbX, tempVec3).addLocal(3 * bbY.getX(), 3 * bbY.getY(), 3 * bbY.getZ());
          BufferUtils.setInBuffer(tempVec3, vertexBuffer, startIndex + 1);

          _position.add(bbX, tempVec3).subtractLocal(bbY);
          BufferUtils.setInBuffer(tempVec3, vertexBuffer, startIndex + 2);
          break;
        }
      case Line:
        {
          _position.subtract(bbX, tempVec3);
          BufferUtils.setInBuffer(tempVec3, vertexBuffer, startIndex);

          _position.add(bbX, tempVec3);
          BufferUtils.setInBuffer(tempVec3, vertexBuffer, startIndex + 1);
          break;
        }
      case Point:
        {
          BufferUtils.setInBuffer(_position, vertexBuffer, startIndex);
          break;
        }
    }
    Vector3.releaseTempInstance(tempVec3);
  }