-
Notifications
You must be signed in to change notification settings - Fork 0
Handling Collisions
Due to the nature of a computer simulation, it is possible for the particles to overlap when they collide. If there were no separation, the final velocity of each particle may not be enough to separate them and they would collide with each other. If this is the case, the particles will accelerate in each other, as they will have opposing velocities but be overlapping on the wrong side.
To solve this, a loop goes through each overlapped particle and subtracts its velocity from its position before collisions are calculated. This will only happen if the particle does not get sent into a wall.
Solving 2D collisions given only the initial velocity vectors is difficult, so for simplicity the axis of collision is rotated to the x-axis. Meaning, the velocity in the direction of the collision must be determined.
If angle phi = the angle of the collision, and theta = the angle of the velocity of the respective particle:
Values for particle 2 can be solved by replacing all 1's with 2.
Using the rotated values the collision can be calculated along a 1D axis. The collision will be calculated along the x-axis, and the values along the y-axis will remain the same. Since this is an elastic collision, both kinetic energy and momentum are conserved. Thus we can use the below formula:
Values for particle 2 can be solved by replacing all 1's with 2.
To know the final x and y velocities with respect to the canvas, the calculated final velocities must be rotated back. To do this, the x and y components of the collision vectors must be added. For the x-direction, the x component of the x rotated collision and x component of the y rotated collision velocity will be added:
Note: pi/2 is added since the y component is at a 90-degree angle from phi and the x component.
The y-value can be found by adding the y component of the x rotated collision velocity, and the y component of the y rotated collision velocity. This is the equation: