Modelling entities 2
Projectile physics
Last updated
Projectile physics
Last updated
The simulation controller is just a wrapper of the resetting and manual update logic discussed during the development of the ticking clock model. It packages the scripts for loading and storing data into the “notes” panel from the CAD document and the packing and unpacking serialization logic.
The controller unit is split into two clusters because this is the point where data flow should have “closed the loop”. Breaking the circular dependency requires us to load the current model state, perform calculations and finally store it such that it can be retrieved in the upcoming clock iteration.
The physics logic takes the particles through a series of transformations which result into the calculation of new states propagated forward. Force analysis requires us to imagine that time is frozen and our aim is to determine all the forces acting on our particles. State integration looks at how to compute the future from the present state of the system. Finally, constraints help us to define boundaries and interactions even out of the physics box.
Eventually we compute the sum of forces for each particle and emit a list of force vectors in Newtons. In later iterations we will model more complex forces which relate particles with one another. For now there are no interactions as particles are oblivious to one another’s existence.
Practice
We know that acceleration is a = dv/dt
i.e. the change of velocity over time. Which we can write as a = ( vnew – vold ) / dt
. With a bit of rearrangement we obtain the form vnew = vold + a * dt
. The quantity “dt” in theory is infinitely small but just as we ignored limits in computing the tangent on a curve, here we also just pick a small enough number i.e. “0.05s”.
Having computed the new velocity we proceed with updating the particle’s position using the relationship v = dp / dt
. In exactly the same way we compute a discrete version of this as pnew = pold + v * dt
. We have thus constructed new positions and velocities for each particle. Masses in this scenario remain constant and passed along to the output unmodified.
Practice
Experiment with different values for the time step “dt” value and explain its influence to the trajectory of particles. What happens when the value it too large?
In the integrator example we updated the position of a particle using its velocity from the time step ahead. Try to connect the current velocity and observe what is the results the explicit Euler integration method.
The final piece of the puzzle is the constraint enforcement logic block. This is totally optional and in a sense outside the realistic modelling of physics. As an introduction to the subject we will discuss the scenario of implementing a ground plane such that particles can bounce off instead of falling perpetually. Let’s build it step by step.
For each particle, we will decompose its position and check if the “z” coordinate is negative ie. it has fallen below the world XY-plane. We convert the “true/false” result of the conditional to “1/0” by passing it through the “integer” component.
We multiply the result with the “z” coordinate and reassemble the particle’s position. Therefore, if the particle has fallen below the ground we will force its “z” coordinate to be on the ground i.e. znew= 0 * zold
otherwise we just let it be znew = 1 * zold
.
If this style of branchless adjustment of the “z” coordinate is confusing, you may use instead “if” expressions e.g. if( z < 0, 0, z )
or in python 0 if( z < 0 ) else z
. Avoiding “if” expressions by using only arithmetic is just generally computationally faster.
The result of this simple ground constraint is a bit whimsical as seen below. Once the particles hit the ground they stay on it and slide away. To achieve a bouncing type of behaviour we need to act not only on their position but also their velocity.
If we detect that a particle has fallen below the ground we will both force it back to the ground i.e. set “z” coordinate to zero and also reverse the “z” component of its velocity. In this sense we have developed a simple model for collision response.
The expression component is used here to convert between the “1/0” number to a sign multiplier “+/-1” for the “z” velocity component using “2 * bit – 1”. Therefore, if the particle has fallen below the ground we will force its velocity “z” component to flip i.e. znew= -1 * zold
otherwise we just let it be znew = 1 * zold
.
Applying constraints in this fashion in known as applying “penalties” because we perform actions outside the force analysis and integration framework. As a result the outcomes are not necessarily faithful to the laws of physics. For example you will notice that we forced particles back on the ground somewhat miraculously i.e. we conducted actual work or equivalently we added energy to the system.
Practice
Instead of setting the particle’s “z” position to zero, we can use the same inversion logic applied for the velocity “z” component. Explain why this is approach may be more sensible geometrically and in the physics sense.
Create a bounding rectangle such that particles bounce also against vertical walls at “x = -1” and “x = 1”. Use clusters to avoid copy/pasting the same logic.
Create a constraint that sets the particle’s mass to zero at the point of impact on the ground. Setting the mass to zero will immobilize the particle because the acceleration is computed using the “zero safe” division.
The projectile setup requires only two forces: gravity and drag. Gravity is expressed using the simplified form of earth’s gravity F = mg
[]. The acceleration of gravity is constructed as scaled version of the Z-direction by the standard value “9.8 m/s2“. We use the negative direction just such that projectiles fall in the -Z sense.
Drag is a force resisting a particles motion. Again we use the simplified low Reynolds number form F = -kv
[], where “k” is an abstract coefficient capturing something about both the body’s material, geometry as well as the medium in which the body travels, and the “v” is the current object’s velocity.
The use of drag is for letting particles come into a stop eventually instead of moving perpetually without frictions as per Newton’s first law of motion []. Drag, as well as gravity, can be disabled by setting the scaling coefficients to zero.
A projectile’s trajectory with an initial velocity under only gravity is computable as a parabola using analytical methods []. Try to draw the trajectories using such approach and compare the results with the simulation.
Under no drag we shall expect to retain the same kinetic energy over time for each particle and the systems in total. You can compute the kinetic energy [] using the dot product of particle’s velocities times half of their masses. Observe how those quantities change or not over time.
You may also compute the potential energy of particles [] and graph it along the kinetic energy in the XY-plane.
The goal of this section is to compute the new positions and velocities of all particles given the existing values, under the influence of the forces computed. To achieve this we use Newton’s second law of motion F = ma
[], where “F” is the sum of forces acting of a body, “m” is its mass and “a” is its acceleration. Since we know the forces per particle, we can compute their accelerations by dividing with masses “a = F / m”.
There are a lot subtle aspects about this interpretation of Newton’s law as they are implemented here. The process overall is known as integration, in the mathematics sense, because we are summing infinitely small changes. The particular version is known as the semi-implicit or symplectic Euler integration []. It is reasonably fast and stable making it popular from video games to even scientific computing.
Particles bouncing away towards one end of the viewport is a bit annoying. You can use modular arithmetic to force particles to re-enter a virtual screen rectangle in the same fashion pac-man [] exits from the left and enters from the right side.