Published on
6 min read

How to Create a Tornado Effect

The Geometry of Sine and Cosine
Authors
  • avatar
    Name
    John Moscarillo
    Twitter

How to Create a Tornado Effect: The Geometry of Sine and Cosine

Creating visually stunning effects in computer graphics often involves leveraging mathematical concepts. One particularly mesmerizing effect is the tornado, which can be achieved by cleverly using the geometric properties of sine and cosine functions. In this article, we'll explore how to create a tornado effect and dive into the mathematical principles behind it.

To see it in action, check out this Fun.

Understanding Sine and Cosine

Before we delve into the tornado effect, let's briefly review sine and cosine. These trigonometric functions are fundamental in describing circular and periodic motion:

  • Sine (sin) represents the vertical component of a point on a circle.
  • Cosine (cos) represents the horizontal component of a point on a circle.

When used together, sine and cosine can create circular or spiral motions, which are key to our tornado effect.

Trigonometric Functions

The image shows a graph of the trigonometric functions sine and cosine and there relation to x and y values. We'll see in the code how the x and y are calculated.

Creating the Tornado Effect with Particles

The tornado effect is created using a particle system. Each particle's motion is controlled using sine and cosine functions to achieve the spiraling effect. Let's break down the key components:

Particle Class

The Particle class is the core of our tornado effect. Here's a simplified version of the class:

class Particle {
  constructor(canvas, context, position, startPosition) {
    this.startPosition = startPosition;
    this.position = position;
    this.currAngle = 0;
    this.currRadius = initialRadius;
    this.rSpeed = this.getRandomInt(particleRotationSpeedMin, particleRotationSpeedMax);
    this.vSpeed = this.getRandomInt(particleVerticalSpeedMin, particleVerticalSpeedMax);
  }

  update() {
    this.currRadius += this.vSpeed / 4;
    this.currAngle += this.rSpeed / 2;

    if (mouseDown) {
      this.position.y = this.startPosition.y + Math.cos(this.currAngle) * this.currRadius;
      this.position.x = this.startPosition.x + Math.sin(this.currAngle) * this.currRadius;
    } else {
      this.position.y -= this.vSpeed;
      this.position.x = this.startPosition.x + Math.cos(this.currAngle) * this.currRadius;
    }
    this.position.z = centerZupdate + centerZ + Math.sin(this.currAngle) * this.currRadius;

    // ... (code for 3D projection)
  }
}

Using Sine and Cosine for Particle Movement

The key to creating the tornado effect lies in how we update the particle positions:

  1. Circular Motion: We use Math.cos(this.currAngle) and Math.sin(this.currAngle) to create circular motion in the x-z plane.

  2. Spiral Effect: By gradually increasing this.currRadius, we create an expanding spiral.

  3. Vertical Movement: We subtract this.vSpeed from the y-position to make particles rise, creating the vertical component of the tornado.

  4. 3D Effect: By applying sine to the z-position as well, we create a 3D spiral effect.

Customizing the Tornado

The effect can be customized through various parameters:

let particleCount = 800;
let particleRadiusMin = 2;
let particleRadiusMax = 12;
let initialRadius = 4;
let particleVerticalSpeedMin = 0.2;
let particleVerticalSpeedMax = 0.9;
let particleRotationSpeedMin = 0.04;
let particleRotationSpeedMax = 0.06;

These parameters control the number of particles, their size, initial radius of the tornado, and the speed of vertical and rotational movement.

Color and Appearance

The tornado's appearance is further enhanced by color choices:

let particleWhite = ['#FFF', '#FFF', '#FFF', '#FFF', '#FFF', '#FFF'];
let particleBW = [
  '#FF4500',
  '#8B4513',
  '#1C2951',
  '#CD5C5C',
  '#fff',
  '#2F4F4F',
  // ... (more colors)
];

The particleBW array provides a range of colors that create a more realistic and ominous tornado effect, with colors representing sky glow, rubble, shadows, and building silhouettes.

Interactivity

The code includes interactivity through mouse input:

if (mouseDown) {
  this.position.y = this.startPosition.y + Math.cos(this.currAngle) * this.currRadius;
  this.position.x = this.startPosition.x + Math.sin(this.currAngle) * this.currRadius;
} else {
  this.position.y -= this.vSpeed;
  this.position.x = this.startPosition.x + Math.cos(this.currAngle) * this.currRadius;
}

When the mouse is pressed, the particles move in a perfect circular pattern. When released, they resume their upward spiral motion.

Conclusion

Creating a tornado effect using particle systems and trigonometric functions is a fascinating blend of mathematics and computer graphics. By manipulating sine and cosine functions, we can create complex, natural-looking phenomena that respond to user input.

The key takeaways are:

  1. Use sine and cosine to create circular motion
  2. Combine this with vertical movement for a spiral effect
  3. Apply 3D transformations for depth
  4. Use a particle system for a more dynamic and realistic effect
  5. Customize colors and parameters for different visual styles

Experimenting with these concepts can lead to a wide variety of impressive visual effects beyond just tornadoes. Happy coding, and may your graphics be ever more dynamic and engaging!