Computer Graphics and Flocking Simulation, by PCK

Version: [], ID:[]

I always enjoy coding, and some of my favorite projects are creating computer graphics (CG) simulations.
I created this page to show every big steps that I have taken in order to create such simulations.
Ultimately, the simulations that I created will resemble a flock of living things, such as fish, birds, or even people.

Position

When simulating an object in a computer graphics, the first thing it needs is the position.
Since we view the result on a screen, the very basic representation of an object is usually a 2D point.
A 2D point is a pair of numbers, namely the x and y coordinates.
Below is a simple example showing some points in a rectangular 2D space, which we call the canvas.

Velocity (or Speed)

Showing just a point or a square is boring, next we want it to move.
When moving an object, we are changing its position over time.
The rate of change of the position is called the velocity.
In your Maths class, you might have come across the topic of calculating speed of moving objects, right?
Velocity is the same as speed.
But more, if we are to move an object in 2D, we also need to specify the direction of the movement.
That direction, together with the velocity, is called the velocity vector.
Below is an example showing an object moving with a velocity vector in the canvas.

Position: (x, y) = (, ) Velocity: (x, y) = (, )

One extra thing to note is that, when we move an object, we need to check for the case when it hits the edges of the canvas.
Such checking is called collision detection.
For my moving object above, if I don't check for collision, the object will fly out of the canvas, and we will not be able to see it anymore.
So, my instruction is to have the object's velocity to reflect, just like how a ball bounces off a wall.
You can apply different boucing rule to the moving object, for example, you can have the object to bounce off the edge of the canvas with a different angle, or you can have the object to bounce off the edge of the canvas with a different speed.
For my example, I have the object to bounce at the same but opposite angle, like how an object looks at an angle from a mirror.
You can also see I added a readout of the current position and velocity of the object, so you can see how it is moving, and how its velocity changes when it collides with the edges.

Steering, Acceleration and Obstacle Avoidance

Do you notice that the object in the above example, always move in a straight line with no change in speed?
In real life, objects don't just start moving with a constant speed, it speeds up from rest to a certain speed, and then it slows down to a stop.
Also, if it were to change direction, it would not just change instantly, the change would be gradual.
To simulate this, we introduce the concept of steering, which is a force that is applied to the object in order to change its velocity.
In other words, steering change the velocity, and velocity change the position.

The example below shows a square chasing towards some random points.
You can see that the chasing square has 2 arrows.
The bigger arrow is the velocity vector, it is pointing towards the direction where the square is moving.
The smaller one is the steering vector, it is affecting the change of the velocity vector.
For example, if the steering vector is opposite to the velocity vector, then the square will appear to be slowing down, like how a car brakes
If the steering vector is at an angle to the velocity vector, then the square will appear to be making a turn
The steering vector and the velocity vector together makes the movement much smoother, and more realistic.

(Bonus notes for a high school student or a university student who has studied calculus:
The position, velocity and steering (acceleration) are one good example to understand the concept of derivatives and integrals.
This lets you visualize how integrating acceleration (the 2nd derivative) with respect to time gives velocity (the 1st derivative), and how integrating velocity (the 1st derivative) with respect to time gives position (the 0th derivative).
I find it fascinating to see how this all plays out with such a simluation with animations.)

The canvas below added more tricks to the chasing square, as there are some obstacles that the chasing square needs to avoid.
If one of the obstacles is in between the chasing square and the target point, the chasing square might bump into it.
As you learned in the previous examples, this is called collision.
My program has to check to see if the chasing square is colliding with any of the obstacles.
Such checking is called collision detection.
If the chasing square is colliding, or getting too close to colliding, it will steer away.
You can see how the steering is changing from time to time, as the chasing square needs to: (1) steer away from the obstacle, and (2) steer towards the target point.

---