@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
  }
Exemple #2
0
  private void boundary(Particle particle) {

    Vector positionInBoundary = particle.getPosition().clone();
    Vector velocityInBoundary = particle.getVelocity().clone();

    /*
    der lantern renderer verändert die übergebenen winkel, somit muss ich hier schauen,
    dass auch die auslenkung wieder ausgleiche.
     */

    if (positionInBoundary.x < -(BOUNDARY * 0.5f)) {
      positionInBoundary.x = -(BOUNDARY * 0.5f);
      velocityInBoundary.x *= -BUMP_DECELERATION; // reverse and damp
    }
    if (positionInBoundary.x > (BOUNDARY * 0.5f)) {
      positionInBoundary.x = (BOUNDARY * 0.5f);
      velocityInBoundary.x *= -BUMP_DECELERATION; // reverse and damp
    }

    if (positionInBoundary.y < -(BOUNDARY * 0.5f)) {
      positionInBoundary.y = -(BOUNDARY * 0.5f);
      velocityInBoundary.y *= -BUMP_DECELERATION; // reverse and damp
    }
    if (positionInBoundary.y > (BOUNDARY * 1.8)) {
      positionInBoundary.y = (BOUNDARY * 1.8f);
      velocityInBoundary.y *= -BUMP_DECELERATION; // reverse and damp
    }

    particle.setPosition(positionInBoundary);
    particle.setVelocity(velocityInBoundary);
  }
Exemple #3
0
  private void update(Particle particle) {

    float intervalInSecs = (float) intervalInMillis / 1000f; // e.g. 0.033

    // position
    Vector velocity = particle.getVelocity().clone();
    velocity.multiply(intervalInSecs);
    Vector newPosition = particle.getPosition();
    newPosition.add(velocity);
    particle.setPosition(newPosition);

    // velocity
    float dampPowTime = (float) Math.pow(particle.getMass(), intervalInSecs);
    Vector velocityMult = particle.getVelocity().clone();
    velocityMult.multiply(dampPowTime);

    Vector acceleration = particle.getAcceleration().clone();
    acceleration.multiply(intervalInSecs);

    velocityMult.add(acceleration);
    particle.setVelocity(velocityMult);
  }
Exemple #4
0
  private Particle createRandomizedParticle() {
    Particle rc = new Particle();

    float[] pos = rc.getPosition();
    pos[0] = (float) Math.random() * boxWidth;
    pos[1] = (float) Math.random() * boxHeight;

    float[] vel = rc.getVelocity();
    vel[0] = (float) Math.random() * 150 - 75;
    vel[1] = (float) Math.random() * 150 - 75;

    return rc;
  }
  /**
   * Updates the effect.
   *
   * @param g The Graphics component the particles are being drawn with.
   * @return the new screenX and screenY shift values
   */
  public static int[] update() {
    double screenXShift = currentLevel.getScreenXShift();
    double screenYShift = currentLevel.getScreenYShift();
    long elapsedTime = elapsedTime();
    screenXShift +=
        (shakeValues[0]
            * Math.sin(elapsedTime / shakeValues[1])
            * Math.exp(shakeValues[2] * elapsedTime));
    screenYShift +=
        (shakeValues[3]
            * Math.sin(elapsedTime / shakeValues[4])
            * Math.exp(shakeValues[5] * elapsedTime));

    for (int i = 0; i < particles.size(); i++) {
      Particle p = particles.get(i);

      for (Body bod : currentLevel.getBodies()) {
        if (bod.intersects(p)
            || bod.getCenter().distanceSquared(p.getCenter())
                    <= (bod.getRadius() * .5) * (bod.getRadius() * .5)
                && i != 0) {
          particles.remove(i);
          i--;
        }
      }
      for (Blockage blockage : currentLevel.getBlockages()) {
        if (blockage.intersects(p.getCenter()) && i != 0) {
          particles.remove(i);
          i--;
        }
      }
      for (GoalPost gp : currentLevel.getGoalPosts()) {
        if (p.intersects(gp) && i != 0) {
          particles.remove(i);
          i--;
        }
      }
      p.setVelocity(p.getVelocity().multiply(0.99));
      p.move();
    }
    xShift = (int) screenXShift;
    yShift = (int) screenYShift;
    return new int[] {xShift, yShift};
  }
  /** Update particle's velocity and position */
  public void update(Swarm swarm, Particle particle) {
    double position[] = particle.getPosition();
    double velocity[] = particle.getVelocity();
    double globalBestPosition[] = swarm.getBestPosition();
    double particleBestPosition[] = particle.getBestPosition();

    // Update velocity and position
    for (int i = 0; i < position.length; i++) {
      // Update position
      position[i] = position[i] + velocity[i];

      // Update velocity
      velocity[i] =
          swarm.getInertia() * velocity[i] // Inertia
              + Math.random()
                  * swarm.getParticleIncrement()
                  * (particleBestPosition[i] - position[i]) // Local best
              + Math.random()
                  * swarm.getGlobalIncrement()
                  * (globalBestPosition[i] - position[i]); // Global best
    }
  }