Nature2D - 2D Physics Engine for UI Elements

Hey, thanks for the feedback!

The large amount of lag is purely due to collision detection. In the next coming updates, I would be switching to quadtrees for iterating through the rigidbodies during collision detection, to make the engine 10 times faster!

The 2nd issue is known, well, to be honest that’s just how verlet integration works! A work around would be to set friction of all rigidbodies stacked on top of each other to 0 (RigidBody:SetFriction()) and then set it back to normal when some other body collides with it, etc. But yes, this is something I wish to fix in the future!

5 Likes

This is really cool. It opens a whole range of new ideas I can try, definitely will implement this in my projects

4 Likes

Very impressive. Glad to see more people contributing towards the 2D development scene considering it’s neglected by Roblox.

2 Likes

v0.0.5 - Fixes and New Stuff!

  • The library now utilizes sleitnick’s Signal class instead of bindable events.
    • RigidBody.CanvasEdgeTouched:Connect()
    • RigidBody.Touched:Connect()
  • Bug Fixes
    • Error Handling
  • Improved code - Removed bad practices
  • RigidBody.CanvasEdgeTouched event returns the edge the RigidBody collides with, a string which is one of the following: Top, Bottom, Left and Right.
  • Added new methods to Engine

Added new example which covers the concept of creating Custom Constraints, where we create the following simulation of a RigidBody hanging from a rope, and wind forces being applied on it.

Jump to Example

rope


Updated Documentation

Updated Roblox Model & Github

Updated Wally Package - 0.0.4 → 0.0.5



In the next few updates, I’ll try to implement Quadtrees in the collision detection algorithm to improve performance and make the library 10 times faster!

2 Likes

v0.1.0 - Additions to Constraints, Engine and RigidBodies.

  • Added new methods to Constraints
  • Added new methods to RigidBodies
  • Added new events to Engine
    • Engine.Started
    • Engine.Stopped
engine.Started:Connect(function()
    -- fires when the engine starts
end)

engine.Stopped:Connect(function()
    -- fires when the engine stops
end)

Documentation Updated

Roblox Model and Github Updated

Wally Package Updated - 0.0.5 → 0.1.0


Check out some stuff I made in the past weekend with Nature2D! The game of Plinko and a weight bearing flexible bridge by connecting RigidBodies with constraints!




Quality updates soon. Next updates will be regarding the implementation of quadtrees in collision detection. If anything breaks, open an issue at the github repository!

5 Likes

v0.1.2 - Quadtrees in the works!

  • Collision Detection is now being re-written, with the addition of quadtrees!
  • Fixed RigidBody:Destroy() Connections are now destroyed when RigidBody:Destroy() is called.
  • Added new methods to Points
    • Point:Velocity()
  • Added new methods to RigidBodies
    • RigidBody:AverageVelocity()
  • Added new methods to Constraints
    • Constraint:GetPoints()
    • Constraint:GetFrame()
  • Fixed Constraint:Destroy(). Does not spam errors if connected to a RigidBody now.

gif


Updated Roblox Model & Github

Updated Documentation

Updated Wally Package - 0.1.0 → 0.1.1 → 0.1.2


4 Likes

v0.2.0 - Quadtrees in Collision Detection!

Quadtrees have now been implemented into the collision detection algorithm making the engine 10 times faster than before. Instead of wasting resources on wasted and unnecessary collision detection checks, RigidBodies are now distributed into different regions of a quadtree with a collision hit range. RigidBodies only in this hit range are processed with collision detection checks. This opens the gate for many new creations that required a larger amount of RigidBodies to be simulated!

Since this is still in beta, there may occur bugs and unwanted behavior. If you encounter any, be sure to open an issue at the github repository. I’ll be adding configuration methods for you to switch between traditional methods of collision detection or quadtrees.


Updated Roblox Model & Github

Updated Documentation

Updated Wally Package - 0.1.2 → 0.2.0


