@quenty/physicsutils 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/LICENSE.md +1 -1
- package/README.md +6 -30
- package/package.json +2 -2
- package/src/Shared/PhysicsUtils.lua +71 -25
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [2.0.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/physicsutils@2.0.0...@quenty/physicsutils@2.0.1) (2021-12-30)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @quenty/physicsutils
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
# [2.0.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/physicsutils@1.2.0...@quenty/physicsutils@2.0.0) (2021-09-05)
|
|
7
15
|
|
|
8
16
|
|
package/LICENSE.md
CHANGED
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
## PhysicsUtils
|
|
2
2
|
<div align="center">
|
|
3
|
-
<a href="http://quenty.github.io/
|
|
4
|
-
<img src="https://
|
|
3
|
+
<a href="http://quenty.github.io/NevermoreEngine/">
|
|
4
|
+
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
|
|
5
5
|
</a>
|
|
6
6
|
<a href="https://discord.gg/mhtGUS8">
|
|
7
|
-
<img src="https://img.shields.io/
|
|
7
|
+
<img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
|
|
8
8
|
</a>
|
|
9
9
|
<a href="https://github.com/Quenty/NevermoreEngine/actions">
|
|
10
10
|
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
|
|
@@ -13,33 +13,9 @@
|
|
|
13
13
|
|
|
14
14
|
General physics library for use on Roblox
|
|
15
15
|
|
|
16
|
+
<div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/PhysicsUtils">View docs →</a></div>
|
|
17
|
+
|
|
16
18
|
## Installation
|
|
17
19
|
```
|
|
18
20
|
npm install @quenty/physicsutils --save
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Usage
|
|
22
|
-
Usage is designed to be simple.
|
|
23
|
-
|
|
24
|
-
### `PhysicsUtils.getConnectedParts(part)`
|
|
25
|
-
Retrieves all connected parts of a part, plus the connected part
|
|
26
|
-
|
|
27
|
-
### `PhysicsUtils.getMass(parts)`
|
|
28
|
-
|
|
29
|
-
### `PhysicsUtils.estimateBuoyancyContribution(parts)`
|
|
30
|
-
Estimate buoyancy contributed by parts
|
|
31
|
-
|
|
32
|
-
### `PhysicsUtils.getCenterOfMass(parts)`
|
|
33
|
-
Return's the world vector center of mass.
|
|
34
|
-
|
|
35
|
-
### `PhysicsUtils.momentOfInertia(part, axis, origin)`
|
|
36
|
-
Calculates the moment of inertia of a solid cuboid. This is wrong for Roblox.
|
|
37
|
-
|
|
38
|
-
### `PhysicsUtils.bodyMomentOfInertia(parts, axis, origin)`
|
|
39
|
-
Given a connected body of parts, returns the moment of inertia of these parts
|
|
40
|
-
|
|
41
|
-
### `PhysicsUtils.applyForce(part, force, forcePosition)`
|
|
42
|
-
|
|
43
|
-
### `PhysicsUtils.acceleratePart(part, emittingPart, acceleration)`
|
|
44
|
-
Accelerates a part utilizing newton's laws. emittingPart is the part it's emitted from.
|
|
45
|
-
|
|
21
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/physicsutils",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "General physics library for use on Roblox",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -27,5 +27,5 @@
|
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "d146c77d0a8e452824de0ab0b4b03ba0370bcc1b"
|
|
31
31
|
}
|
|
@@ -1,18 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
--[=[
|
|
2
|
+
General physics library for use on Roblox
|
|
3
|
+
@class PhysicsUtils
|
|
4
|
+
]=]
|
|
3
5
|
|
|
4
6
|
local Workspace = game:GetService("Workspace")
|
|
5
7
|
|
|
6
8
|
local PhysicsUtils = {}
|
|
7
9
|
PhysicsUtils.WATER_DENSITY = 1 -- (mass/volume)
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
--[=[
|
|
12
|
+
Retrieves all connected parts of a part, plus the connected part.
|
|
13
|
+
@param part BasePart
|
|
14
|
+
@return { BasePart }
|
|
15
|
+
]=]
|
|
10
16
|
function PhysicsUtils.getConnectedParts(part)
|
|
11
17
|
local parts = part:GetConnectedParts(true)
|
|
12
18
|
parts[#parts+1] = part
|
|
13
19
|
return parts
|
|
14
20
|
end
|
|
15
21
|
|
|
22
|
+
--[=[
|
|
23
|
+
Retrieves mass of all parts
|
|
24
|
+
@param parts { BasePart }
|
|
25
|
+
@return number
|
|
26
|
+
]=]
|
|
16
27
|
function PhysicsUtils.getMass(parts)
|
|
17
28
|
local mass = 0
|
|
18
29
|
for _, part in pairs(parts) do
|
|
@@ -21,7 +32,13 @@ function PhysicsUtils.getMass(parts)
|
|
|
21
32
|
return mass
|
|
22
33
|
end
|
|
23
34
|
|
|
24
|
-
|
|
35
|
+
--[=[
|
|
36
|
+
Estimate buoyancy contributed by parts
|
|
37
|
+
@param parts { BasePart }
|
|
38
|
+
@return number -- buoyancy
|
|
39
|
+
@return number -- mass
|
|
40
|
+
@return number -- volume
|
|
41
|
+
]=]
|
|
25
42
|
function PhysicsUtils.estimateBuoyancyContribution(parts)
|
|
26
43
|
local totalMass = 0
|
|
27
44
|
local totalVolumeApplicable = 0
|
|
@@ -42,14 +59,17 @@ function PhysicsUtils.estimateBuoyancyContribution(parts)
|
|
|
42
59
|
return totalFloat, totalMass, totalVolumeApplicable
|
|
43
60
|
end
|
|
44
61
|
|
|
45
|
-
|
|
46
|
-
|
|
62
|
+
--[=[
|
|
63
|
+
Return's the world vector center of mass.
|
|
64
|
+
@param parts { BasePart }
|
|
65
|
+
@return Vector3 -- position
|
|
66
|
+
@return number -- mass
|
|
67
|
+
]=]
|
|
47
68
|
function PhysicsUtils.getCenterOfMass(parts)
|
|
48
69
|
local mass = 0
|
|
49
70
|
local weightedSum = Vector3.new(0, 0, 0)
|
|
50
71
|
|
|
51
72
|
for _, part in pairs(parts) do
|
|
52
|
-
-- part.BrickColor = BrickColor.new("Bright yellow")
|
|
53
73
|
mass = mass + part:GetMass()
|
|
54
74
|
weightedSum = weightedSum + part:GetMass() * part.Position
|
|
55
75
|
end
|
|
@@ -57,10 +77,18 @@ function PhysicsUtils.getCenterOfMass(parts)
|
|
|
57
77
|
return weightedSum/mass, mass
|
|
58
78
|
end
|
|
59
79
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
80
|
+
--[=[
|
|
81
|
+
Calculates the moment of inertia of a solid cuboid.
|
|
82
|
+
|
|
83
|
+
:::warning
|
|
84
|
+
This is wrong for Roblox. Roblox has hollow cuvoids as parts
|
|
85
|
+
:::
|
|
86
|
+
|
|
87
|
+
@param part BasePart
|
|
88
|
+
@param axis Vector3
|
|
89
|
+
@param origin Vector3
|
|
90
|
+
@return number
|
|
91
|
+
]=]
|
|
64
92
|
function PhysicsUtils.momentOfInertia(part, axis, origin)
|
|
65
93
|
local size = part.Size
|
|
66
94
|
local position = part.Position
|
|
@@ -76,10 +104,13 @@ function PhysicsUtils.momentOfInertia(part, axis, origin)
|
|
|
76
104
|
return ip+id
|
|
77
105
|
end
|
|
78
106
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
107
|
+
--[=[
|
|
108
|
+
Given a connected body of parts, returns the moment of inertia of these parts
|
|
109
|
+
@param parts The parts to use
|
|
110
|
+
@param axis the axis to use (Should be torque, or offset cross force)
|
|
111
|
+
@param origin The origin of the axis (should be center of mass of the parts)
|
|
112
|
+
@return number
|
|
113
|
+
]=]
|
|
83
114
|
function PhysicsUtils.bodyMomentOfInertia(parts, axis, origin)
|
|
84
115
|
local totalBodyInertia = 0
|
|
85
116
|
|
|
@@ -90,14 +121,23 @@ function PhysicsUtils.bodyMomentOfInertia(parts, axis, origin)
|
|
|
90
121
|
return totalBodyInertia
|
|
91
122
|
end
|
|
92
123
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
124
|
+
--[=[
|
|
125
|
+
Applies a force to a Roblox body.
|
|
126
|
+
|
|
127
|
+
:::tip
|
|
128
|
+
Roblox has :ApplyImpulse now as an API surface, so I recommend using that
|
|
129
|
+
instead.
|
|
130
|
+
:::
|
|
131
|
+
|
|
132
|
+
It should be noted that setting the velocity to one part of a connected part on Roblox sets
|
|
133
|
+
the velocity of the whole physics model.
|
|
134
|
+
http://xboxforums.create.msdn.com/forums/p/34179/196459.aspx
|
|
135
|
+
http://www.cs.cmu.edu/~baraff/sigcourse/notesd1.pdf
|
|
136
|
+
|
|
137
|
+
@param part BasePart
|
|
138
|
+
@param force Vector3 -- the force vector to apply
|
|
139
|
+
@param forcePosition Vector3 -- The position that the force is to be applied from (World vector).
|
|
140
|
+
]=]
|
|
101
141
|
function PhysicsUtils.applyForce(part, force, forcePosition)
|
|
102
142
|
local parts = PhysicsUtils.getConnectedParts(part)
|
|
103
143
|
|
|
@@ -121,8 +161,14 @@ function PhysicsUtils.applyForce(part, force, forcePosition)
|
|
|
121
161
|
part.Velocity = part.Velocity + acceleration
|
|
122
162
|
end
|
|
123
163
|
|
|
124
|
-
|
|
125
|
-
|
|
164
|
+
--[=[
|
|
165
|
+
Accelerates a part utilizing newton's laws. emittingPart is the part it's emitted from.
|
|
166
|
+
force = mass * acceleration
|
|
167
|
+
|
|
168
|
+
@param part BasePart
|
|
169
|
+
@param emittingPart BasePart
|
|
170
|
+
@param acceleration Vector3
|
|
171
|
+
]=]
|
|
126
172
|
function PhysicsUtils.acceleratePart(part, emittingPart, acceleration)
|
|
127
173
|
local force = acceleration * part:GetMass()
|
|
128
174
|
local position = part.Position
|