Untitled
unknown
plain_text
3 years ago
1.0 kB
7
Indexable
// Declare variables for the ball's position, speed, acceleration, and dissipation
let ballX;
let ballY;
let ballSpeedY = 0;
let ballAccelerationY = 0.1;
let ballDissipation = 0.9;
let ballWidth = 50;
let ballHeight = 50;
function setup() {
createCanvas(300, 300);
// Set the initial position of the ball
ballX = width/2;
ballY = 0;
}
function draw() {
background(255);
// Update the ball's speed based on its acceleration
ballSpeedY = ballSpeedY + ballAccelerationY;
// Update the ball's position based on its speed
ballY = ballY + ballSpeedY;
// Check if the ball has reached the bottom of the canvas
if (ballY+ballHeight > height) {
// If it has, reverse the ball's vertical speed
ballSpeedY = -ballSpeedY;
// Dissipate the ball's energy if it is moving upward (bouncing off the bottom wall)
if (ballSpeedY < 0) {
ballSpeedY = ballSpeedY * ballDissipation;
}
}
fill(255, 0, 0);
// Display the ball
ellipse(ballX, ballY, 50, ballHeight);
}
Editor is loading...