コード例 #1
0
 private static void start() {
   CollisionEffect.kill();
   level.reset();
   double xSpeed = CalcHelp.gaussianDouble(3.5, 1);
   double ySpeed = CalcHelp.randomDouble(-1, 1);
   level.getBall().setVelocity(new Vector2d(xSpeed, ySpeed));
   level.getBall().setLaunched(true);
 }
コード例 #2
0
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(1000, 700);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Collision Effects Test - press any key to start");
    frame.setVisible(true);
    frame.setFocusable(true);
    frame.addKeyListener(
        new KeyListener() {
          public void keyPressed(KeyEvent e) {
            start();
          }

          public void keyReleased(KeyEvent e) {}

          public void keyTyped(KeyEvent e) {}
        });
    frame.add(new CollisionEffectsTest());

    Ball ball = new Ball(200, 300, 3);
    ArrayList<Body> bodies = new ArrayList<Body>();
    bodies.add(new Body(500, 300, 150, Color.blue));
    level = new Level(ball, bodies, null, null, null, 5.0, 1.0);
    level.generateLevelData();
  }
コード例 #3
0
  /**
   * 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};
  }
コード例 #4
0
  /**
   * Starts the effect.
   *
   * @param currentLevel the level the effect is taking place on
   */
  public static void start(Level currentLevel) {
    started = true;
    CollisionEffect.currentLevel = currentLevel;
    Point2d shift = currentLevel.getShift();
    Ball ball = currentLevel.getBall();
    Body intersected = currentLevel.getIntersectingBody();
    particles = Particle.generateParticles(ball, intersected);
    shakeValues = new double[6];
    double speed = ball.getVelocity().magnitude();

    if (speed < .25) speed = .25;
    if (speed > 2.5) speed = 2.5;

    double shakeFactor = 3 * speed / currentLevel.getFollowFactor();
    if (currentLevel.getFollowFactor() == 0.0) shakeFactor = .25 * speed;
    // 1st value is multiplicative factor
    int sign1 = -1;
    if (ball.getCenter().x() + shift.x() < 0) {
      sign1 = 1;
    } else if (ball.getCenter().x() + shift.x() > 0) {
      sign1 = CalcHelp.randomSign();
    }
    shakeValues[0] = CalcHelp.randomDouble(35, 40) * shakeFactor * sign1;

    int sign2 = -1;
    if (ball.getCenter().y() + shift.y() < 0) {
      sign2 = 1;
    } else if (ball.getCenter().y() + shift.y() > 0) {
      sign2 = CalcHelp.randomSign();
    }
    shakeValues[3] = CalcHelp.randomDouble(35, 40) * shakeFactor * sign2;

    // 2nd value is sinusoidal factor
    shakeValues[1] = CalcHelp.randomDouble(45, 50);
    shakeValues[4] = CalcHelp.randomDouble(45, 50);

    // 3rd value is exponential factor
    shakeValues[2] = CalcHelp.randomDouble(-.0035, -.0045);
    shakeValues[5] = CalcHelp.randomDouble(-.0035, -.0045);

    startTime = System.currentTimeMillis();
    ball.setLaunched(false);
  }
コード例 #5
0
  @Override
  public void paintComponent(Graphics gr) {
    super.paintComponent(gr);
    Graphics2D g = (Graphics2D) gr;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    if (level != null) {

      try {
        Thread.sleep(10);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      level.updateLevel();

      double xS = level.getScreenXShift();
      double yS = level.getScreenYShift();

      if (level.timeToReset() && !CollisionEffect.started()) {
        CollisionEffect.start(level);
      }

      if (CollisionEffect.running()) {
        int[] vals = CollisionEffect.update();
        xS = vals[0];
        yS = vals[1];
      } else {
        CollisionEffect.kill();
      }

      level.draw((int) xS, (int) yS, g);
      if (CollisionEffect.running()) {
        CollisionEffect.draw(g);
      } else if (!CollisionEffect.running() && level.timeToReset()) {
        level.reset();
      } else {
        level.getBall().draw(xS, yS, g);
      }
      repaint();
    }
  }