The Invisible Boundaries: Collisions and Physics
At videogames the forces of physics are ruled by pixels and maths, apples doesn't fall from trees without your design
Hello my fellow gamedevs!
After a few years working in my spare time on pixel art and gamedev, I came to the conclusion that we can’t learn everything, and when I mean everything I’m talking about reaching a mature level at the fundamentals of gamedev — basically, the essential knowledge.
But solo gamedev is about motivation and discipline, right? Don’t worry, I have good news for you.
We can’t master all the concepts, but I have a learning technique that helped me a lot during the process: creating a dictionary of concepts and short definitions.
In this way, your subconscious is creating a treemap structure in your brain which later, during your gamedev sessions, will be showing up like a Rattata on Route 1.
In this post I want to introduce you to collisions and physics.
Collisions - Translate Real World Rules Into Game Rules
I’m totally conviced that Isaac Newton and Albert Einstein would be proud of games like Superliminal or Minecraft where the physics, energy and mass laws they defined were totally challenged and the fourth wall broke. (Or maybe not?)
There is a key moment during gamedev, when you feel you are bringing life to characters and objects — it is so satisfying.
But wait… how I’m supossed to do that? Well, gamedev engines provide a wide set of tools to make our lives easier, but at the end the game feel will be connected to your own (and unique) translation between the real world rules and your videogame.
Some Inspiration
GTA IV Euphoria game engine was insane on that matter, the approach they had at physics manages to create thrilling atmospheres and feeling alive moments, something most of the games during the last 15 years never achieved.
But now, going back to your unique interpretation of collisions, let’s first define a collision on our real world.
Collision is short-duration interaction between two bodies or more than two bodies simultaneously causing change in motion of bodies involved due to internal forces acted between them during this.
Let’s break it down (You, my subscriber, know I love to do this)
Short-duration
Causing change in motion
Internal force
There is actually one game that could illustrate all those properties of a collision, this game is called Arkanoid
Arkanoid for PICO8 - Collision and Physics
Using Arkanoi-8 by Josh Millar developed on PICO-8 as reference - https://www.lexaloffle.com/bbs/?pid=18924#p
Our goal in Arkanoid is to break all the colorful bricks on each level. There is a satisfying feeling of progress during the session, as you can clearly see what’s remaining to complete the level.
But there is another perfect scratch for your itch playing Arkanoid: the direction of the ball is based on where the ball hits the platform, changing the ball path based on your movement.
I’ve been reading the beautiful code of Arkanoi-8 to understand it’s logic and teach you how simple calculations (using math, especially algebra) can result into smooth and refined gameplay.
-- Code created by Josh Millar
function collide_ball_paddle()
-- check if ball is touching
-- the paddle
if(collide_rect(
b.x,b.y,
b.w,b.h,
p.x,p.y,
p.w,p.h)) then
-- it is! make it bounce up
-- if it’s currently moving
-- downward
if(b.vy > 0) then
ball_bounce_vert()
sfx(2,0)
b.y = p.y - b.h
-- and change ball angle
-- based on where the ball
-- hit the paddle
local bcenter = b.x+(b.w/2)
local pcenter = p.x+(p.w/2)
local offset = (b.x-p.x)/p.w
b.th = 0.375 + (offset*-0.3)
b.v += 0.1
b.vx = b.v * cos(b.th)
b.vy = b.v * sin(b.th)
p.bounces += 1
end
end
endLet’s break it down to understand the logic behind this Simple collision and physics system, initially as you can read by the code comments, the game just detect if the ball hitted the platform (also called paddle)
if(collide_rect(
b.x,b.y,
b.w,b.h,
p.x,p.y,
p.w,p.h))As soon as that function return a true value, then the logic of direction check is triggered,
local offset = (b.x-p.x)/p.w -- Detect ball hit position at the paddle
b.th = 0.375 + (offset*-0.3) -- Calculate local offset. Calculates a normalized value (roughly 0.0 to 1.0) representing where on the paddle the ball hits. As you see at the previous recording, right side of the paddle is closer to 1.0 and left side of the paddle is closer to 0.0.
The formula b.th = 0.375 + (offset*-0.3) is calculating in which direction the ball should be moving, based on the previous hit calculation. Basically is the angle of the ball, which is required later to calculate our X and Y coordinates.
We are making simple games with a tiny scope and the number of calculations and physics are limited, but imagine on AAA productions like GTA IV where physics are calculated for many objects at the same time.
That’s when the magic of math is super useful to create efficient code, a code that could escalate to other elements in case needed.
At this system, the ball velocity (b.v) and the cos of the angle is used to determine coordinate X, then velocity and sin of the angle is used to determine coordinate Y.
b.vx = b.v * cos(b.th)
b.vy = b.v * sin(b.th)PICO-8 uses an input range of 0.0 to 1.0 to represent the angle, a percentage of the unit circle.
Important: PICO-8 measures the angle in a clockwise direction on the Cartesian plane, with 0.0 to the right, 0.25 downward, and so on. This is inverted from the convention used in traditional geometry, though the inversion only affects
sin(), notcos().
Applying trigonometry, we can quickly calculate the movement of the ball in a really efficient way, this calculation is commonly used for smooth gravity systems where refined physics are key to polish game feel.
Did you liked this concept? Try yourself!
If you really enjoyed this concept as much as I did during the writing of this article I really encourage you to create physics for a basketball ball (bounce effect) using PICO-8 and the concepts shared at this article.
Keep it simple, just create an environment with a floor and your ball, depende from where you drop, the bounce will be different. You will be creating systems that could be useful for your future projects, trust me!
Thank you for reading.
Other Articles from Gamedevpills
Every Great Game Starts with Words, Not Code
We’ve been speaking about Game design, time management, scope definition and many aspects on previous post but we never mentioned our elephant in the room…













