Nature2D - 2D Physics Engine for UI Elements

If you run into any bugs or find any flaws in my implementation - open an issue at the github repository. I’ll be revisiting the documentation in the following days and make the necessary changes w.r.t v0.6.5 and v0.7.0!

I’m not really sure if this error is caused by my engine, however as of lately, I upgraded Nature2D to 0.7.0

However this update is causing me an error, that I’m not sure why it’s happening.
This happens when I start Nature2D.

Would it be possible for you to share parts of the code where you deal with rigid bodies (in dms)? Are you by chance using Plugins.MouseConstraint() somewhere? Since v0.7.0 introduced a bug in that plugin.

This error usually pops up when a rigid body is destroyed during collision detection (which is very unlikely unless it is destroyed by a part of Nature2D’s code)

No, I’m not using Plugins.MouseConstraint() anywhere in my code, I’ll update the repo to the updated version.

1 Like

v0.7.1 - Fixed Physics Timestep, Bug Fixes & Additions

Linked Pull Request - #38

  • Added Runner.lua in src/Physics
  • The physics stepping rate has been set to 60hz. No matter the frame rate, the physics will still be calculated at 60 calculations per second. This can be turned off (if required) using already implemented methods like Engine:FrameRateIndependent(). This fixes issues with fps unlockers and provides stable simulations at higher frame rates. Fixes issue #37.
  • Fixed MouseConstraint bug where lifting your mouse button would destroy the rigid body that was held.
  • MouseConstraint now returns a callback which can be called to disconnect all events connected when Plugin.MouseConstraint() was ran.
  • Added new methods to Points
    • Point:GetNetForce() - Returns the net force acting on the point.
  • Updated documentation for the previous and current versions.
  • Added Plugins to the API documentation - Plugins | Nature2D

Updated Roblox Asset & Github

Updated Documentation

Updated Wally Package - 0.7.0 → 0.7.1


4 Likes

lol so i am a begginer and dont know anything about scripting, but when i tried to follow the turial
i dont know where to put al of this
how to definea gui as a rigidbody?
where to put Engine:Create()?

Been a long time since I worked on an update. Here’s a sneak peek of what I’m working on for the next update! Hinge Joints! No where near complete but hopefully I can get this out to you this week.

3 Likes

Hey, this is some really cool stuff! Could you open source the demos included in the posts? I would like to see how you did all of them since I am really interested in this project and not all of them were shown on the website’s tutorials.

1 Like

Sure, I’ll update the documentation today to include all demos.

1 Like

Also, quick question, how could I make a rigid body draggable? I tried setting the position to the mouse continuously but that flickers the box around.

Check out this link that contains a way to create draggable rigid bodies (MouseConstraint)! It’ll help you hold onto the corners of the rigid bodies to move them.

https://jaipack17.github.io/Nature2D/docs/tutorial-basics/Using%20Nature2D%20Plugins

Also in your code, instead of updating .Position property of the GuiObject, use RigidBody:SetPosition(mouseX, mouseY) instead!

Yes, I just saw that, sorry to bother!

1 Like

Okay, sorry to bother again, but I think I got it set up correctly so far but it throws these errors:

local UIS = game:GetService('UserInputService')
local Constraint

UIS.InputBegan:Connect(function(i,g)
	if not g and i.UserInputType == Enum.UserInputType.MouseButton1 then
		Constraint = N2DPlugins.MouseConstraint(Nature2D, 1)
	end
end)

UIS.InputEnded:Connect(function(i,g)
	if not g and i.UserInputType == Enum.UserInputType.MouseButton1 then
		if Constraint then
			Constraint()
		end
	end
end)

Did I make a mistake somewhere?

All you need to do is add the following line anywhere in your code :slight_smile:

-- The first parameter is the engine (using which you create all the physical elements i.e. rigid bodies)
-- Range is 10 so that it's easier to pick up a body
N2DPlugins.MouseConstraint(Engine, 10)

The second error is gone, but the first one remains and it does not work yet unfortunately. And for context I would like it so that you hold mouse button 1, and then you can drag something, instead of being able to drag anything at any time.

And would I return the engine like this:

local Nature2D = require(ReplicatedStorage:FindFirstChild("Nature2D").Engine)

Or this as mentioned in the tutorials:

local Nature2D = require(ReplicatedStorage:FindFirstChild("Nature2D"))

MouseConstraint already handles the input part. If you take a look at its source, all user input events are handled for you!

image

Both would work, the latter is the shorter one so you should go for the one in the tutorials.

1 Like

Hmmm, that’s weird then, everything seems to be correct. This is my code so far:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Nature2D = require(ReplicatedStorage:FindFirstChild("Nature2D"))
local N2DPlugins = require(ReplicatedStorage.Nature2D:FindFirstChild('Plugins'))

local ScreenGui = script.Parent
local Canvas = ScreenGui.Canvas

local engine = Nature2D.init(ScreenGui)

engine:Create("RigidBody", {
	Object = Canvas.Plane,
	Collidable = true,
	Anchored = true
})

for _, box in ipairs(Canvas.Boxes:GetChildren()) do
	local rb = engine:Create("RigidBody", {
		Object = box,
		Collidable = true,
		Anchored = false
	})
end

N2DPlugins.MouseConstraint(Nature2D, 1)

engine:Start()
N2DPlugins.MouseConstraint(Nature2D, 1)

You had to pass in the engine variable instead of the module, like so:

N2DPlugins.MouseConstraint(engine, 10)

Should work now!

1 Like

Nice, this is really cool looking! However, it has to be dragged from a certain corner, is that fixable?

That’s the intended behavior, you’d have to make a custom dragger for dragging the object from any point. RigidBody:SetPosition() will help out for that, you can lerp the position of the body to the mouse to make it smoother. Good luck!

1 Like