A fun visualization based on Opencv about physical motion
Video Demo: https://youtu.be/Wcz9SoOLR00
The system describes the trajectory of an artillery shell and an objective under gravity (without air resistance, friction). The goal is that find the trajectory of the canon shell so that it can break the objective.
The system includes two objects. The first one is a canon shell fired from a canon with initial speed v1, initial angle alpha1, initial coordinates x1_0 and y1_0. The second one is an objective with initial speed v2, initial angle alpha2, initial coordinates x2_0 and y2_0
Consequently, we find the coordinate equations of two objects in terms of time. Then, when the collision happens, the coordinates of two objects are equals.
For implement, we model two objects by OOP. Then we use Numpy libraby for efficient coordinates calculation. Finally, we use OpenCV library to visualize the trajectory of two objects. In case of collision, an effect will happen (see at the demo).
The coordinates of the canon shell in terms of time are given by:
$$x_1 = x_1(t_0) + v_1cos(\alpha_1)t$$
$$y_1 = y_1(t_0) + v_1sin(\alpha_1)t - 0.5Gt^2$$
where:
Similarly to the objective: $$x_2 = x_2(t_0) + v_2cos(\alpha_2)t$$ $$y_2 = y_2(t_0) + v_2sin(\alpha_2)t - 0.5Gt^2$$
When collision happens:
$$x_1(t_0) + v_1*cos(\alpha_1)t = x_2(t_0) + v_2cos(\alpha_2)t$$ $$y_1(t_0) + v_1sin(\alpha_1)t = y_2(t_0) + v_2sin(\alpha_2)*t$$
I choose a set of hyper-parameters
The solutions of the set of equations are
Result: The canon shell could break the objective
1. This project uses Python 3.9
2. Run 'pip install -r requirements.txt' to install dependencies
1. Change the parameters at main.py:
2. Run 'python main.py'
In the future, I will incorporate incremental learning to the canon shell, so it can find the objective automatically with any initial conditions.
In addition, I will improve the design of two objectives.