Since Quadtrees may still have bugs, I added a new method to the Engine that lets you configure quadtrees.

Engine:UseQuadtrees(use: boolean) determines if quadtrees will be used in collision detection. By default this is set to false!

I think it would be nice if you made your Signal API have a public .Connected property, and also so that your Signal destroy method actually loop through the linked list and disconnect everything. Would make behaviour much more consistent with RBXScriptSignals.

Other than that I don’t know what to tell you because I’m not a math person :v:
Good job!

1 Like

Hey, dope module, I been using it for about a week now.

I’m trying to figure out how you make objects with more than 4 sides respect their collisions such you did in this clip.

The circle and round edges ^^

Would I trace it using multiple textlabels or would I just use an image with a set collision box?

Thanks.

2 Likes

Hey there! Its completely possible to make RigidBodies with different amount of sides. Say triangles, hexagons etc.

While there’s no API that eases this problem, there’s a cheeky way to do it. You create points and constraints to construct the RigidBody, you store those constraints and points in a table, then you create the RigidBody using Engine:CreateRigidBody(). After the Body is initialized, you destroy its UI element and replace its points and constraints with the ones you made earlier. Now, you just need to set the visible property of the constraints to true!

Collisions work for all possible convex shapes. If a concave RigidBody is made, its hitbox will still remain convex.

I may add the official API to make your job easier soon! I’ll attach a placefile for the same if I am able to reproduce the methods above.

p.s. That gif was actually a physics engine made with verlet integration outside of Roblox!

1 Like

Ooh I see! Thank you for the help! Forgot about the visible property on your constraints.

This also works for circles and more round wavy surfaces? Like if I wanted a hill on screen.

That gif was actually a physics engine made with verlet integration outside of Roblox!

Yeah, I couldn’t find the one you made with triangles so I took that. Amazing :flushed:

Yes! Hills can be made using the methods I told above with constraints and points mapped out as such:

The circles in my showcase examples use the default square hitbox of the library. It works pretty well if the circles are small!

image

Here’s the one with triangles!

2 Likes

v0.2.3 - State Management

Added state management for RigidBodies. Individual RigidBodies can have their own States/Custom Properties now!

  • RigidBody:SetState(state: string, value: any)
  • RigidBody:GetState(state: string)

Example usage:

image


Updated Roblox Asset & Github

Updated Documentation

Updated Wally Package - 0.2.1 → 0.2.2 → 0.2.3


You can track my progress for v0.3 over at the github repository!


v0.2.4 - Improvements to Code

  • Add type definitions & annotations
  • Type check everything (almost)
  • Removed code redundancies
  • Replaced deprecations
    • Vector2.xVector2.X
    • Vector2.yVector2.Y
    • Vector2.magnitudeVector2.Magnitude
    • Vector2.unitVector2.Unit
    • … etc

Updated Roblox Asset & Github

Updated Wally Package - v0.2.3 → 0.2.4



A variety of new Constraints - Coming soon!

v0.3 - New Constraints: Ropes, Rods & Springs!

Major Release.

  • Checks to prevent division by 0.
  • Fix Constraint:SetLength(). Disregard invalid lengths. (length <= 0)
  • Fix Engine:CreateConstraint(). Disregard invalid thickness and lengths. (length & thickness <= 0)
  • Added new methods to Constraint
    • Constraint:SetSpringConstant(k: number)
    • Constraint:GetId()
  • New “type” paramater to Engine:CreateConstraint()
  • New “restLength” parameter to Engine:CreateConstraint()
    • Engine:CreateConstraint(Type: string, point1: Point, point2: Point, visible: boolean, thickness: number, restLength: number)
  • Added rope constraint type
    • Constraints that have an upper constrain limit and exclusive of a lower limit. Similar to Roblox’s 3D Rope Constraints.
  • Added rod constraint type
    • These constraints are similar to how Rope constraints function. But unlike rope constraints, these constraints have a fixed amount of space between its points and aren’t flexible. These constraints can move in all directions just how rope constraints can, but the space between them remains constant.
  • Added spring constraint type
    • Spring constraints are elastic, wonky and flexible. Perfect for various simulations that require springs. Achieved using Hooke’s Law.
  • Type parameter in Engine:CreateConstraint() must be “SPRING”, “ROD” or “ROPE” (text case does not matter).

