im a beginner coder (this is my first java course) and i have a project due tomorrow!! Assignment: Use stdDraw to make ball that bounces off the walls with an obstacle that only moves horizontally across the screen (the obstacle also bounces off the walls, side-to-side). Now here is where im having issues: if the ball hits the obstacle, it must bounce off.
here is my code:
public class BouncingBall {
public static void main(String[] args) {
//Features of the BALL
double rx = .0480, ry = .860;
double vx = .0015, vy = .0023; //velocity
double radius = .05;
//Features of the OBSTACLE
double x = -.080, y = -.480;
double vox = -.002, voy = 0.0;
double height = .02, width = .15;
// set the drawing area to be [-1, 1]
StdDraw.setXscale(-1.0, +1.0);
StdDraw.setYscale(-1.0, +1.0);
while(true) {
// Movement of the ball
if (Math.abs(rx + vx) > 1.04) vx = -vx;
if (Math.abs(ry + vy) > 1.04) vy = -vy;
rx += vx;
ry += vy;
// Movement of the obstacle
if (Math.abs(x + vox) > 1.0) vox = -vox;
x += vox;
/* if (((rx + radius) > (y + height)) || ((rx + radius) < (x - width))){
vy = -vy;
}*/
// set background color
StdDraw.clear(StdDraw.GRAY);
// set ball color
StdDraw.setPenColor(StdDraw.RED);
// draw the ball
StdDraw.filledCircle(rx, ry, radius);
// obstacle color
StdDraw.setPenColor(StdDraw.BLUE);
// draw the obstacle
StdDraw.filledRectangle(x, y, width, height);
// pause animation for 5 millisecond
StdDraw.show(5);
}
}
}
The part between the /* */ comment is wwhat i tried for detecting collision
Aucun commentaire:
Enregistrer un commentaire