Modify the move method to accelerate the boxBall based on the gravity constant.
ID: 3805337 • Letter: M
Question
Modify the move method to accelerate the boxBall based on the gravity constant.
Hint 1: Acceleration/deceleration is a change in ySpeed value
Hint 2: ySpeed is reverse of normal physics, negative is up, positive is down
public class BoxBall {
private int diameter;
private float xPosition;
private float yPosition;
private float xSpeed;
private float ySpeed;// initial downward speed
private static final int GRAVITY = 3; // effect of gravity
/**
* Constructor for objects of class BoxBall
*
* @param xPos the horizontal coordinate of the ball
* @param yPos the vertical coordinate of the ball
* @param xSpeed the horizontal speed of the ball
* @param ySpeed the vertical speed of the ball
* @param ballDiameter the diameter (in pixels) of the ball
*/
public BoxBall(int xPos, int yPos, int xSpeed, int ySpeed, int ballDiameter)
{
xPosition = xPos;
yPosition = yPos;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
diameter = ballDiameter;
}
public void move() {
//accelerate based on gravity constant
// compute new y position
this.ySpeed -= yPosition;
}
Explanation / Answer
public class BoxBall {
private int diameter;
private float xPosition;
private float yPosition;
private float xSpeed;
private float ySpeed;// initial downward speed
private static final int GRAVITY = 3; // effect of gravity
/**
* Constructor for objects of class BoxBall
*
* @param xPos the horizontal coordinate of the ball
* @param yPos the vertical coordinate of the ball
* @param xSpeed the horizontal speed of the ball
* @param ySpeed the vertical speed of the ball
* @param ballDiameter the diameter (in pixels) of the ball
*/
public BoxBall(int xPos, int yPos, int xSpeed, int ySpeed, int ballDiameter)
{
xPosition = xPos;
yPosition = yPos;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
diameter = ballDiameter;
}
public void move() {
//accelerate based on gravity constant
this.ySpeed = this.ySpeed + GRAVITY; // v = u + at assuming move is called every second
// compute new y position
this.yPosition -= (this.ySpeed - 0.5*GRAVITY);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.