In this lab, you will create an Orb class to simulate
bouncing circular objects with realistic physics. You previously worked
with a single bouncing ball—now you’ll refactor that code using
object-oriented programming principles to manage multiple orbs
simultaneously.
x, y - position (in pixels)vx, vy - velocity (in pixels/second)ax, ay - acceleration (in
pixels/second²)radius - size of the orb (in pixels)color - Color object for drawingWIDTH = 640 - canvas width (you can change this)HEIGHT = 480 - canvas height (you can change this)GRAVITY = 980.0 - gravitational acceleration in
cm/sYou must implement three constructors:
Orb()
ax = 0, ay = -GRAVITYradius = 20 and
color = StdDraw.BLUEOrb(double x, double y)
Orb(double x, double y, double vx, double vy, double ax, double ay, double radius, Color color)
We will use StdLib again for this. Look for your stdlib.jar file, copy it to the ‘src/’ folder and right click – ‘Add as library…’
public void update(double deltaT)Updates the orb’s position and velocity using Euler integration. This method is provided for you:
public void update(double deltaT) {
// Update position
double deltaX = vx * deltaT + 0.5 * ax * deltaT * deltaT;
double deltaY = vy * deltaT + 0.5 * ay * deltaT * deltaT;
x += deltaX;
y += deltaY;
// Update velocity
vx += ax * deltaT;
vy += ay * deltaT;
// Check for wall collisions
checkWallCollision();
}private void checkWallCollision()Detects when the orb hits a wall and reverses the appropriate velocity component. An orb hits a wall when its edge (center ± radius) reaches a boundary.
Physics of wall collision: When an orb bounces off a
wall, the velocity component perpendicular to that wall reverses. For
example, hitting the top or bottom wall reverses vy, while
hitting the left or right wall reverses vx.
public void draw()Draws the orb on the canvas using
StdDraw.filledCircle(). Set the pen color before
drawing.
Provide public getter methods for: x, y,
vx, vy, and radius
You will be provided with most of the main simulation class (see starter code). The key structure is:
private ArrayList<Orb> orbs;
public void createOrbs(int n) {
// Create n orbs and add them to the ArrayList
}
public void run() {
// Animation loop:
// 1. Clear the canvas
// 2. Update all orbs
// 3. Draw all orbs
// 4. Show the frame
}createOrbs – you need to write this method! Add orbs to the orb list.
Start with 5-10 orbs. You should see them bounce around the canvas with gravity pulling them downward. Orbs should stay within bounds and bounce realistically off walls.
x should
be between radius and WIDTH - radiusMath.random() to generate random valuesay should be negative (gravity pulls
downward)This will be submitted as part of a larger project.