To simulate the projectile motion of a ball and stop the simulation once the y-position goes below the floor (y = 0), you can use a loop that updates the position and velocity of the ball at each time step. Here's how you can modify your code to include this logic:
angle_rad = deg2rad(angle);
Vx = V0 * cos(angle_rad);
Vy = V0 * sin(angle_rad);
Sy = Sy + Vy * dt - 0.5 * g * dt^2;
Sx_array = [Sx_array, Sx];
Sy_array = [Sy_array, Sy];
plot(Sx_array, Sy_array);
xlabel('x-position (m)');
ylabel('y-position (m)');
title('Projectile Motion of a Ball');
Explanation
- Parameters: Define the initial conditions, gravitational constant, and time step.
- Convert Angle to Radians: Convert the projection angle from degrees to radians.
- Initial Velocities: Calculate the initial velocities in the x and y directions.
- Initialize Positions: Set the initial positions.
- Initialize Time: Set the initial time to zero.
- Initialize Arrays: Create arrays to store the positions for plotting.
- Loop: Use a while loop to update the positions and velocities at each time step until the y-position (Sy) goes below the minimum value (ymin).
- Update the x and y positions using the current velocities and time step.
- Update the y-velocity to account for the effect of gravity.
- Increment the time by the time step.
- Store the current positions in the arrays.
- Plot the Trajectory: Plot the stored x and y positions to visualize the projectile motion.
This code will simulate the projectile motion of the ball and stop the simulation once the ball hits the floor (y = 0). Adjust the parameters as needed for your specific scenario.