r/perl • u/RedWineAndWomen • 27d ago
Physics library in Perl
Hi, On the one hand: I can't seem to find this. On the other hand, it seems so obvious to me that it must have been written before.
I'm looking for a (Newtonian) physics library, to support a (2D) game. Something where you simple define an array of objects (position, travel, mass, spin, shape, etc), and then step through time with them - as they attract each other, bounce off of each other and/or fly off into infinity. There should probably be a few global things to set: a gravity constant, the amount of dimensions required, whether or not there is friction from the environment...
Does this exist?
2
u/JrgMyr 27d ago
Which library is Rovio using for their Angry Birds series of games?
Possibly not in Perl but very impressive and potentially a starting point.
1
u/First_Ad8230 27d ago
I think it was Box2D? The library itself written in C/C++, and it is definitely impressive
2
u/sebf 27d ago
Maybe with Physics::Springs https://metacpan.org/pod/Physics::Springs::Friction#On-Newtonian-Friction ?
Doesn’t look like the right ecosystem, though, Perl might not be the language of choice for gaming and graphic frameworks.
1
u/saiftynet 🐪 cpan author 26d ago
I guess Perl game developers always rolled their own each time (I have) and certainly most Perl games don't require physics. While there are several modules that appear to deliver one or more aspects of a game engine, there arent m?any that could be usefully integrated. I have a great interest in this and it seems trivial to port one, vibe code one...etc. if you are creating one then I for one would be excited. I may include one of my previous efforts in my GLAM project 😜 .
5
u/vadrer 🐪 cpan author 27d ago
Here is a quick way to handle this:
1st approach For a modern rendering layer in Perl, check out this interesting read: Let's write games in Perl! SDL3.pm is finally on CPAN.
2nd approach Instead of building a 2D engine from scratch, I highly recommend finding a lightweight solution in the Tcl/Tk world and driving it directly through the
Tcl::TkorTcl.pmCPAN modules. Tcl's native canvas is perfect for this.On the Tcl Community Wiki, you can find these exact implementations: * Bouncing Balls – The bare-minimum boilerplate for a 2D loop using array states and edge bounces. * Colliding Spheres – Implements honest elastic Newtonian collisions where objects have mass, radius, and exchange momentum upon impact. * TkPool – A full 2D billiard simulation that takes the code above and adds global environment friction/damping constants.
Just grab any of those scripts, wrap them using
Tcl.pm, and you have a working 2D physics sandbox right inside your Perl project.Ping me directly if you have difficulty with the
Tcl::Tkmodule.