Physics 2D (A 2D Physics Library)

Hey all,

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 :smiley:

Cheers,
Tyler

I would like to be the first to say. WOW!! Thanks.
Can you provide a small example though. How do i tie the phyics object to the drawn object?

Oh and you missed a ) on line 159 and 269

Most definately, I am working on the whole set the state of the physics simulation, then I will create a small little example with a cube that should bounce off of 5 pixel barriers around the edge of the screen.

Give me a little bit.

Add support for rotation and then you’ll have my wowness. And alot of other peoples’ wowness.

The man above me tells the truth

I shall make that top priority after a simple example.

:slight_smile:

sigh I wish I knew physics. Looks pretty awesome though.

@Carnag3:

Could you tell me the specific lines? I have modified and added to the code, so the line numbers do not correlate for me here.

159: if (obj1.Mass > (obj2.Mass * 2) then
269: if (table.HasValue(Barriers,obj) then

Thanks, fixed. I will upload in a minute. I was just adding some functions to add forces to objects.

I will then work on a demo.

EDIT: Uploaded a new script. It has the functions Pause, Resume, Stop (pass true to delete all objects), AddForce(object,force), AddForceX(obj,force), AddForceY(obj,force) and fixes for the two errors found by Carnag3

Nox’s 2d mario boxes gamemode could use this.

Here is a slight demo.

The bars along the outer parts of the screen (white) are barriers. Green box is a physics box. Click middle mouse to toggle pause/running. Left click and add -50 VelocityY to the green box, Right click and add 50 VelocityX. Double click a mouse button to create new smaller boxes.

Right now I can tell it is horribly buggy (reminds me of Havok). The green block tends to fall into the barrier if shot at it too fast with a shitting bricks type effect.

New script uploaded (works 100% in game, removed all bugs preventing it from running).
Demo here: http://mysticfox.info/demo.lua (lua/includes/autorun folder)

404

My bad, fixed now.

So, is this just clientside 2d physics, or what I initially was hoping for?

Being, that I could spawn a 3d barrel, then turn this on, the 2d - block STOPS when it lands on the barrel, and reacts to said - barrel. (The barrel does not react in any way to the block, note.)

EDIT:

Lol ratings.

EDIT-2:

What I’m saying isn’t completely insane, but I suspect it MIGHT require a module. Then again, some aimbots can find props, but I guess that isn’t really related in the right way.

It could be done. i dont see the point to it but it could be done.

:pcgaming:

:doh: Fail

Nice release though! I think I’ll have a look into making something with this :dance:

Yeah, my spelling is indeed, due to my 100% shit keyboard. So that explains mistakes such as a missing trailing or leading parentheses.

It has been more than a month, and I had been busy with my own OpenGL based game engine, so I realized I hadn’t put much time towards this.

I have been playing with the innards of Havok and Bullet for my own engine, so I think I am going to come back, finish this and add angular velocity, then release a small 2D gamemode.

That’s prety neat but not really revolutionary.