initDirection method

  1. @protected
void initDirection(
  1. Particle p,
  2. double speed
)

Initializes a new direction for the provided Particle.

Implementation

@protected
void initDirection(Particle p, double speed) {
  double dirX = random.nextDouble() - 0.5;
  double dirY = random.nextDouble() - 0.5;
  double magSq = dirX * dirX + dirY * dirY;
  double mag = magSq <= 0 ? 1 : math.sqrt(magSq);

  p.dx = dirX / mag * speed;
  p.dy = dirY / mag * speed;
}