Question: Using Processing 3.4 from processing.org Q2: Bumper Golf - You have a golf ball (the white circle) and a randomly placed hole (the grey circle).
Using Processing 3.4 from processing.org
Q2: Bumper Golf - You have a golf ball (the white circle) and a randomly placed hole (the grey circle). The white line is controlled by the mouse, and indicates how hard you will hit the ball, and in what direction. The ball will be hit when you press the mouse button. Dont shoot too hard, or the ball will go right over the hole and keep moving. Shoot straight, since the ball needs to be completely within the hole to fall in. This assignment is quite big compared to what you may be used to. Good commenting and explanations of what each region of code will do (basically, to help you break it down...) will be very beneficial and will save you a lot of debugging time. It will probably be helpful to write out what should happen in English first (in comments!!). The challenge here is to keep track of what state everything is in (what is currently happening). Is the ball moving? How fast? In what direction? Is there a hole? Is the ball inside the hole? etc. You will need global constants for all of the game parameters. This includes the MIN_SPEED of the ball (below which it is considered to be not moving - try 0.5 pixels per frame), the FRICTION (how much of the velocity is retained from frame to frame - try 0.98). You will develop others, and some are mentioned below. You will also need global variables for the hole location (both x and y coordinates), a boolean variable for whether there is currently a hole, variables for the ball location (x and y), the ball speed in x and y directions, a boolean variable indicating if the ball is moving, and others. In the draw block, you need to balance the following tasks: If there is a hole, draw it. If not, create one at a random location. The hole size should be 1.5 times the ball size. If the ball is not moving, then draw a line from the ball to the mouse and draw a white ball. If the mouse is pressed, then shoot: o Determine the x and y distances from ball to the mouse cursor and scale them down to get the initial x and y velocities (try a scaling factor of 0.04). That means, the velocity in the x direction in pixels per frame will be this scaling factor times the distance (in pixels) in the x direction, and similarly for the y direction. o Add 1 stroke to the players score, and print this to the console using println. If the ball is moving, o Draw the ball. o Add the x velocity to the x position and the y velocity to the y position. o If the ball is too close to the left or right side of the canvas, (less than one ball radius), reverse the sign of the horizontal velocity. o If the ball is too close to the top or bottom of the canvas, (less than one ball radius), reverse the sign of the vertical velocity. o Apply friction to the ball speed (multiply both velocity components by the friction factor). o If the ball is completely inside the hole (hint: if the distance between the two centers is less than the hole radius minus the ball radius), and the ball isnt moving too fast (try 2 pixels per frame), then the ball is sunk. You can calculate the velocity as the length of the hypotenuse (is the longest side of a right-angled triangle) if you make a triangle with the x and y speeds. Move the hole and move the ball back to the center of the screen (with no speed!) when the user clicks to reset the game. Remember that If the total ball velocity is too slow, mark it as not moving at all. Note for Implementation: Write the program in English first. Implement and test your program in phases. First get the ball on the screen. Then be able to shoot it using the mouse. Then implement the wall rebounding. Finally, add the hole and the scoring.
hints:
1. draw the ball.
2. draw the ball to move to a mouse click location
3. Update step 2, so it moves with a friction constant. From question the constant is 0.98. Which means, you multiply ballVelocityX and ballVelocityY with 0.98.
//update the ball velocity due to friction
ballVelocityX *= FRICTION_FACTOR; //FRICTION_FACTOR=0.98
ballVelocityY *= FRICTION_FACTOR;
//update the ball's location information based on velocity
ballX += ballVelocityX;
ballY += ballVelocityY;
4. Draw a line to the ball.
//line(mouseX, mouseY, ballX, ballY)
5. Determine of the ball is not moving only then draw the line.
//Condition is that the ballSpeed is less than 0.5. --> Then notMoving = true or //moving = false.
ballSpeed = sqrt(ballVelocityX*ballVelocityX+ballVelocityY*ballVelocityY)
//change flag accordingly
6. Accept mouse Click only when ball is not moving.
//Without using mouseClicked() function you used in A1, use two flags to determine.
//if moving = false. Then accept new click and new location.
(Try following example for understanding: Draw a rect. Change color to red only after mouse clicked using flags only)
7. ball is in hole
//If distance between hole and ball is less than the radius of the hole.
//And, ballSpeed is less than ballSunkSpeed = 2.0
Set a flag gameOver=true.
//You can stop moving the ball
(Try following example for understanding: Draw a circle on mouse pointer. Draw another center twice size of first one. Move your small ellipse inside bigger ellipse and change flag to true)
8. Bounce ball of the wall.
//Use the equation from ball in a box example in the text book.
9. Reset parameters when gameOver=true and mouse is clicked.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
