MTAT.03.305 Computer Graphics Seminar
Natural simulation with JavaScript using Processing.js
Yaroslava MALASH
Tartu, Estonia 2015
using Processing.js Yaroslava MALASH Tartu, Estonia 2015 Outline - - PowerPoint PPT Presentation
MTAT.03.305 Computer Graphics Seminar Natural simulation with JavaScript using Processing.js Yaroslava MALASH Tartu, Estonia 2015 Outline Introduction Randomness Noise Vectors Oscillation Angular movements Particle systems References and
MTAT.03.305 Computer Graphics Seminar
Yaroslava MALASH
Tartu, Estonia 2015
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
http://processingjs.org/articles/jsQuickStart.html#whatisprocessing Yaroslava Malash
More http://en.wikipedia.org/wiki/Applications_of_randomness
Yaroslava Malash
We add a methods in JavaScript by attaching them to the object prototype
Yaroslava Malash
http://processingjs.org/reference/floor_/ Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
“Perlin noise” – an algorithm named for its inventor Ken Perlin
Perlin noise can be used to generate various effects with natural qualities, such as clouds, landscapes, and patterned textures like marble Perlin noise is a random sequence generator producing a more natural ordered, harmonic succession of numbers compared to the standard random() function.
Yaroslava Malash
ProcessingJS has a built-in implementation of the Perlin noise algorithm: the function noise(). Perlin noise is defined in an infinite n-dimensional space where each pair of coordinates corresponds to a fixed semi-random value (fixed only for the lifespan of the program). Processing can compute 1D, 2D and 3D noise, depending on the number of coordinates given.
http://processingjs.org/reference/noise_/ Yaroslava Malash
A noise() expects to pass in an argument that signifies a "moment in time," and always returns a value between 0 and 1 will return 0.490 at time equals 0.03
Yaroslava Malash
Yaroslava Malash
The map() function takes five arguments:
draw a rectangle with a width between 0 and the current width.
Live Example: https://www.khanacademy.org/computer-programming/noisy- rectangle/6454127177498624
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
For every concept in this world (wind, location, acceleration) we’ll need two
zspeed, etc. Wouldn’t it be nice if we could simplify our code and use fewer variables?
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Each vector has two components, an x and a y. To add two vectors together, we simply add both x’s and both y’s.
Live Example: https://www.khanacademy.org/computer-programming/bouncing-ball- with-pvector/6734144616792064
Yaroslava Malash
1.A method called add() that takes another PVector object as its argument.
Newton’s First Law
Yaroslava Malash
Newton’s Second Law
Yaroslava Malash
Newton’s Third Law
Yaroslava Malash
Yaroslava Malash
“Force equals mass times acceleration” 1.Create an object Mover, which has location, velocity, and acceleration
Where Wind and Gravity are PVectors
Yaroslava Malash
What can be a problem? What is the value of acceleration when it is added to velocity? How are we going to handle more than one force?
Yaroslava Malash
Net Force equals mass times acceleration. Our implementation of this is through a process known as force accumulation. At any given moment, there might be 1, 2, 6, 12, or 303 forces. Modify a bit the applyForce() Next step, we have to make sure that we clear acceleration acceleration (i.e. set it to zero) before each time update() is called.
Yaroslava Malash
Acceleration, in our simulation, has no memory; it is simply calculated based
The easiest way to implement clearing the acceleration for each frame is to multiply the PVector by 0 at the end of update().
Yaroslava Malash
Dealing with Mass Mass is a scalar (float), not a vector, as it’s just one number describing the amount of matter in an object. Let’s add a mass with “1” to the Mover object. Where does mass come in? We use it while applying Newton’s second law to
div() - divides a vector by a scalar or divides one vector by another.
Yaroslava Malash
Creating the Force Let’s start with the idea of simulating wind. Live example: https://www.khanacademy.org/computer- programming/mover-with-force-accumulation/4525763346825216
Yaroslava Malash
A radian is a unit of measurement for angles defined by the ratio of the length
180 degrees = PI radians, 360 degrees = 2*PI radians, 90 degrees = PI/2 radians, etc.
Yaroslava Malash
Processing.JS implementation for radians: Example of rotation the shape by 60 degree
Yaroslava Malash
angel= angel+angular velocity angular velocity=angular velocity + angular acceleration An angle is a scalar quantity—a single number, not a vector!
Live example: https://www.khanacademy.org/computer-programming/spinning- baton/5179782058737664
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Whatever comes out of the sine function we multiply by amplitude. We know that sine will oscillate between -1 and 1. If we take that value and multiply it by amplitude then we’ll get the desired result: a value oscillating between -amplitude and amplitude.
Yaroslava Malash
TWO_PI*frameCount / Period
We know that sine will repeat every 2*PI radian 1. frameCount/Period – amount of cycle we’ve completed 2. Multiplying by TWO_PI – the result 3. TWO_PI – is the number of radians required for one sine to complete the cycle Live example: https://www.khanacademy.org/computer-programming/simple-harmonic- motion/4920589909229568
Yaroslava Malash
More examples: http://processingjs.org/exhibition/
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash
Yaroslava Malash