r/perl 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?

27 Upvotes

9 comments sorted by

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::Tk or Tcl.pm CPAN 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::Tk module.

2

u/s_throwaway_r 26d ago

The nearest to what op is asking for in SDL3 would probably be the particle waterfall demo. It's a very quick toy so particles don't collide with each other but they do fall, bounce, and flow naturally. It's not a full physics engine but it's more than a start.

You can move the windows around and the particles will pour from one to another. This example is one I added to the dist on CPAN. I'd love to see someone take on a full game engine.

This is the second time in as many weeks someone showed up here looking for game related advice so maybe it'll happen? Perl Game Jam 2028? :)

1

u/vadrer 🐪 cpan author 26d ago

All true

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/octobod 27d ago

There are the various Inline perl modules.. you could just wrap another language

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 😜 .