public double f1(Vector3f point, Vector3f A, Vector3f B) {
    // ****** basic parameters ******
    Vector3f vec_b = B;
    Vector3f vec_a = A.subtract(vec_b);
    Vector3f vec_r = point;
    double L = vec_a.length();
    Vector3f vec_d = vec_r.subtract(vec_b);
    double d = vec_d.length();
    double h = vec_d.dot(vec_a.normalize());

    // terms
    double s2 = s * s;
    double h2 = h * h;
    double lmh = L - h;
    double d2 = d * d;
    double p2 = 1 + s2 * (d2 - h2);
    double p = Math.sqrt(p2);
    double ww = p2 + s2 * h2;
    double qq = p2 + s2 * lmh * lmh;
    double sdp = s / p;
    double tpp = 2.0 * p2;
    double sp = s * p;
    double term = Math.atan(sdp * h) + Math.atan(sdp * lmh);

    return (h / ww + lmh / qq) / tpp + term / (tpp * sp);
  }
  public double flt(Vector3f point, Vector3f A, Vector3f B) {
    Vector3f vec_b = B;
    Vector3f vec_a = A.subtract(vec_b);
    double L = vec_a.length();

    return L * f1(point, A, B) - ft(point, A, B);
  }
 /**
  * This method applies the variation to the particle with already set velocity.
  *
  * @param particle the particle to be affected
  */
 protected void applyVelocityVariation(Particle particle) {
   particle.velocity.set(initialVelocity);
   temp.set(FastMath.nextRandomFloat(), FastMath.nextRandomFloat(), FastMath.nextRandomFloat());
   temp.multLocal(2f);
   temp.subtractLocal(1f, 1f, 1f);
   temp.multLocal(initialVelocity.length());
   particle.velocity.interpolate(temp, velocityVariation);
 }
  /**
   * Method for calculating new velocity of agent based on steering vector.
   *
   * @see AbstractSteeringBehavior#calculateSteering()
   * @return The new velocity for this agent based on steering vector
   */
  protected Vector3f calculateNewVelocity() {
    agent.setAcceleration(calculateSteering().mult(1 / agentTotalMass()));
    velocity = velocity.add(agent.getAcceleration());
    agent.setVelocity(velocity);

    if (velocity.length() > agent.getMaxMoveSpeed()) {
      velocity = velocity.normalize().mult(agent.getMaxMoveSpeed());
    }
    return velocity;
  }
  public double ft(Vector3f point, Vector3f A, Vector3f B) {
    // ****** basic parameters ******
    Vector3f vec_b = B;
    Vector3f vec_a = A.subtract(vec_b);
    Vector3f vec_r = point;
    double L = vec_a.length();
    Vector3f vec_d = vec_r.subtract(vec_b);
    double d = vec_d.length();
    double h = vec_d.dot(vec_a.normalize());

    // terms
    double s2 = s * s;
    double h2 = h * h;
    double lmh = L - h;
    double d2 = d * d;
    double p2 = 1 + s2 * (d2 - h2);
    double ww = p2 + s2 * h2;
    double qq = p2 + s2 * lmh * lmh;

    double ts2 = 2.0 * s2;
    return h * f1(point, A, B) + (1.0 / ww - 1.0 / qq) / ts2;
  }
  void lookAt(Vector3f pos) {
    Vector3f location = spatial.getLocalTranslation();
    Vector3f target = pos.clone();

    Vector3f distance = target.subtract(location);
    if (distance.length() < 0.3f) {
      return;
    }
    Vector3f newVel = distance.normalize();
    Quaternion q = new Quaternion();
    q.lookAt(newVel, Vector3f.UNIT_Y);
    // q.addLocal(playerInfo.initalRot);
    spatial.setLocalRotation(q);
  }
  void moveTo(Vector3f pos) {
    float speed = playerInfo.getRealSpeed();
    Vector3f location = spatial.getLocalTranslation();
    Vector3f target = pos.clone();

    Vector3f distance = target.subtract(location);
    if (distance.length() < 0.3f) {
      return;
    }
    Vector3f newVel = distance.normalize().mult(speed);
    // Vector3f steering = desierdVel.subtract(velocity).negate();

    spatial.setLocalTranslation(location.add(newVel));
  }
Exemple #8
0
  private void addWheel(VehicleControl v, ModelBase model, WheelComponent wheel) {
    Vector3f offset = arrayToVector(wheel.offset());
    Vector3f direction = arrayToVector(wheel.direction());
    Vector3f axe = arrayToVector(wheel.axe());

    if (offset.length() == 0) offset = model.getChild(wheel.nodeName()).getLocalTranslation();

    v.addWheel(
        model.getChild(wheel.nodeName()),
        offset,
        direction,
        axe,
        wheel.restLength(),
        wheel.radius(),
        wheel.frontWheel());
  }
  private void applyGravity(Spatial blackHole, Spatial target, float tpf) {
    Vector3f difference = blackHole.getLocalTranslation().subtract(target.getLocalTranslation());

    Vector3f gravity = difference.normalize().multLocal(tpf);
    float distance = difference.length();

    if (target.getName().equals("Player")) {
      gravity.multLocal(250f / distance);
      target.getControl(PlayerControl.class).applyGravity(gravity.mult(80f));
    } else if (target.getName().equals("Bullet")) {
      gravity.multLocal(250f / distance);
      target.getControl(BulletControl.class).applyGravity(gravity.mult(-0.8f));
    } else if (target.getName().equals("Seeker")) {
      target.getControl(SeekerControl.class).applyGravity(gravity.mult(150000));
    } else if (target.getName().equals("Wanderer")) {
      target.getControl(WandererControl.class).applyGravity(gravity.mult(150000));
    } else if (target.getName().equals("Laser") || target.getName().equals("Glow")) {
      target.getControl(ParticleControl.class).applyGravity(gravity.mult(15000), distance);
    }
  }
