The repository contains implementations of the mathematical apparatus and visualization of the absolutely elastic collision of balls with the exchange of kinetic energy in two-dimensional space.
pip install -r requirements.txt
- install all needed dependenciespython run.py
- run visualization and enjoy
├── src
│ ├── ball.py
│ ├── config.py
│ ├── field.py
│ └── ui.py
└── run.py
ball.py
- ball modelconfig.py
- configuration modulefield.py
- field model with ballsui.py
- module with visualizationsrun.py
- point of entry
example1.mp4
example2.mp4
example3.mp4
example4.mp4
WIDTH
andHEIGHT
- field dimensions in pixels;N_BALLS
- the actual number of balls to be added (optimal value is from 0 to 100);BALL_MIN_X_SPEED
andBALL_MAX_X_SPEED
- minimum and maximum initial speed for the ball along the x-axis;BALL_MIN_Y_SPEED
andBALL_MAX_Y_SPEED
- minimum and maximum initial speed for the ball along the y-axis;BALL_MIN_RADIUS
andBALL_MAX_RADIUS
- minimum and maximum ball radius (I recommend no more than 5% of the field size);BALL_MIN_X_ACCELERATION
,BALL_MAX_X_ACCELERATION
,BALL_MIN_Y_ACCELERATION
andBALL_MAX_Y_ACCELERATION
- initial acceleration along the axes, if less than 1 then the balls will slow down and the system will lose energy, if more than 1 then the balls will accelerate and the system will increase energy and if equal to 1 then the balls will move at a constant speed and the energy of the entire system will be unchanged;HANDLE_COLLISIONS
- a boolean indicating that collisions should be handled, when set toFalse
the balls will fly through each other;FPS
- the number of frames per second, optimally 120, but depends on the performance of the hardware.
The mathematical model is two-dimensional, so
In order to represent the movement of the ball on a plane, we will store the speed along the
In order for the ball to move in time with different speeds, it is also necessary to store the acceleration along the
Also, the ball needs a radius
To move the ball, it is necessary to recalculate the coordinates and accelerations along axes in t seconds using the following formulas:
When colliding with a wall, to reflect the ball from it, it is enough to invert the direction of movement along the corresponding axis:
To check whether it is necessary to handle a collision between the balls, it is necessary to estimate the distance between their centers and, if it is less than or equal to the sum of the radii, process the collision. To indicate the overlap of balls (collisions), the following formula is used:
If the balls collided (an overlap was detected according to the formula above), then it is necessary to push the balls away from each other and recalculate the corresponding direction vectors and velocities.
First you need to calculate the normals for the axes:
Then the tangents for the axes are calculated:
Next, the scalar product of the normals for each axis is calculated:
Calculate momentum for each axis:
We calculate the scalar product of tangents along the axes:
And finally we recalculate the speeds for both balls along each axis:
The kinetic energy of each ball is calculated by the following formula:
The total energy of the system is calculated as the sum of the energies of all its balls: