r/howdidtheycodeit • u/mystman12 • Jan 08 '23
Question How did they code physics in Pinball Dreams/Fantasies?
So I'm interested in trying to make a pinball game for the Playdate in my spare time as a side project. While I've managed to create a prototype that almost works I've run into a lot of problems, to the point where I'm wondering if I need to take a different approach.
To summarize what I have currently, there's basically a 2D array of data representing collision (I think it's 512x1024 in size, been a while since I touched this project) and a ball that, each physics update, checks each point around the circumference (There's about 80). If a point collides with the collision data it takes the ball's velocity and where the ball was hit and determines a new direction to move.
I have a prototype where this kind of works but there are issues with the ball clipping through collision points and getting stuck and other weird behavior. Also not entirely sure how I'd handle things like properly distributing forces when the ball collides with multiple points on the same physics update.
Anyways, last I was working on this it was just getting really messy and I started wondering if there was a better way. Anyone know how 2D pinball games on similarly limited hardware, like Pinball Dreams/Fantasies or Epic Pinball were programmed? Do they take a similar approach of having all the collision data represented via an array? Or is there a better way? I feel like there might be some way to represent collision via vectors or some other method that isn't limited in the same way a low-res array is, but I'm not sure how that would work. My current method just doesn't seem quite right for something so reliant on precise physics calculations.
13
u/[deleted] Jan 08 '23
Physics for something high speed like that can be really tricky to get right.. especially with penalty methods for collision vs continuous collision detection.. If you cant use continuous collision detection, then you need to run your physics at a much higher framerate to catch all the collisions because a fast pinball moves more than half it's radius in one frame.
With CCD on the other hand, it computes the collision as a capsule shape and can compute higher velocity. But assuming you have to stick with what engine is there.. I suggest trying to run the simulation at a higher rate, and also having a max velocity for your ball that you enforce.
This brings up the issue that doing all the collision checks per frame can be slow, so you may need to have some sort of spatial partitioning scheme.. like a coarse grid that contains a list of all the collision objects that intersect with each grid cell, and use that to cull the bulk of the collision checks.
Ok now that thats out of the way, I've re-read your post, and it sounds like you're rolling your own.. so you may be able to implement your own form of CCD. You treat your collisions as the collision of a line with various planes and circles. the planes are the rails and straight bumpers, and the circles are the round bumpers. Since those parts are all stationary, you can expand their shapes by the radius of the pinball, and reduce the problem to a bunch of line vs rounded shapes.. and rounded shapes can be decomposed into planes that connect with circles at their corners..
Those shapes, sometimes called Minkowski sums.. look like this: https://www.researchgate.net/profile/Fernando-Alonso-Marroquin/publication/227138410/figure/fig1/AS:650503558025234@1532103542799/Minkowski-sum-of-a-polygon-with-a-disk.png
https://image.slidesharecdn.com/minkowskisum-090917170955-phpapp02/85/minkowski-sum-on-2d-geometry-22-320.jpg?cb=1668136132
So to do CCD, you end up needing 2 routines.. The routines calculate the T value from 0 to 1 where a line intersects a primitive.. along the current timestep.
The first is line -> plane, and the second is line->circle. Once you have those.. you run them on all the lines/circles in your scene, and find the earliest T value.. advance the whole simulation to that T value, and then start again with the remaining time for the substep. It sounds complicated but it's not actually that bad.
You're only dealing with 3 combinations.. line -> plane intercept and line circle intercept, and the rest is all just how you advance the simulation etc.
I googled the playdate a bit more, and yeah it's gonna be challenge in a resource constrained environment like that.. so if rolling your own sounds too daunting, there is also this: https://devforum.play.date/t/playbox2d-port-of-box2d-lite-physics-engine-to-c-and-playdate-sdk/1656
Which may use/support CCD internally.
I've implemented both CCD and non CCD pinball/billiards sims before. Hit me up if you want to talk about it more.
9
u/[deleted] Jan 09 '23
Apparently the source code is online:
https://news.ycombinator.com/item?id=28667945
7
u/pigeon768 Jan 09 '23
Oh wow. Don't do that.
If you have two circles, with coordinates (x1,y1), (x2,y2) and and radii r1,r2, you know that the circles are intersecting if (x1-x2)2 + (y1-y2)2 <= (r1+r2)2. You also build a similar check for intersecting a line. I forgot the formula off the top of my head, but google for "line circle intersection" and you'll find it. Then you build the board out of lines and circles. Note that there's a difference between a collision between a circle and an arbitrary line and an axis aligned line; for instance, you will use one check for a line where the endpoints are (x1,y1) and (x2,y2) and a different check for x1,x2 and y. The axis aligned check is a lot faster. Then you build a circle-axis aligned rectangle check out of circle-axis aligned line checks, and you build a circle-polygon check out of circle-line checks.
This goes beyond pinball- basically all collision detection will follow a similar pattern. But in the specific case of pinball, you can hardcode that the radius of a pinball is 1. And then scale the board based on that.
4
u/BattleAnus Jan 09 '23
I think /u/cesium-sandwich touched on the physics aspect way better than I could, but I just wanted to touch on this:
Are you saying you have a 2D array of true/false values that say whether a space is occupied or not? If so, that's a super inefficient way to store collision data. It may work for that specific size but it's going to scale exponentially as you increase the board size, so you'll want to look at other solutions if you can.
A much more efficient way is to store only the vertices that make up the boundary of your collision volumes, so if you have a rectangular volume you'd only store 4 sets of 2D values. The tough part with that is that you now have to do more complex calculations involving the normals of the collision surfaces and such, and even more so if you allow non-convex collisions meshes. But if you restrict your volumes to simple shapes like circles and rectangles, it shouldn't be too hard, as there's a ton of resources on the internet for learning the math of that stuff.
If my assumption that you're storing boolean values over the whole space was wrong, then just ignore this comment :)
2
u/mystman12 Jan 09 '23
Thanks for all the comments everyone! It's all super helpful. I've never dealt with collision like this before so that's why my first attempt was so bad haha. This will be a good excuse to learn some new techniques!
2
u/mystman12 Mar 19 '24
I'm back to add some interesting info I found, funnily enough, on a Discord thread about someone else's Playdate pinball project. Supposedly, Pinball Dreams used a grayscale image to represent collision and surface normals simultaneously. Not only that, but this collision data was extruded by the radius of the ball so that the ball only had to do one collision check at the center. More info can be found here: https://talk.pokitto.com/t/wip-pinball-engine-for-the-pokitto/2206
I guess I didn't find this when Googling because there's no mention of Pinball Dreams on the thread, but the example in the first post is clearly the table Ignition from that game.
2
u/NUTTA_BUSTAH Jan 09 '23
Your physics system sounds quite hacky. I'd build a proper one with circle/box/line colliders with textbook math, then optimize if needed (aabb checks, broad/narrow phase, quadtrees and all that, going deeper as needed)
0
u/[deleted] Jan 08 '23
[deleted] — view deleted comment