Lab Assignment: Orb Bouncer

Overview

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.

Learning Objectives

The Orb Class

Instance Variables (all private)

Class Constants (public static final)

Constructors

You must implement three constructors:

  1. Default constructor Orb()
  2. Position constructor Orb(double x, double y)
  3. Full constructor Orb(double x, double y, double vx, double vy, double ax, double ay, double radius, Color color)

StdLib

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…’

Methods

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.

Getter methods

Provide public getter methods for: x, y, vx, vy, and radius

The Main Simulation Class

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.

Testing Your Code

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.

Tips

Next steps

Submission

This will be submitted as part of a larger project.