Skip to content

Commit

Permalink
CanRotate Property, UniversalMass and changes to Friction (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaipack17 authored Feb 2, 2022
1 parent 15e03e8 commit f18ab2f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 21 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://github.com/jaipack17/Nature2D/releases"><img alt="version" src="https://img.shields.io/badge/v0.6.3--beta-version-%231FD67F"></img></a>
<a href="https://github.com/jaipack17/Nature2D/releases"><img alt="version" src="https://img.shields.io/badge/v0.6.4--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 @@ -37,7 +37,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].3"
Nature2D = "jaipack17/[email protected].4"
```
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
14 changes: 9 additions & 5 deletions src/Constants/Globals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
return {
engineInit = {
gravity = Vector2.new(0, .3),
friction = 0.99,
airfriction = 0.99,
friction = 0.9,
airfriction = 0.98,
bounce = 0.8,
timeSteps = 1,
canvas = {
Expand All @@ -19,6 +19,7 @@ return {
"friction",
"collisionmultiplier",
"airfriction",
"universalmass"
},
rigidbody = {
props = {
Expand All @@ -31,7 +32,8 @@ return {
"Friction",
"AirFriction",
"Structure",
"Mass"
"Mass",
"CanRotate"
},
must_have = {
"Object"
Expand Down Expand Up @@ -100,7 +102,8 @@ return {
"Friction",
"AirFriction",
"Structure",
"Mass"
"Mass",
"CanRotate"
},

OBJECT_PROPS_TYPES = {
Expand All @@ -122,6 +125,7 @@ return {
Friction = "number",
AirFriction = "number",
Structure = "table",
Mass = "number"
Mass = "number",
CanRotate = "boolean"
},
}
12 changes: 6 additions & 6 deletions src/Engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function Engine.init(screengui: Instance)
airfriction = Globals.engineInit.airfriction,
bounce = Globals.engineInit.bounce,
timeSteps = Globals.engineInit.timeSteps,
mass = Globals.universalMass,
path = screengui,
speed = Globals.speed,
quadtrees = false,
Expand Down Expand Up @@ -432,7 +433,7 @@ function Engine:Create(object: string, properties: Types.Properties)

local newBody = RigidBody.new(
obj,
properties.Mass or Globals.universalMass,
properties.Mass or self.mass,
properties.Collidable,
properties.Anchored,
self,
Expand All @@ -446,6 +447,7 @@ function Engine:Create(object: string, properties: Types.Properties)
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
if typeof(properties.CanRotate) == "boolean" and not properties.Structure then newBody:CanRotate(properties.CanRotate) end

table.insert(self.bodies, newBody)
newObject = newBody
Expand Down Expand Up @@ -513,6 +515,9 @@ function Engine:SetPhysicalProperty(property: string, value: Vector2 | number)
elseif string.lower(property) == "airfriction" then
throwTypeError("value", value, 2, "number")
object.airfriction = math.clamp(1 - value, 0, 1)
elseif string.lower(property) == "universalmass" then
throwTypeError("value", value, 2, "number")
object.mass = math.max(0, value)
end
end

Expand Down Expand Up @@ -559,11 +564,6 @@ function Engine:GetConstraintById(id: string)
return
end

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

function Engine:GetDebugInfo() : Types.DebugInfo
return {
Objects = {
Expand Down
42 changes: 36 additions & 6 deletions src/Physics/RigidBody.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ function RigidBody.new(frame: GuiObject?, m: number, collidable: boolean?, ancho
anchored = anchored,
mass = m,
collidable = collidable,
canRotate = true,
rotationCache = {},
center = isCustom and CalculateCenter(vertices) or frame.AbsolutePosition + frame.AbsoluteSize/2,
engine = engine,
spawnedAt = os.clock(),
Expand Down Expand Up @@ -324,14 +326,24 @@ end
-- This method updates the positions of the RigidBody's points and constraints.
function RigidBody:Update(dt: number)
self.center = CalculateCenter(self.vertices)

for _, vertex in ipairs(self.vertices) do

if not self.canRotate then
for i, info in ipairs(self.rotationCache) do
local r = info[1]
local t = info[2]
local v = self.vertices[i]

v:ApplyForce((self.center + Vector2.new(math.cos(t), math.sin(t)) * r) - v.pos)
end
end

for _, vertex in ipairs(self.vertices) do
vertex:Update(dt)
vertex:Render()
end

for _, edge in ipairs(self.edges) do
for i = 1, self.engine.iterations.constraint do
for i = 1, self.engine.iterations.constraint do
edge:Constrain()
end
edge:Render()
Expand All @@ -353,13 +365,17 @@ function RigidBody:Render()
if self.anchored then
local anchorPos = self.anchorPos - CalculateOffset(self.anchorPos, self.frame.AnchorPoint, self.frame.AbsoluteSize)
self.frame.Position = UDim2.fromOffset(anchorPos.X, anchorPos.Y)
self:Rotate(self.anchorRotation)
if self.canRotate then
self:Rotate(self.anchorRotation)
end
else
local center = self.center - CalculateOffset(self.center, self.frame.AnchorPoint, self.frame.AbsoluteSize)
local dif: Vector2 = self.vertices[2].pos - self.vertices[1].pos

self.frame.Position = UDim2.new(0, center.X, 0, center.Y)
self.frame.Rotation = math.deg(math.atan2(dif.Y, dif.X))
if self.canRotate then
self.frame.Rotation = math.deg(math.atan2(dif.Y, dif.X))
end
end
end

Expand Down Expand Up @@ -516,6 +532,20 @@ function RigidBody:CanCollide(collidable: boolean)
self.collidable = collidable
end

function RigidBody:CanRotate(canRotate: boolean)
restrict(self.custom)
throwTypeError("canRotate", canRotate, 1, "boolean")
self.canRotate = canRotate

if not self.canRotate then
for _, p in ipairs(self.vertices) do
local r = (p.pos - self.center).Magnitude
local theta = math.atan2(p.pos.Y - self.center.Y, p.pos.X - self.center.X)
table.insert(self.rotationCache, { r, theta })
end
end
end

-- The RigidBody's UI Element can be fetched using this method.
function RigidBody:GetFrame() : GuiObject
return self.frame
Expand Down
3 changes: 2 additions & 1 deletion src/Types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export type Properties = {
AirFriction: number?,
Structure: {}?,
Mass: number?,
CanRotate: boolean
}

export type Custom = {
Expand Down Expand Up @@ -151,4 +152,4 @@ export type DebugInfo = {
}
}

return nil
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.6.3"
version = "0.6.4"
license = "MIT"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
Expand Down

0 comments on commit f18ab2f

Please sign in to comment.