This simulation shows how the gravity acts on a ball, making it fall and bounce when it hits the ground. It's a classic demonstration of the motion under uniform acceleration and energy transformation.
Gravity is a natural force that pulls the objects towards the center of the Earth. In this simulation, the ball experiences a constant downward acceleration due to the gravity, causing it to speed up as it falls. The rate of acceleration depends on the gravity type selected (e.g., Earth, Moon, Jupiter or Mars).
let x = 100, y = 100;
let vy = 0;
const gravity = 0.98; // Gravitational acceleration
const bounceFactor = 0.7; // Energy loss on bounce
let ground = 400; // Ground level
function draw(){
vy += gravity; // Apply gravity to vertical velocity
y += vy; // Update position
// Check for collision with ground
if(y >= ground) {
y = ground; // Reset position to ground level
vy = -vy * bounceFactor; // Reverse and reduce velocity for bounce
}
//Draw the ball at (x, y)
circle(200, y, 40);
}| Parameter | Description | Example Value |
|---|---|---|
| Ball Size (px) | Determines the diameter of the ball in pixels. | 40 |
| Mass (kg) | Defines the weight of the ball, affecting its momentum and kinetic energy. | 5 |
| Friction Coefficient (μ) | Reduces the motion after bouncing to simulate air resistance. | 0 |
| Wind (m/s²) | Applies a horizontal acceleration simulating air movement. | 1 |
| Gravity Type | Selects the gravity strength based on different celestial bodies like Earth, Moon, Jupiter, or Mars. | Earth (9.81 m/s²) |