Week 10 Particle Systems and Beginning of Collisions

Unfortunately, Chapter 4, namely, Particle Systems will require another week to finish. That’s not so bad since after chapter 4, there are only 2 more chapters to finish this semester’s main learning curriculum. There are a few sections of Chapter 4 on inheritance and polymorphism that I did not get to cover because I ran out of time.

The sections I covered of Chapter 4 discussed the idea of not just having one particle class but rather having multiple classes communicating with one another to handle systems of systems of particles. The other topic covered was coding in lifespan for each particle so that the program did not bottleneck from continuing to draw all the particles emitted. The author introduced the arraylist class, though I had already been using it since Week 5 when I decided to jumpstart on composing particle systems. I was introduced to this more convenient form of an array inĀ  brief look I had on Chapter 23 of “Learning Processing”. For particle systems, it is extremely convenient since it allows straightforward resizing of the array by adding and removing items. The regular array can also be resized but it is not as easy as just “arrayname.add” or “arrayname.remove”. As my weekly composition, I decided to do one of the exercises that required me to make a moving emitter. The sketch can be seen here.

I spent most of the week trying to tackle the problem of collision, something Prof. Ponto suggested. It actually turns out to be a little bit more complicated than I thought (lol). Prof. Schiffman actually dedicates an entire chapter, the next chapter I am supposed to cover, on the use of an external physics engine built for Java called Box2D for collisions. But he also says it is possible to tackle simple collisions without the need of an additional class library like Box2D. Anyways I feel I am getting close but I still don’t have a working composition for the collisions of multiple particles. But here’s what I got so far.

Okay so the basic pseudo algorithm we are looking to code is as follows:

– Have Particle objects stored in arraylist
– Objects store location, velocity and size
– Objects check for collision with border and collision with each of the other particles
– Each object will loop through the array, checking if any of the particles are colliding with it.
– The object will bounce off the other objects depending on size and velocity
The part I focused on this week was detailing the class of Particles and writing functions for the constraints with window border and for the bounce.
The writing of the bounce function was probably the trickiest part for me but I did some research on the internets and found this really helpful animation of how each vector is playing out in the collision of two objects. Essentially, the bounce function will determine the velocities of each object after they collide by taking the velocity vector of one of the objects and mapping it to a local coordinate system oriented abt the direction of one particle center to the other particle center. So for the bounce function work we need another function that extracts the component vectors:
Bounce Function

Bounce Function

Now I just need to come up with the actual collision function which checks if two objects are colliding and applies the bounce function to give each a different velocity in different directions. I will complete this by next week and finish Particle Systems.

See you soon.