Nature2D - 2D Physics Engine for UI Elements


v0.7.1-beta

GithubRoblox ModelDocumentationAPI ReferenceTutorials


About

Nature2D is a 2D physics library designed for and on Roblox! Ever wanted to create 2D games but step back because Roblox doesn’t have a built-in 2D physics engine? Use Nature2D to create versatile and smooth simulations and mechanics for your 2D games with minimum effort! Nature2D primarily uses methods of Verlet Integration and Convex Hull collisions.

It’s user friendly and supports all UI Elements. RigidBodies and constraints can potentially be made with almost all UI elements, from Frames to TextBoxes. Collision detection and response are also handled for all UI elements by default.

Create almost anything you can imagine. From bouncy boxes to destructible structures, even character movement in no time. Here’s a wrecking ball connected to an invisible constraint knocking a few boxes off of the blue platform.

about-example


Showcase

Rotating RigidBodies

Smooth Collisions

Destructible Structures

Inclined Plane

Ragdolls

Constraints

External Forces like Wind


Conceptual Understanding

It is important to know how the library really comes together and do what its supposed to do. The library uses methods of Verlet Integration and Convex Hull Collisions (Separating Axis Theorem) to simulate physics. I have detailed the math and working of both of those methods on the Devforum and Github with code snippets. If you’d want to explore more about them, go through the following links.

The library is divided into different segments. The core, which is the Engine. The physics based classes, which are the RigidBodies, Constraints and Points, utilities which consists of non inclusive modules to help render constraints and points on the screen and global constant values.

Engine

The Engine or the core of the library handles all the RigidBodies, constraints and points. It’s responsible for the simulation of these elements and handling all tasks related to the library. Physics is simulated every RenderStepped with the following structure:

image

All tasks are performed in order as seen in the picture above.

Point

Points are what make the rigid bodies behave like real world entities. Points are responsible for the movement of the RigidBodies and Constraints. These points are not rendered on the screen by default and it is advisable to keep it that way. Points don’t need to be created manually unless creating custom Constraints. By default, points are handled by the core (Engine) itself.

Constraint

Constraints keep two points together in place and maintain uniform distance between the two. Constraints and Points together join to keep a RigidBody in place hence making both Points and Constraints a vital part of the library. Custom constraints such as Ropes, Rods, Bridges and chains can also be made. Points of two rigid bodies can be connected with constraints, two individual points can also be connected with constraints to form Ropes etc.

RigidBody

RigidBodies are formed by Constraints, Points and UI Elements. These RigidBodies are highly flexible to meet all your use cases. RigidBodies can be customized and custom physical properties can be defined for them. By default they abide by the universal physical properties of the engine. RigidBodies as of now cannot have different masses. An update has been planned to support this.


Performance

Regarding performance, Nature2D does just fine for almost any game. Every RenderStepped event, actions take place, as described above. Performance varies according to the amount of RigidBodies and constraints being simulated. It also depends upon how many collisions take place in between ‘n’ number of RigidBodies.

It runs at 60fps with an average of 100 collisions every frame. Frames may drop, if RigidBodies are used as particle emitters or large scale simulations. Its advisable to use custom 2D Particle Emitters instead of RigidBodies due to frame drops.


Accuracy

In terms of physical accuracy, Nature2D does well. It’s not and isn’t meant to be completely physically accurate since that’s near to impossible but I do plan on improving a lot of things that make it resemble the real world much better. Few things that I would improve is the friction. Friction currently is just a damping value, I do plan on refactoring it. (Refactored)

Collisions are not necessarily physically accurate. They are meant to have a game-like vibe. I do plan on refactoring it as well.


Conclusion

Nature2D is still in BETA. Thus, bugs, mistakes in the code are natural. If you spot any, you can open an issue on github or alert me over here in the replies. I’ll frequently update this resource. If you wish to contribute to this resource, head over to the github repository, fork it and open a pull request!

I hope you enjoy these posts and resources and I hope they help out some of you. If you wish to support these projects, chuck them a star! Stick around for more since there are a lot of new updates, projects and resources coming around! :heart:

You can follow my twitter. I post future and new updates for my projects as well as some cool creations!

Thanks for reading!

Contribution

If you encounter bugs or would like to support this project by improving the code, adding new features or fixing bugs - Feel free to open issues and pull requests! Also read the contribution guide!

Releases

606 Likes

I’ll frequently update the resource, thus I recommend you to get the Roblox Model or watch the github repository by clicking the “watch” button. Examples and tutorials will also be pushed out day by day (if possible)!

4 Likes

For GitHub, I like to release versions through it, and then you can have it only notify people of Releases.


7 Likes

Bro this is mad!

This is one of the best resource I’ve seen on this website in a while! Good job!

My collective brain cells are melting while trying to comprehend how you manage to make this on Roblox

30 Likes

angry birds nice
wonder what else could be made on this :thinking:

anyways great progress, you keep getting better and better

7 Likes

You can make almost anything that has to do with collisions or physics in general. This makes it easy to make stuff like people’s playground, construction based educational/adventure games, pong games, slither io games, physics based platformers etc.

