I decided to continue playing with forces this week before moving on to the next chapter. Two other types of force simulations that I hadn’t explored were fluid resistance and object to object attraction. I created two compositions which can be seen here and here.
Each composition contains two classes and there was an issue which led me to have to recode one function from one class into another for the simulation to work. With the algorithm having two classes that need to act together, there need to be functions in either one of the classes that reference an object of the other class.
I wanted to add a region to the sketch where the particles would experience a fluid resistance akin to falling into water or an air pocket. There are two key aspects of adding selective fluid resistance to our simulation. First, there needs to be a function that detects a “collision” with the region and returns a boolean, which would be used to activate a certain behavior. The behavior we are wanting to activate when a particle collides with our region is the initiation of a new force to be added. So secondly, there needs to be a function that defines the force to be experienced. When I was adding fluid resistance functionality to my particle string, I opted to add this functionality to the Particle class rather than to the Liquid class. But it didn’t work. If it did, we would expect the smaller particles (which are assigned a smaller mass), to experience greater attenuation of speed as they go down.
Anyways, as soon as I incorporated the functionality into the Liquid class, the region was producing a force on the particles as expected.
I am still unsure why my first attempt was not successful because technically everything should have worked. I was referencing the liquid class.
When coding for the attractor, it should technically be possible to produce a simulation with the particle class becoming attracted to the attractor rather than the attractor attracting the particles. The technical difference lies in which of the classes references the other. But since I didn’t have much luck with particles referencing in their class the object that would produce their forces, I went with the second scenario of having the force entity reference the particles.
This week I will cover Chapter 3: Oscillations and produce attractions between each particle.
See you soon.