Spring Constraint Example:

The constraint’s length is randomly set every few seconds.


Previously

  • Added state management for RigidBodies. Individual RigidBodies can have their own States/Custom Properties now!
    • RigidBody:SetState(state: string, value: any)
    • RigidBody:GetState(state: string)
  • Add type definitions & annotations
  • Type check everything (almost)
  • Removed code redundancies
  • Replaced deprecations
    • Vector2.xVector2.X
    • Vector2.yVector2.Y
    • Vector2.magnitudeVector2.Magnitude
    • Vector2.unitVector2.Unit
    • … etc

Roblox Asset & Github Updated

Documentation Updated

Wally Package Updated


3 Likes

100% cool and i will use it in my game

1 Like

v0.3.1 - Fixes to Collision Detection & New Methods

  • Made restLength: number? an optional parameter for Engine:CreateConstraint().
  • Fixes to Collision Detection - Collision detection is no longer skipped at low frame rates.
  • Fixed how forces are applied to Points when the engine is framerate independent.
  • Added new methods to Engine
    • Engine:FrameRateIndependent(independent: boolean) - Determines if rendering frame rate does not affect the simulation speed. By default set to true.
  • Added new methods to Constraint
    • Constraint:GetParent()
  • Added new methods to Point
    • Point:GetParent()

Roblox Asset & Github Updated

Updated Documentation

Wally Package Updated - 0.3.0 → 0.3.1


2 Likes

v0.3.2 - Major Improvements to Frictional Forces

Linked Pull Request - #4

  • Fixed a bug where changes to physical properties before creating any RigidBodies, Constraints or Points won’t affect/apply to newly created objects.
  • Friction only applies if RigidBodies collide with each other or the edges of the canvas. If none of those conditions are true, AirFriction is applied.
  • Added AirFriction physical property to Engine, Points and RigidBodies. Frictional force applied when a RigidBody neither touches another body nor the edges of canvas.
    • Engine:SetPhysicalProperty("AirFriction", 0.1)
  • Friction and AirFriction are set to 0.01 by default. (0.99 damping value).
  • Changed how we pass parameters when initializing or updating friction of the engine or rigidbodies. The closer the friction to 1, the higher it is. The closer the friction to 0, the lower it is. Same applies for AirFriction. Values not in the range of 0-1 are automatically clamped down.
  • Added new Methods to RigidBodies
    • RigidBody:SetAirFriction(airfriction: number)

Updated Roblox Asset & Github

Updated Documentation

Updated Wally Package - 0.3.1 → 0.3.2


1 Like

v0.3.4 - Anchor Point Support

Linked pull request - #5

Something that I have been wanting to add to RigidBodies, is the flexibility of having any anchor point of the frame and still be able to produce the best simulations. Earlier this was not the case, you could only have the anchor point of 0, 0. Now you can have any anchor point, and rigidbodies will still work as expected!

  • Added Anchor point support
  • Added new methods to RigidBodies
    • RigidBody:GetCenter()
  • Added new methods to Points
    • Point:SetPosition(newPosition: Vector2)
  • Bug fixes
    • Fixed Parent hierarchy errors in Points.
    • Fixed Point:Update() “Cannot read property ‘Parent’ of nil” errors.
    • Fixed Point:KeepInCanvas() “Cannot read property ‘Parent’ of nil” errors.

Roblox Asset & Github Updated

Updated Documentation

Updated Wally Package - 0.3.2 → 0.3.3 → 0.3.4


1 Like