WEEK 5 DAY 2
Autonomous agents, swarms, physics simulations
HOW TO CREATE MULTIPLE OBJECTS INTERACTING WITH EACH OTHER?
Cellular automata: Conway's Game of Life
(ALSO USING VECTORS FOR MORE COMPLEX SIMULATIONS)
Pixel sorting, filters, glitch, image detection
HOW TO USE IMAGES AND VIDEO AS A SOURCE MATERIAL?
A FUNCTION REPEATING ITSELF
MAKING GRAPHICS FASTER WITH SHADERS
CREATING 3D ENVIRONMENTS
HOW TO MAKE YOUR P5 SKETCH COMMUNICATE WITH CONTROLLERS, SENSORS AND OTHER PROGRAMS?
MEMORY
Storing data and accessing it later
Variables, arrays, objects
SEQUENCE
Running instructions in order
Functions, algorithms
SELECTION
Making choices
Conditionals and logic (if, else, and, or, not)
REPETITION
Doing the same thing more than once
Loops (for, while)
PROBLEM:
HOW TO STORE AND ACCESS VARIOUS PIECES OF INFORMATION?
HOW TO ALLOW MULTIPLE ELEMENTS TO INTERACT WITH EACH OTHER?
Object-oriented programming is a fundamental programming paradigm which most popular programming languages use
In OOP, code is organised into objects that interact with each other
An object is a data structure that encapsulates both data and functionality
Data: Properties ("what it is")
Functionality: Methods ("what it can do")
PROPERTIES: location, size, colour
METHODS:
move, bounce
CLASS
OBJECT
OBJECT
JavaScript was not originally designed to be a strong OOP language
Traditionally in JS, objects are created as object literals and they inherit their features from prototypes
Classes were added to JS as a feature in 2015
CLASS
OBJECT
OBJECT
OBJECT
CLASS INHERITANCE
PROTOTYPE INHERITANCE
A JS object is a variable that can store multiple values as properties
An object literal is declared using const (or let)
const myPizza = {
name: "Margherita",
toppings: ["mozzarella", "tomato", "basil"],
price: 18.90
}
myPizza.price //returns 18.90
A JS class is an abstracted template for creating multiple objects with same properties and methods
eg. Ball class defines how all balls look and behave
Class is defined outside other function blocks
Class name starts with a capital letter
Class methods and properties go inside curly brackets
All classes require a constructor method
Inside the class definition, the object properties are defined with keyword this
class Ball {
constructor(){
this.x = 250;
this.y = 250;
this.r = 100;
}
move() {...}
bounce() {...}
}
The constructor method is executed when a new object instance is created
Constructor usually defines the objects's properties and sets initial values for them
Acts like setup() for classes
class Ball {
constructor(){
this.x = 250;
this.y = 250;
this.r = 100;
}
move() {...}
bounce() {...}
}
let myBall = new Ball();
//constructor is now executed and myBall is an object: {x:250, y:250, r:100}
ellipse(myBall.x,myBall.y,myBall.r);
myBall.move();
myBall.bounce();
New object instance is created using keyword new
let myBall = new Ball(mouseX,mouseY);
//myBall is an object: {x:mouseX, y:mouseY, r:100}
class Ball {
constructor(x,y){
this.x = x;
this.y = y;
this.r = 100;
}
move() {...}
bounce() {...}
}
noise(x, [y], [z])