To model the motion and potential collision of two squares under gravity using integer-based physics, we can derive continuous equations that describe their positions at any given integer time without employing trigonometry or fractions.
Assumptions:
- The squares move in a two-dimensional plane with discrete integer coordinates.
- Gravity affects only the vertical motion, with a constant integer acceleration ( g ).
- Initial velocities and positions are integers.
- Collisions are perfectly elastic, and upon collision, the squares exchange their velocities.
Equations of Motion:
For each square ( i ) (where ( i = 1, 2 )):
-
Horizontal Position:
[
x_i(t) = x_{i0} + v_{ix} \times t
]
- ( x_{i0} ): Initial horizontal position.
- ( v_{ix} ): Constant horizontal velocity.
-
Vertical Position:
[
y_i(t) = y_{i0} + v_{iy} \times t + \frac{1}{2} \times g \times t^2
]
- ( y_{i0} ): Initial vertical position.
- ( v_{iy} ): Initial vertical velocity.
- ( g ): Gravity constant (integer).
Collision Detection:
A collision occurs when the bounding boxes of the two squares overlap. Assuming each square has a side length ( s ):
- Horizontal overlap:
[
|x_1(t) - x_2(t)| < s
]
- Vertical overlap:
[
|y_1(t) - y_2(t)| < s
]
Collision Response:
Upon collision, the squares exchange their velocities:
- Horizontal velocities:
[
v_{1x} \leftrightarrow v_{2x}
]
- Vertical velocities:
[
v_{1y} \leftrightarrow v_{2y}
]
Python Implementation:
The following Python code calculates the positions of two squares at any integer time ( t ) and checks for collisions:
def position_x(x0, vx, t):
return x0 + vx * t
def position_y(y0, vy, g, t):
return y0 + vy * t + (g * t * t) // 2
def check_collision(x1, y1, x2, y2, s):
return abs(x1 - x2) < s and abs(y1 - y2) < s
# Initial conditions (all integers)
x1_0, y1_0 = 0, 0
x2_0, y2_0 = 10, 0
v1_x, v1_y = 2, 3
v2_x, v2_y = -2, 1
g = 1 # Gravity constant
s = 5 # Side length of squares
# Time at which to calculate positions
t = 5 # Example time
# Calculate positions at time t
x1_t = position_x(x1_0, v1_x, t)
y1_t = position_y(y1_0, v1_y, g, t)
x2_t = position_x(x2_0, v2_x, t)
y2_t = position_y(y2_0, v2_y, g, t)
# Check for collision
collision = check_collision(x1_t, y1_t, x2_t, y2_t, s)
# Output results
print(f"At time t = {t}:")
print(f"Square 1 position: ({x1_t}, {y1_t})")
print(f"Square 2 position: ({x2_t}, {y2_t})")
print(f"Collision: {'Yes' if collision else 'No'}")