Slingshot
Slingshot and Bird Setup: We start by creating a canvas using createCanvas(800, 600) to define the drawing area. The Bird class represents our projectile (similar to the Angry Birds bird). It has properties like position, velocity, and radius. The bird’s position is initially set to the center of the canvas. The slingshot is represented by a line connecting the bird’s position to a fixed point (slingshot). When the mouse is pressed, the bird is attached to the mouse position, and when released, it’s launched. The mousePressed() function checks if the mouse is over the bird. If so, it sets isDragging to true, allowing us to drag the bird. The mouseReleased() function calculates the force based on the distance between the bird and the slingshot and applies it to the bird. Bird Physics and Display: In the Bird class, the update() function handles physics. If the bird is being dragged (isDragging is true), its position follows the mouse. Otherwise, gravity is applied, and the bird falls. The display() function draws the bird as a red ellipse at its current position. The contains(x, y) function checks if a given point (mouse position) is inside the bird’s radius. The applyForce(force) function updates the bird’s velocity based on the applied force. We also keep the bird within canvas boundaries using constrain().
Link to the code