A few weeks back we had a workshop in Processing learning about object orientated programming (OOP) as Java is an object orientated language and what Processing is based on.
Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions” and data rather than logic.
With this type of programming we can create relationships between objects making them inherit characteristics from each other to create interactions.
The objective of the workshop was to be a basic introduction to OOP by creating agents which simulate brownian motion. The movement of particles (random) suspended in a fluid is called Brownian motion, the particles are seen to wiggle about due to them being hit by liquid molecules.
We started by creating a class for our agent which is going to wiggle about on the screen. A class is made up of 3 components, the class data, constructor and it’s methods. The class data us unique to each agent created b the class, the constructor is used to create this instance and the methods are shared by each instance of the class and provide an interface to interact with an object’s data.
The method update() is what is simulating the brownian motion, a random value between -1 & 1 is added onto the x and y coordinates which is constantly being updated making the agent wiggle around a little.
The class is then used in an array list; an array that has a flexible number of data slots and can constantly be added to. The sketch uses are for loop to iterate through each instance of the agent and apply its methods, updating its location. A new agent is added to the array when the mouse is clicked.
Here is a .gif of the agents having a wiggle party together, showing their simulated brownian motion. While this example is relatively simple, its a good starting point to get to understand classes and how to implement them. From here we can add more behaviours and interactions between the objects to develop the complexity of the idea.