It's Quite Windy

๐Ÿ•“ Oct 25, 2019 ยท โ˜•8 min read

Foreword

One thing that always have had my mind, is the wind. It can rumble everything, alas it looks like nothing. What I mean, is, that you cannot see it, but you can feel it. You can even make it yourself, which is super awesome, I think.

So, today were a quite windy day, and I wanted to do some personal project, instead of work *kek*. So I remembered that I had a quite, quite old project, called PotentioWindy, which were super fun to make. Thus I decided to make a post/tutorial about it! I thought to myself, that it would fit good with the last post I make, as it was about how to add processing - or rather p5 - as a hugo shortcode. Link

That said, let's move!

PS. If you'd like to check the original code, go to here. It's controlled by a potentiometer on an arduino.

Result

This is the complete result.

Balls

First of all

We cannot make the above without the most important thing. Exactly, I'm talking about the balls. One cannot do survice without balls. Haha.

Sorry!

Okay, let's get going.

First of all, make a new function, this will have to own some properties.

Namely:

  1. a location: loc
  2. a velocity: vel
  3. a acceleration: acc
  4. a color: col
  5. a size: sz
  6. a mass: mass

The loc (1.), vel (2.) and acc (3.) are vectors, which is why they're initiated from the p5` createVector`.

The col (4.) is simply a color in the areas of the grays.

The sz (5.) is the size of our ball, and the mass (6.) is how hard it should be to move the ball, for this, I've decided to multiply its size with 1.5.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var Ball = function Ball() {
  this.loc = createVector(width / 2, height / 2);
  this.vel = createVector();
  this.acc = createVector();

  this.col = color(random(55, 200));

  this.sz = random(10, 25);
  this.mass = this.sz * 1.5;
}

Now, a ball without any movement is quite boring, so let's implement a movement function.

First of all, add the acceleration to the velocity of the ball. Next, limit it to a maximium of 1 and add that result to the location.

At last, you'd always remember to reset the acceleration.

1
2
3
4
5
6
7
Ball.prototype.move = function move() {
  this.vel.add(this.acc);
  this.vel.limit(1);
  this.loc.add(this.vel);

  this.acc.mult(0);
}

For now, the ball will always move with a constant acceleration of 0. That's no fun. Let's add some wind!

First, we divide the received wind with the mass of the ball. Next up, add the newly returned force to our acceleration. Easy, peasy, eh’?

1
2
3
4
Ball.prototype.addForce = function addForce(force) {
  var f = p5.Vector.div(force, this.mass);
  this.acc.add(f);
}

Now, nothing is fun, if we can't see what we're trying to move around along the wind.

Thus, let's add some contraints, to keep our balls in view.

1
2
3
4
Ball.prototype.keepInView = function keepInView() {
  this.loc.x = constrain(this.loc.x, 0, width);
  this.loc.y = constrain(this.loc.y, 0, height);
}

At last, we'll draw the ball. Simply by specifying the fill color, which is the ball's color. Then draw an ellipse.

1
2
3
4
Ball.prototype.draw = function draw() {
  fill(this.col);
  ellipse(this.loc.x, this.loc.y, this.sz, this.sz);
}

Phew, that was quite a mouthful. Let's move on, then!

Let the arrow poin the way

So, it's more fun, if we know which way the wind goes. I nkow we have some balls, but it's better to see live. This is where the arrow comes into play!

Let's add a arrow function.

Don't worry, I'll explain what it does in the next pieces of code.

1
2
3
4
5
6
7
8
var Arrow = function Arrow(w) {
  this.wind = w;

  var fake = this.wind.copy();
  fake.y *= -1;
  this.loc = p5.Vector.sub(fake, createVector());
  this.loc.normalize()
}

It's fun to see the arrow move, so let's add something to let it move against.

The setWind save a copy of our wind for later use.

1
2
3
Arrow.prototype.setWind = function setWind(w) {
  this.wind = w.copy();
}

