A bit new to actually posting something I have worked on on the forums for others to see, admire, view, comment/critique on, and edit, so feel free to let me know if I do something wrong.
I was horribly bored during the past 2-3 hours, and a friend came to me asking a question about physics. I answered, and he told me he was making a test little “game” hooked into the HUDPaint hook where you drug and threw a rectangle around the screen that reacted with realistic physics. After the time I spent doing customized Lua functions for him and helping him out a little bit, I decided to make an entire, self-contained, 99.99% accurate 2-dimensional physics library for Garry’s Mod for people to do with whatever they want.
To be honest, I have not taken the time to test it or implement it into an actual real world example, but all of the functionality comes directly from my intense study in the fields of physics and the physical sciences, and my particular attention to deal, though I do tend to make mistakes, hence while I release her in a pseudo WIP/Release state, basically a BETA of the library.
Though it is done, there is much more functionality I can add to make it more realistic and useful, so here is my TODO Addon / Fix list:
-- TODO: -- Consider adding physics calculation quality: Exact, Linear, Fast (Run slower and more accurate, balance between, or fast and inaccurate) -- Look for optimizations (the number of times I loop these table could lead to massive slow downs with lots of physics objects) -- Look for missed physics functions that have no been included yet (Though at present I do not feel adding shit as far as quantum physics, thermodynamics, or general dicking with energy goes) -- Implemented rectangles that can be rotated (requires angular intertia, torque calculations, etc.) -- Implement damping (linear damping, and maybe angular damping if angular physics is added) -- Add toggleable per-object gravity effects (simply multiply gravity for the object times a 0 or 1 value) -- Implement drawing functions? (I really wanted actual drawing to be handled seperately, but integrating may make this library more useful -- Create a Pong type game based on this library (Or some other cool 2D type physics game?) [DONE] -- Implement pause/resume of the physics state [DONE] -- Implement sleeping/waking of physics objects
I did my best to comment as I went, use OO style structure whenever possible, and make it optimized.
It can be downloaded here: http://mysticfox.info/physics_2d.lua
I would recommend placing it in lua/includes/modules so that anything can utilize it.
To use, simply call require(‘physics_2d’) at the top of your Lua file, and ensure that you call physics_2d.Initalize() to setup the default gravity variables. The physics simulation is presented hooked to the engine Think.
Once initialized, you use the functions physics_2d.CreatePhysicsObject and physics_2d.CreatePhysicsBarrier to generate mobile and immobile physics entities that are immediately added to the physics simulation. Drawing is presently up to you.
Expect to see me add to this. I am open to all comments, suggestions, and criticism of the positive and negative variety. My goal in posting this here is to expand and finalize it more quickly, as well as provide something that others can use that I have not yet seen done before. Also, please keep your “Oh I could do this in 30 seconds so fuck off suck a dick” type comments to yourselves, as I know flamers are quick on things like this, and it isn’t something anyone can just Google search and copy+paste into Lua accurately.
Hope people can use this