Week 13 – Polymorphism and Inheritance… Particle Systems continued…

I haven’t gotten a chance to talk about Polymorphism and Inheritance from Chapter 4 yet so I wanted to make a post about them.

 

We have seen how coding particle systems becomes a lot more intuitive when done in an object – oriented way by creating classes and defining arrays of objects of those classes. In more complicated scenarios, we may want to have more than one type of particle or agent with specific functions associated with it. Even then, it is very likely that each class will still have a lot of the same properties; since they are particles, their constructors will all have location vectors, velocity vectors and acceleration vectors. Moreover, their functions for describing motion may be the same. So it would seem like a good idea if we could re use the code in some way. Inheritance allows us to do this very easily by extending the properties of another class. So a class that inherits another class becomes a subclass of that class:

class Cat extends Animal {
  Cat() {
    super();
  }
  void meow() {
    println("MEOW!");
  }
}

This means that the cat inherits all of the properties and functions of the Animal. Using super() makes sure that the same initialized properties of the Animal(height, weight) are given to the cat. Also any functions of the animal, for example, eat(), sleep() are also now applicable to the cat. In addition to those functions, the cat object can have its own functions.

So what becomes of a cat object when it is extending another class? In cases where we are declaring arrays or arraylists, code specifies the type of objects that array will store,

ArrayList<Cat> kitty = new ArrayList<Cat>();

Polymorphism makes it possible to store subclasses of a class together as if they were the same type. So it is now perfectly valid to say

ArrayList<Animal> kitty = new ArrayList<Cat>();

With particles systems, this enables us to store all types of particles into one array as long as all the subclasses extend the same Particle class.

 

For this week’s project, I wanted to make sure that I was able to do particle system branched out into a particle class and a particlesystem subclass. This is a good way to start getting into the habit of coding a particle type and then a particle controller. First I did two exercises out of the chapter.

The first exercise asked for a movable rocket. This was basically an extension of Week 10 sketch of having a particle system moving while releasing particles.

Asteroid Exercise

Asteroid Exercise

The second exercise wanted a geometrical form to divide into smaller pieces or shatter. This amounted to using our regular Particle class and then having our ParticleSystem class arrange them in a grid. I wasn’t able to center it in the middle so I think my arrangement was off. When you clicked on the canvas, the particles began their kinematic method and started moving as if shattering.

Shatter Exercise

Shatter Exercise

For this week’s script, I decided to do another particle system but this time incorporating oscillation kinematics, as covered in Chapter 3, to give the particles a more life-like appearance. The particle system was done following the style of using two classes, one for the particle and one for the particlesystem. You may see the script here. Originally I had planned to incorporate the collisions in this script but I ran out of time.