Releases: jaipack17/Nature2D
Fixed Physics Timestep, Bug Fixes & Additions
- Added
Runner.lua
insrc/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 - https://jaipack17.github.io/Nature2D/docs/api/Plugins
Proper Garbage Collection
Implemented proper garbage collection in the library for the Engine and all its Physics Objects to reduce chances of memory leaks in key areas. All problems listed in #33 have been dealt with.
- Added Janitor to
src/Utilities
- Implemented garbage collection in Engine.lua
- Handled how connections are disconnected.
Engine:Stop()
no longer destroys connections of the Engine (Updated, Started, Stopped, ObjectAdded, ObjectRemoved). It simply pauses the engine by disconnecting the primary RenderStepped connection.- Added
Engine:Destroy()
- Disconnects all events, destroys all rigid bodies, points and constraints and also makes the engine unusable.
- Implemented garbage collection in Point.lua
- Added
Point:Destroy()
- Destroys the point's GuiObject, destroys the point's parent constraint (if any), and is no longer part of the engine.
- Added
- Re-wrote garbage collection in Constraint.lua
- Re-implemented
Constraint:Destroy()
- Destroys the constrain't GuiObject, destroys the constraint's parent rigid body (if any), destroys its points (point1 and point2), and is no longer part of the engine.
- Re-implemented
- Re-wrote garbage collection in RigidBody.lua
- Re-implemented
RigidBody:Destroy()
- Destroys all events (Touched, TouchEnded, CanvasEdgeTouched), destroys its GuiObject, destroys its constraints (edges) which subsequently destroys all its points (vertices), and is no longer part of the engine. - Destroying the GuiObject of the rigid body via
Instance:Destroy()
will triggerRigidBody:Destroy()
. - Destroying an edge of the rigid body via
Constraint:Destroy()
will triggerRigidBody:Destroy()
. - Destroying a vertex of the rigid body via
Point:Destroy()
will triggerRigidBody:Destroy()
.
- Re-implemented
Bugs Introduced by these Changes
RigidBody:Destroy()
'skeepFrame: boolean
parameter no longer determines if the GuiObject of the rigid body is destroyed when the method is called. Passing it in as true will still end up destroying the GuiObject. This bug shall be fixed in v0.7.1 along with bug #37.
Custom Rigid Body Manipulation
Custom Rigid Body Manipulation
- Removed restrictions for custom rigid bodies.
RigidBody:Rotate()
RigidBody:SetPosition()
- Optimized rigid bodies with CanRotate set to false (Avoided 2 loops in
RigidBody:Update()
andRigidBody:Anchor()
). - Added
RigidBody:SetScale()
method as an alternative toRigidBody:SetSize()
but only for custom rigid bodies.RigidBody:SetScale(scale: number)
- The scale of the default size is 1. Passing in 2 as the scale will double the size of the custom rigid body, etc. Similar to how UDim's scale property works.
RigidBody:SetPosition()
now works with custom rigid bodies.RigidBody:Rotate()
now works with custom rigid bodies.- Fixed bug in
RigidBody:SetSize()
. EarlierRigidBody:SetSize()
changed only the size of the GuiObject and not the point-constraint structure.
CanRotate Property, UniversalMass and changes to Friction
- Change default friction and airfriction to 0.1 and 0.02.
- Add CanRotate property as a valid property.
- Set CanRotate to true by default.
- Add new methods to RigidBodies
RigidBody:CanRotate(canRotate: boolean)
- Determines if a rigid body can rotate after collisions or when forces are applied, extremely useful for creating platformer games, over the head games etc.
- Update RigidBody:Update() and Engine:Create() to adhere to CanRotate
- Add UniversalMass as a valid physical property of the Engine. By default set to 1
- Engine:SetPhysicalProperty("UniversalMass", 5)
Collision and Constraint Iterations
- Added constraint and collision iterations and their functionality.
- Added new methods to Engine
Engine:SetConstraintIterations(iterations: number)
Engine:SetCollisionIterations(iterations: number)
- Updated RigidBody:Update() to use constraint iterations.
- Fixed RigidBody.Touched and RigidBody.TouchEnded after adding iterations.
- Fixed MouseConstraint plugin bug - Stop looping through the rigid bodies if we have already found one to attach to the mouse.
Iterations provide accurate calculations for more rigid and smoother physics. Constraint iterations are applied to Constraint:Constrain()
method. Constraint iterations are extremely useful of rod constraints and rope constraints. Constraint iterations do not work on spring constraints.
Collision iterations are used to provide accurate and rigid collision detection and resolution. By default both of these iterations are set to 1. Iterations can be in the range of 1-10 only. Collision iterations can be set only if quadtrees are being used in collision detection.
Keep in mind that the higher the number of iterations the more accurate results. But, having more iterations means you'll have to sacrifice performance. The lesser the number of iterations, the better performance but we'll have to sacrifice on accuracy. So be careful where you use them!
Recommended Iteration Amounts:
Constraint Iterations - 3
Collision Iterations - 4
Bug fixes, Improvements to Physics Objects and Error Messages
Bug fixes, Improvements to Physics Objects and Error Messages
- "CanTouch" is not longer a valid property for rigid bodies.
- Fixed bug where Point:KeepInCanvas() won't calculate collisions accurately.
- RigidBody.CanvasEdgeTouched event now fires only the moment a rigid body collides with the canvas' edge and not every rendered frame.
- Improved exception handling code.
- Improved error messages for
Engine:Create()
- If an invalid property is specified, the error message will now contain the name of the invalid property you specified for debugging.
- If properties for completely different physics objects are specified, the error message will now contain the name of the invalid property you specified.
- If a must-have property is not specified, the error message will now contain the name of that must-have property.
- RigidBody.Touched event now returns the rigid body's id as well as the collision information (Collision axis, depth, vertex and edge). This can be beneficial for creating visual effects. For example:
Clone() for Custom Rigid Bodies
- RigidBody:Clone() now works for custom rigid bodies.
- Added Structure parameter to RigidBody.new() to cache the rigid body's structure for the future.
- Remove restrictions from RigidBody:Clone()
Optimizations
- Optimizations to how Rigid bodies are updated. There was a flaw earlier, I was using 2 for loops for the same task but in different locations. This has been cut down to just 1 loop.
- Extra work for non-collidable rigid bodies is not longer performed. Thanks to @boatbomber for the PR.
- Events like .Touched and .TouchEnded are fired only if the events are connected using :Connect() somewhere.
- Prevent memory leaks by:
- Disconnecting all Engine events when Engine:Stop() is called.
- Warning and returning from Engine:Start() if its already running.
- Disconnecting and destroying RigidBody.TouchEnded event when RigidBody:Destoy() is called.
Engine:GetDebugInfo(), Engine.Updated and bug fixes
- Added
Engine:GetDebugInfo()
- Removed
Engine:GetCurrentCanvas()
- Fixed rope constraints and KeepInCanvas property of RigidBodies.
- Added new event
Engine.Updated