Drawing the arrow is quite important, otherwise, why did we even add it?

First of all, we need to push our rotation-matrix - or whatever it's called ๐Ÿค - because we do not want to move everything back after having rotated and translated our arrow.

Next, we translate to the center of our canvas, and add the position of of our arrow. Then, let's fill the arrow with whiteness, rotate the arrow to look at the the way the wind blows.

Now add an ellipse and two triangles. The ellipse and the one triangle will be used as the base of our arrow, the last triangle will make it even more clear, which whay the wind goes.

Lastly pop our metrix, so it won't mess up anything in the future.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Arrow.prototype.draw = function draw() {
  push();
  translate(width / 2 + this.loc.x, height / 2 + this.loc.y);
  fill(255);
  rotate(atan2(this.loc.x, this.loc.y));
  ellipse(this.loc.x, this.loc.y, 10, 10)
  triangle(
    this.loc.x + 5, this.loc.y + 0,
    this.loc.x - 5, this.loc.y + 0,
    this.loc.x + 0, this.loc.y - 10
  );
  triangle(
    0, -width / 2 + width / 64,
    10, -width / 2 + width / 64,
    5, -width / 2
  );
  pop();

Now, I did promise to explain this, as it's the almost the same as in the constructor.

First, we copy our wind, so no one messes with it. Negate the y-axis, and normalize the wind. Then, we figure what to lerp the wind to.

1
2
3
4
5
6
7
  var fake = this.wind.copy();
  fake.y *= -1;
  fake.normalize();

  var to = p5.Vector.sub(fake, createVector());
  this.loc.lerp(to, 0.02);
}

The wind that blows

Now, we're almost ready for the last part. Alas not yet ๐Ÿ˜ซ

I promised you some wind, so why not add it?!

That's exactly what we're doing now.

First of all, let's construct this beautiful and extreme force.

1
2
3
4
var Wind = function Wind() {
  this.wind = null;
  this.update(0);  
}

To update the wind, might be the utmost important task, and it's quite easy! Simply calculate the angle of the input parameter.

1
2
3
Wind.prototype.update = function update(a) {
  this.wind = p5.Vector.fromAngle(a);
}

Lastly it'd be neat if we'd get the wind, we are currently on, right?

1
2
3
Wind.prototype.get = function get() {
  return this.wind;
}

The drawing setup

First of all, we need a couple of variables. An array of balls - we'll have two in ours - and the wind.

1
2
var balls = [];
var wind = null;

To setup everything, we create a canvas as explained in the previous post.

Add a few balls. Contruct a new wind and an arrow.

Then, we want no strokes of anything to be drawn.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function setup() {
  const c = createCanvas(685, 685);
  c.parent("sample");

  balls = new Array(2).fill(0).map(() => new Ball());
  wind = new Wind()
  arrow = new Arrow(wind.get());

  noStroke();
}

To draw it all, we give a background of rgba(25, 25, 25, 1).

The wind needs to be updated at some point, otherwise it'll simply blow in the same direction forever. So, I decided to do it at every 100th frame, you can, of course, do it whenever you want to. For this, we fetch a random number in between 0 and 1. As a float, decimal or double. Whatever suits our tie. ๐Ÿค—

Nextup, we do, for each ball, an add of the wind, moving the ball, make sure it's still in view. Lastly we draw it.

At the absolute last point in this code, we draw the arrow on top of everything. And boom! We're done!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
function draw() {
    background(25);

    if (frameCount % 100 === 0) {
        var windDir = random(0.0, 1.0) * 2 * Math.PI;
        wind.update(windDir);
        arrow.setWind(wind.get());
    }

    balls.forEach((b) => {
      b.addForce(wind.get());
      b.move();
      b.keepInView();
      b.draw();
    })

    arrow.draw();
}

You can see the complete code here.

- Thanks,

Mads Cordes


Mads Cordes
WRITTEN BY
Mads Cordes
Dev of Sorts