Ball Physics
In this project, you will learn how to code (in java) your own ball physics simulator!
(Full Project Attached)
This isn't really a Animation but a simulator showing the physics of a ball bouncing.
This system uses a simplified form of:
Newtonian motion:
- Force → acceleration (gravity)
- acceleration → velocity
- velocity → position
(Also called Euler Integration)
It also includes time Scale , where you can observe and play with the sliders
Downloads
Supplies
Setup, Prepare Variables and World
what this does
This is the base of the entire simulation.
- balls stores every object in the world
- physics variables define how the world behaves:
- gravity = how fast things fall
- elasticity = how “bouncy” collisions are
- startVelocity = how fast new balls begin moving
- timeScale = slow motion control
- airResistance = drag force
important idea
Nothing is actually drawn yet, we are just setting up the variables
Initialize Sliders and Make First Ball
what this does
This runs once at the start.
- creates the window
- spawns the first ball
- builds all UI sliders
important idea
This is where all the variables are controlled, and crating the UI for the sliders.
Actual Simulation
what this does
This is the core simulation loop that runs every frame.
It does 3 major jobs:
- read UI values → update physics variables
- update every ball
- draw everything
important idea
This is the main loop of the system.
Ball Objects
what this does
Each ball is a self-contained objects, which stores the physics variables.
It stores:
- position (x, y)
- velocity (vx, vy)
- size (r)
- color
important idea
this makes it so each ball behaves independently.
Updating the Balls
what this does
This is the physics simulation core.
Step by step:
- gravity increases downward velocity
- velocity moves position
- air resistance slows motion over time
important idea
This is where the balls get affected by the variables.
World Edges(boundaries)
what this does
This keeps balls inside the world:
- floor collision:
- stops sinking
- reverses velocity
- reduces energy
- wall collision:
- reverses horizontal motion
important idea
This makes a closed space for the ball so it doesn't go out of the world.
Rendering
what this does
This simply visualizes the balls
- position → screen location
- radius → size
- color → appearance
important idea
Makes the balls visible
Adding Buttons
what this does
Creates interactive controls:
- add new ball
- reset simulation
important idea
Adds buttons to better control the system.
Mouse Interaction
what this does
Handles clicking:
- detects button presses
- selects which slider is active
important idea
This is allows you to click and drag sliders with your cursor.
Slider Dragging
what this does
Allows real-time control:
- click + drag slider = change physics live
- release = stop interaction
important idea
This is what makes the simulation feel more interactive.
Slider System
what this does
Each slider:
- maps mouse movement → numeric value
- controls physics variables
rendering slider
what this does
Draws:
- slider track
- knob position
- label + value
interaction logic
what this does
Checks if mouse is on the knob.
what this does
Converts the mouse position on the slider to a physics value
Conclusion
This is the end of the tutorial/breakdown of my project
Sources : Processing Wiki , YoutubeTutorial, and grammarly-my grammar isn't great :I
(This took a while so I hope you enjoyed it)
Thank You For Your Time