Skip to content

Commit

Permalink
Engine:GetDebugInfo(), Engine.Updated and bug fixes (#21)
Browse files Browse the repository at this point in the history
* Added Engine:GetDebugInfo()
* Removed Engine:GetCurrentCanvas()
* Added DebugInfo type to Types.lua
* Fixed rope constraints and KeepInCanvas property of RigidBodies.
* Added new event - Engine.Updated
  • Loading branch information
jaipack17 authored Jan 4, 2022
1 parent cbfe7ac commit 8a948f9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">
<img src="https://doy2mn9upadnk.cloudfront.net/uploads/default/original/4X/e/7/0/e709b34f89add2336a7d74faee3b2a839a9391b0.png" width="480" /><br/><br/>
<a href="https://devforum.roblox.com/t/physics-library-nature2d-bring-ui-elements-to-life/1510935/75"><img alt="version" src="https://img.shields.io/badge/v0.5.5--beta-version-%231FD67F"></img></a>
<a href="https://devforum.roblox.com/t/physics-library-nature2d-bring-ui-elements-to-life/1510935/75"><img alt="version" src="https://img.shields.io/badge/v0.5.6--beta-version-%231FD67F"></img></a>
<br/>
<a href="https://devforum.roblox.com/t/physics-library-nature2d-bring-ui-elements-to-life/1510935/"><img alt="devforum" src="https://img.shields.io/badge/topic-devforum-white"></img></a>
<a href="https://www.roblox.com/library/7625799164/Nature2D"><img alt="model" src="https://img.shields.io/badge/asset-roblox-white"></img></a>
Expand Down Expand Up @@ -34,7 +34,7 @@ https://www.roblox.com/library/7625799164/Nature2D
**Using wally** - Use [wally](https://github.com/UpliftGames/wally), a package manager for roblox to install Nature2D in your external code editor. This requires wally to be installed on your device. Then, add Nature2D to the dependencies listed in your `wally.toml` file.<br/>
```toml
[dependencies]
Nature2D = "jaipack17/[email protected].5"
Nature2D = "jaipack17/[email protected].6"
```
After that, Run `wally install` in the cli. Nature2D should be installed in your root directory. If you encounter any errors or problems installing Nature2D using wally, [open an issue!](https://github.com/jaipack17/Nature2D/issues)

Expand Down
42 changes: 36 additions & 6 deletions src/Engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ function Engine.init(screengui: Instance)
Started = Signal.new(),
Stopped = Signal.new(),
ObjectAdded = Signal.new(),
ObjectRemoved = Signal.new()
ObjectRemoved = Signal.new(),
Updated = Signal.new(),
}, Engine)
end

Expand Down Expand Up @@ -212,6 +213,8 @@ function Engine:Start()
point:Render()
end
end

self.Updated:Fire()
end)

self.connection = connection
Expand Down Expand Up @@ -399,7 +402,7 @@ function Engine:Create(object: string, properties: Types.Properties)

--Apply properties
if properties.LifeSpan then newBody:SetLifeSpan(properties.LifeSpan) end
if properties.KeepInCanvas then newBody:KeepInCanvas(properties.KeepInCanvas) end
if typeof(properties.KeepInCanvas) == "boolean" then newBody:KeepInCanvas(properties.KeepInCanvas) end
if properties.Gravity then newBody:SetGravity(properties.Gravity) end
if properties.Friction then newBody:SetFriction(properties.Friction) end
if properties.AirFriction then newBody:SetAirFriction(properties.AirFriction) end
Expand Down Expand Up @@ -516,12 +519,39 @@ function Engine:GetConstraintById(id: string)
return
end

-- Returns current canvas the engine adheres to.
function Engine:GetCurrentCanvas() : Types.Canvas
return self.canvas
---- Returns current canvas the engine adheres to.
--function Engine:GetCurrentCanvas() : Types.Canvas
-- return self.canvas
--end

function Engine:GetDebugInfo() : Types.DebugInfo
return {
Objects = {
RigidBodies = #self.bodies,
Constraints = #self.constraints,
Points = #self.points
},
Running = not not (self.connection),
Physics = {
Gravity = self.gravity,
Friction = 1 - self.friction,
AirFriction = 1 - self.airfriction,
CollisionMultiplier = self.bounce,
TimeSteps = self.timeSteps,
SimulationSpeed = self.speed,
UsingQuadtrees = self.quadtrees,
FramerateIndependent = self.independent
},
Path = self.path,
Canvas = {
Frame = self.canvas.frame,
TopLeft = self.canvas.topLeft,
Size = self.canvas.size
}
}
end

-- Determines if Quadtrees will be used in collision detection.
-- Determines if Quadtrees will be used in collision deteWction.
-- By default this is set to false
function Engine:UseQuadtrees(use: boolean)
throwTypeError("useQuadtrees", use, 1, "boolean")
Expand Down
16 changes: 8 additions & 8 deletions src/Physics/Constraint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ function Constraint:Constrain()

-- Validate constraint types
if self._TYPE == "ROPE" then
local restLength
if cur > self.restLength then
restLength = self.restLength
elseif cur < self.thickness then
local restLength = self.restLength
if cur < self.thickness then
restLength = self.thickness
end

-- Solve rope constraint force
local offset = ((restLength - cur)/restLength)/2
force = self.point2.pos - self.point1.pos
force *= offset
if cur > self.restLength or self.restLength < self.thickness then
-- Solve rope constraint force
local offset = ((restLength - cur)/restLength)/2
force = self.point2.pos - self.point1.pos
force *= offset
end
elseif self._TYPE == "ROD" then
-- Solve rod constraint force
local offset = self.restLength - cur
Expand Down
34 changes: 33 additions & 1 deletion src/Types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,43 @@ export type Properties = {
AirFriction: number?,
Structure: {}?,
Mass: number?,
CanTouch: boolean?
}

export type Custom = {
Vertices: { any },
Edges: { any }
}

return nil
export type Plugins = {
Triangle: (a: Vector2, b: Vector2, c: Vector2) -> (),
Quad: (a: Vector2, b: Vector2, c: Vector2, d: Vector2) -> (),
MouseConstraint: (engine: { any }, range: number, rigidbodies: { any }) -> ()
}

export type DebugInfo = {
Objects: {
RigidBodies: number,
Constraints: number,
Points: number
},
Running: boolean,
Physics: {
Gravity: Vector2,
Friction: number,
AirFriction: number,
CollisionMultiplier: number,
TimeSteps: number,
SimulationSpeed: number,
UsingQuadtrees: boolean,
FramerateIndependent: boolean
},
Path: ScreenGui,
Canvas: {
Frame: GuiObject,
TopLeft: Vector2,
Size: Vector2
}
}

return nil
2 changes: 1 addition & 1 deletion wally.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "jaipack17/nature2d"
description = "Create versatile physics simulations and mechanics with Guis on Roblox!"
authors = ["jaipack17"]
version = "0.5.5"
version = "0.5.6"
license = "MIT"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
Expand Down

0 comments on commit 8a948f9

Please sign in to comment.