It all lies within the infinite boundaries of imagination!

9 Likes

awesome, probably going to make a yt tutorial on this to help out a bit
thanks for the reply

8 Likes

Pretty cool! But I’m wondering how this would do on high frame-rates?

4 Likes

This is cool!
Now we are getting Unity2D in roblox. I’m just kidding.
This is actually awesome!

9 Likes

It should do well, although I haven’t tested it yet. It may result in the simulations being a bit faster. If you encounter any undesired behavior, you can open an issue in the github repository or alert me here.

Thanks for bringing this up, I’ll make the use of detlaTime (I should have from the start >_<) and push the update sooner or later!

7 Likes

Amazing resource. If something like this was around (or if I was as bigbrain as OP to implement this myself) when I was making my game Calmfall, I would’ve made it 2D! Thank you for the resource OP

6 Likes

v0.0.1 - Improvements & New Methods

Slowly improving custom Constraints support.

  • Improved architecture for anchored RigidBodies.
  • Constraints now have their own unique IDs like RigidBodies.
  • Added new Methods to Constraints
    • Constraint:GetLength()
    • Constraint:Destroy()
  • Added new Methods to Engine
    • Engine:GetConstraints()
    • Engine:GetConstraintById()
  • Added new Methods to RigidBodies
    • RigidBody:SetFriction()
    • RigidBody:SetGravity()

Updates to Documentation - Updated


3 Likes

This is so amazing! This will enable some really cool things to be done with GUI, such as more natural animations. It will also enable some cool interactions with GUI… like replacing the Backpack GUI with a dynamic item shelf at the bottom of the screen, where items can be dragged around and can be dragged onto other items to interact with them, as well as dragged onto certain 3D things to interact with them (e.g. dragging a key onto a locked door to unlock the door) Cool stuff!

3 Likes

v0.0.2 - Custom Point Support, Configuration Methods and More!

  • Installation through Wally! Nature2D can now be installed using Wally, the package manager for Roblox. This requires wally to be installed on your device. In order to install Nature2D, add a dependency to your wally.toml file
    • image

    • After you have added the dependency, run wally install in the command line. A “Packages” directory is created containing the library. You can now use Nature2D in your external editor using wally!


  • Bug Fixes
    • Linked Issue: #1
  • Engine:CreateCanvas() now has an optional ‘frame’ parameter to help render custom points and constraints.
  • Refactored certain segments of code for better readability.

  • Added Custom Point support to Engine
    • Engine:CreatePoint()
    • Engine:GetPoints()
  • Added new Configuration methods to Constraints
    • Constraint:Stroke()
  • Added new Configuration methods to Points
    • Point:SetRadius()
    • Point:Stroke()
    • Point:Snap()

Updates to Documentation - Updated

Update Wally package - Updated



Documentation will be updated soon. New examples and tutorials will also be added so look out for those at the github repository!

4 Likes

Note for the 2 people that read down here.

This will bug with seperate framerates. I.E: Higher than 60 will run faster than normal, and lower than 60 will… just use your brain.

But yeah this is really sick. I give it the Official LemonyPep Seal Of Quality. Because that exists now.

3 Likes

Correct, this will be fixed in the upcoming versions though! Thanks.

2 Likes

The engine is now frame-rate independent!

Earlier: Simulations running on different frame rates had a difference in their speeds. A simulation running on 60fps would run faster than that of a simulation running at 30fps.

Now: Frame-rate no longer affects simulations. A RigidBody covering a distance of 10 units in a simulation running at 30fps and a simulation running 60fps will take almost the same time to reach the destination.

Note: This hasn’t been tested with multiple kinds of simulations. Thus, if you encounter any unwanted behavior, errors or visual bugs - you can open an issue at the github repository!

Wally Package Updated: 0.0.2 → 0.0.3
Roblox Model Updated
Github Repository Updated


More feature rich updates soon. Contribution is also welcome! Documentation will be updated in response to the previous updates, soon!

5 Likes

v0.0.4 Engine:SetSimulationSpeed() & Documentation

  • Updated API-References for RigidBodies, Constraints, Points and Engine in response to this and previous updates.
  • Documented source code with comments.
  • Better error handling and error messages.
  • Fixed Engine:SetPhysicalProperty() bug where updating a physical property before creating a rigidbody did not change the properties for the rigidbody.
  • Added new Methods to Engine

Updated Documentation

Wally Package Updated - 0.0.3 → 0.0.4

Roblox Model Updated


2 Likes

No way! Finally what I always wanted! Now I don’t have to script gui collisions anymore

I really wanted this for like 3 years all I could do is 3d, but now it’s finally here

2 Likes

I have been messing around with this a little and it’s pretty cool! Although I did run into two issues:

  • There’s a lot of lag when there is a large amount of RigidBodies (I was able to fix this on my own by editing the collision checking code to only check for collisions on anchored bodies if the other body is not anchored)

  • If an unanchored RigidBody is in a position where it should seemingly not be moved by physics (on top of an anchored RigidBody, for example), it will be continuously moved by a small amount to the left/right every few frames

This resource would be incredibly useful if these issues were to be fixed!

3 Likes