Physics Simulation
Overview
Pelota implements a realistic physics engine that simulates ball motion with gravity, air resistance, spin effects (Magnus force), and bouncing. The simulation is deterministic and can predict ball trajectory for future frames.
Physics Constants
All physics calculations use the following constants (from GameConstants):
| Constant | Symbol | Value | Unit | Description |
|---|---|---|---|---|
| Gravity | 9.81 | m/s² | Standard Earth gravity acceleration | |
| Ball Damping (Bounce) | 0.7 | - | Vertical velocity retention (70% energy) | |
| Ball Damping (Horizontal) | 0.9 | - | Horizontal velocity retention after bounce | |
| Spin Side Effect | 0.08 | - | Sidespin multiplier for velocity change | |
| Spin Down Force | 0.1 | - | Topspin multiplier for Magnus downward force | |
| Air Drag Coefficient | 0.02 | - | Air resistance factor | |
| Ground Level | 0.035 | m | Ball contact threshold with ground |
Velocity Update Algorithm
The ball velocity is updated each physics frame using this sequence:
1. Magnus Force & Gravity
The Magnus force is a sideways and downward acceleration caused by ball spin. Combined with gravity:
Where is the spin vector:
- : Sidespin (causes horizontal curve)
- : Topspin (positive) or Backspin (negative)
- : Forward spin
The total downward acceleration is:
The velocity update becomes:
2. Air Drag
Air resistance creates a velocity-dependent drag that slows the ball:
This exponential decay approximates aerodynamic drag:
3. Position Update
After velocity is computed, position is integrated:
Collision & Bounce Physics
Ground Bounce
When the ball collides with the ground, velocity is decomposed into normal and tangential components:
Where is the ground normal.
Energy-Based Bounce
If the normal velocity magnitude is significant ():
The new velocity is:
Rolling State
If normal velocity is below threshold, the ball rolls without bouncing:
This simulates friction and sliding on the court surface.
Trajectory Prediction
The trajectory prediction simulates future ball motion for AI and aiming systems. It runs a deterministic physics loop:
for step in range(TRAJECTORY_PREDICTION_STEPS):
v = ApplyMagnusAndGravity(v, spin, dt)
v = ApplyAirDrag(v, dt)
p += v * dt
if p.y < GROUND_LEVEL:
v = SimulateBounce(v)
p.y = GROUND_LEVEL
bounce_count += 1
if |v| < VELOCITY_THRESHOLD:
breakEach trajectory point records:
- Position: in world coordinates
- Time: Elapsed time in seconds
- Bounce Count: Number of ground contacts so far
Prediction Parameters
| Parameter | Value | Purpose |
|---|---|---|
| Steps | 200 | Maximum trajectory points |
| Time Step | 0.016 s (16 ms) | Matches 60 FPS refresh rate |
| Stop Threshold | 0.01 m/s | Minimum velocity to continue prediction |
Velocity Calculation for Targeting
When a player executes a stroke, the AI must calculate the required velocity to land the ball at a target position:
Given:
- Start position:
- Target position:
- Z-velocity constraint:
The algorithm iteratively adjusts and to match the target:
Update rule:
Where learning rate and typically 8 iterations are performed.
Spin Parameter Conventions
The spin vector uses the following conventions:
Component Meanings
| Component | Range | Effect | Example |
|---|---|---|---|
| (Sidespin) | -1 to +1 | Curves ball left (-) or right (+) | 0.1 = slight right curve |
| (Topspin) | -15 to +1 | Topspin (+) creates downward curve, Backspin (-) creates upward curve | -10 = strong backspin, 0.8 = topspin |
| (Forward Spin) | -1 to +1 | Rotation along forward axis (cosmetic effect) | -0.65 = significant forward spin |
Physical Interpretation
- Topspin (): Ball dips down rapidly, short arc
- Backspin (): Ball stays up longer, float feel
- Sidespin (): Ball curves sideways during flight
- Slice (high negative + ): Combination of backspin and sidespin
Spin-Affected Stroke Examples
Forehand Drive
- Spin:
- Power: 25 m/s
- Result: Forward-moving with topspin curve, slight sidespin
Backhand Slice
- Spin:
- Power: 18 m/s
- Result: Low arc with strong backspin and sidespin
Serve
- Spin:
- Power: 35 m/s
- Result: Fast, flat serve with slight sidespin
Drop Shot
- Spin:
- Power: 5 m/s
- Result: Short, high arc with heavy backspin
Frame Rate Considerations
The physics engine runs at:
- Simulation Rate: 60 FPS (0.01667 s per frame)
- Trajectory Simulation: 240 FPS (0.00417 s per step) for finer prediction
- Physics Time Step: s in gameplay
This ensures smooth motion and accurate collision detection.
Energy Conservation
The ball loses energy through:
- Bounce Damping: and (10-30% loss)
- Air Drag: Exponential decay proportional to velocity
- Friction: Horizontal damping during rolling
Total energy is never conserved, realistically modeling real tennis ball behavior.