Exemple #10
0
  @Override
  public void simpleUpdate(float tpf) {
    for (Planet planet : planets) {
      planet.getGeom().rotate(0, 0, planet.getRotationSpeed() * tpf);
      planet.getPivot().rotate(0, planet.getTranslationSpeed() * tpf, 0);
    }

    for (Spatial spatial : explosions.getChildren()) {
      ParticleEmitter explosion = (ParticleEmitter) spatial;
      if (explosion.getNumVisibleParticles() < 1) {
        explosion.removeFromParent();
      }
    }

    Vector3f rotation = new Vector3f(0, 0, 0);
    if (left) {
      rotation.addLocal(0, 1, 0);
    }
    if (right) {
      rotation.addLocal(0, -1, 0);
    }
    if (up) {
      rotation.addLocal(1, 0, 0);
    }
    if (down) {
      rotation.addLocal(-1, 0, 0);
    }
    if (leftSide) {
      rotation.addLocal(0, 0, 1);
    }
    if (rightSide) {
      rotation.addLocal(0, 0, -1);
    }

    Vector3f transformed = new Vector3f(0, 0, 0);
    spaceship.getLocalRotation().mult(rotation, transformed);
    spaceship.getControl().setAngularVelocity(transformed);

    Vector3f movement = new Vector3f(0, 0, 0);
    if (moving) {
      spaceship.getWorldRotation().mult(new Vector3f(0, 0, -6), movement);
    }
    spaceship.getControl().setLinearVelocity(movement);

    bloom.setBloomIntensity(bloom.getBloomIntensity() + (bloomDirection * tpf / 8));
    if (bloom.getBloomIntensity() > 4) {
      bloomDirection = -1;
    }
    if (bloom.getBloomIntensity() < 2) {
      bloomDirection = 1;
    }

    Vector3f direction = spaceship.getRear().getWorldTranslation().subtract(cam.getLocation());
    float magnitude = direction.length();
    if (magnitude > 0) {
      cam.setLocation(
          cam.getLocation().add(direction.normalize().mult(tpf * magnitude * magnitude / 2)));
    }
    cam.lookAt(spaceship.getFront().getWorldTranslation(), Vector3f.UNIT_Y);

    for (Spatial spatial : asteroids.getChildren()) {
      if (spatial.getWorldTranslation().subtract(Vector3f.ZERO).length() > 200) {
        spatial.removeFromParent();
      }
    }

    if (Math.random() < 0.01 && asteroids.getChildren().size() < MAX_ASTEROIDS) {
      generateRandomAsteroid();
    }

    for (Spatial spatial : lasers.getChildren()) {
      Laser laser = (Laser) spatial;
      if (laser.getWorldTranslation().subtract(Vector3f.ZERO).length() > 200) {
        bap.getPhysicsSpace().remove(laser.getControl());
        laser.removeFromParent();
        continue;
      }
      laser.move(laser.getDirection().mult(laser.getSpeed()));
    }

    scoreText.setText("Score: " + score);
  }
  private static void processTriangleData(
      Mesh mesh, List<VertexData> vertices, boolean approxTangent, boolean splitMirrored) {
    ArrayList<VertexInfo> vertexMap = linkVertices(mesh, splitMirrored);

    FloatBuffer tangents = BufferUtils.createFloatBuffer(vertices.size() * 4);

    ColorRGBA[] cols = null;
    if (debug) {
      cols = new ColorRGBA[vertices.size()];
    }

    Vector3f tangent = new Vector3f();
    Vector3f binormal = new Vector3f();
    // Vector3f normal = new Vector3f();
    Vector3f givenNormal = new Vector3f();

    Vector3f tangentUnit = new Vector3f();
    Vector3f binormalUnit = new Vector3f();

    for (int k = 0; k < vertexMap.size(); k++) {
      float wCoord = -1;

      VertexInfo vertexInfo = vertexMap.get(k);

      givenNormal.set(vertexInfo.normal);
      givenNormal.normalizeLocal();

      TriangleData firstTriangle = vertices.get(vertexInfo.indices.get(0)).triangles.get(0);

      // check tangent and binormal consistency
      tangent.set(firstTriangle.tangent);
      tangent.normalizeLocal();
      binormal.set(firstTriangle.binormal);
      binormal.normalizeLocal();

      for (int i : vertexInfo.indices) {
        ArrayList<TriangleData> triangles = vertices.get(i).triangles;

        for (int j = 0; j < triangles.size(); j++) {
          TriangleData triangleData = triangles.get(j);

          tangentUnit.set(triangleData.tangent);
          tangentUnit.normalizeLocal();
          if (tangent.dot(tangentUnit) < toleranceDot) {
            // log.log(Level.WARNING,
            // "Angle between tangents exceeds tolerance "
            // + "for vertex {0}.", i);
            break;
          }

          if (!approxTangent) {
            binormalUnit.set(triangleData.binormal);
            binormalUnit.normalizeLocal();
            if (binormal.dot(binormalUnit) < toleranceDot) {
              // log.log(Level.WARNING,
              // "Angle between binormals exceeds tolerance "
              // + "for vertex {0}.", i);
              break;
            }
          }
        }
      }

      // find average tangent
      tangent.set(0, 0, 0);
      binormal.set(0, 0, 0);

      int triangleCount = 0;
      for (int i : vertexInfo.indices) {
        ArrayList<TriangleData> triangles = vertices.get(i).triangles;
        triangleCount += triangles.size();
        if (debug) {
          cols[i] = ColorRGBA.White;
        }

        for (int j = 0; j < triangles.size(); j++) {
          TriangleData triangleData = triangles.get(j);
          tangent.addLocal(triangleData.tangent);
          binormal.addLocal(triangleData.binormal);
        }
      }

      int blameVertex = vertexInfo.indices.get(0);

      if (tangent.length() < ZERO_TOLERANCE) {
        log.log(Level.WARNING, "Shared tangent is zero for vertex {0}.", blameVertex);
        // attempt to fix from binormal
        if (binormal.length() >= ZERO_TOLERANCE) {
          binormal.cross(givenNormal, tangent);
          tangent.normalizeLocal();
        } // if all fails use the tangent from the first triangle
        else {
          tangent.set(firstTriangle.tangent);
        }
      } else {
        tangent.divideLocal(triangleCount);
      }

      tangentUnit.set(tangent);
      tangentUnit.normalizeLocal();
      if (Math.abs(Math.abs(tangentUnit.dot(givenNormal)) - 1) < ZERO_TOLERANCE) {
        log.log(Level.WARNING, "Normal and tangent are parallel for vertex {0}.", blameVertex);
      }

      if (!approxTangent) {
        if (binormal.length() < ZERO_TOLERANCE) {
          log.log(Level.WARNING, "Shared binormal is zero for vertex {0}.", blameVertex);
          // attempt to fix from tangent
          if (tangent.length() >= ZERO_TOLERANCE) {
            givenNormal.cross(tangent, binormal);
            binormal.normalizeLocal();
          } // if all fails use the binormal from the first triangle
          else {
            binormal.set(firstTriangle.binormal);
          }
        } else {
          binormal.divideLocal(triangleCount);
        }

        binormalUnit.set(binormal);
        binormalUnit.normalizeLocal();
        if (Math.abs(Math.abs(binormalUnit.dot(givenNormal)) - 1) < ZERO_TOLERANCE) {
          log.log(Level.WARNING, "Normal and binormal are parallel for vertex {0}.", blameVertex);
        }

        if (Math.abs(Math.abs(binormalUnit.dot(tangentUnit)) - 1) < ZERO_TOLERANCE) {
          log.log(Level.WARNING, "Tangent and binormal are parallel for vertex {0}.", blameVertex);
        }
      }

      Vector3f finalTangent = new Vector3f();
      Vector3f tmp = new Vector3f();
      for (int i : vertexInfo.indices) {
        if (approxTangent) {
          // Gram-Schmidt orthogonalize
          finalTangent
              .set(tangent)
              .subtractLocal(tmp.set(givenNormal).multLocal(givenNormal.dot(tangent)));
          finalTangent.normalizeLocal();

          wCoord = tmp.set(givenNormal).crossLocal(tangent).dot(binormal) < 0f ? -1f : 1f;

          tangents.put((i * 4), finalTangent.x);
          tangents.put((i * 4) + 1, finalTangent.y);
          tangents.put((i * 4) + 2, finalTangent.z);
          tangents.put((i * 4) + 3, wCoord);
        } else {
          tangents.put((i * 4), tangent.x);
          tangents.put((i * 4) + 1, tangent.y);
          tangents.put((i * 4) + 2, tangent.z);
          tangents.put((i * 4) + 3, wCoord);

          // setInBuffer(binormal, binormals, i);
        }
      }
    }
    tangents.limit(tangents.capacity());
    // If the model already had a tangent buffer, replace it with the regenerated one
    mesh.clearBuffer(Type.Tangent);
    mesh.setBuffer(Type.Tangent, 4, tangents);

    if (mesh.isAnimated()) {
      mesh.clearBuffer(Type.BindPoseNormal);
      mesh.clearBuffer(Type.BindPosePosition);
      mesh.clearBuffer(Type.BindPoseTangent);
      mesh.generateBindPose(true);
    }

    if (debug) {
      writeColorBuffer(vertices, cols, mesh);
    }
    mesh.updateBound();
    mesh.updateCounts();
  }