-
Notifications
You must be signed in to change notification settings - Fork 1
/
simulation.lua
201 lines (158 loc) · 5.8 KB
/
simulation.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
--[[
simulation.lua
Simple N-Body Iterative Simulation in Roblox.
You can change the speed of the simulation via
this script's attributes.
Copyright 2021 (c) Owen Bartolf
Licensed under GNU General Public License v3
https://opensource.org/licenses/GPL-3.0
--]]
local RunService = game:GetService("RunService")
-------------------------------------------------------------------------------------------------
-- CONFIGURATION
-------------------------------------------------------------------------------------------------
local SIMULATION_TIME_MULTIPLIER = 4
local G = 1
local NUMBER_OF_OBJECTS = 12
local PART_RADIUS = 0.2
local MIN_EXTENTS = -50
local MAX_EXTENTS = 50
local RANDOM_VELOCITY_RANGE = 0.7
-------------------------------------------------------------------------------------------------
--[[
The ObjectWithMass object represents a single particle with a position, mass, and velocity.
It is the foundation of the simulation.
--]]
-------------------------------------------------------------------------------------------------
local ObjectWithMass = {}
ObjectWithMass.__index = ObjectWithMass
--[[
Standard Constructor
--]]
function ObjectWithMass.new(mass: number, position: Vector3, velocity: Vector3, part: BasePart)
local self = setmetatable(
{
mass = mass,
position = position,
velocity = velocity,
part = part
},
ObjectWithMass
)
return self
end
-------------------------------------------------------------------------------------------------
-- HELPER FUNCTIONS
-------------------------------------------------------------------------------------------------
--[[
Initializes the simulation.
--]]
function InitializeObjects(numberOfObjects)
local randomGenerator = Random.new(math.random(0, 100000))
local randomMass = {
2,
5,
7
}
local randomMaterialByMass = {
Enum.Material.Plastic,
Enum.Material.Wood,
Enum.Material.DiamondPlate
}
local toReturn = table.create(numberOfObjects)
for i = 1, NUMBER_OF_OBJECTS do
local random = randomGenerator:NextInteger(1, 3)
local mass = randomMass[random]
local randomPos = Vector3.new(
randomGenerator:NextNumber(MIN_EXTENTS, MAX_EXTENTS),
randomGenerator:NextNumber(MIN_EXTENTS, MAX_EXTENTS),
randomGenerator:NextNumber(MIN_EXTENTS, MAX_EXTENTS)
)
local randomVel = Vector3.new(
randomGenerator:NextNumber(-RANDOM_VELOCITY_RANGE, RANDOM_VELOCITY_RANGE),
randomGenerator:NextNumber(-RANDOM_VELOCITY_RANGE, RANDOM_VELOCITY_RANGE),
randomGenerator:NextNumber(-RANDOM_VELOCITY_RANGE, RANDOM_VELOCITY_RANGE)
)
local randomColor = Color3.fromHSV((i-1)/NUMBER_OF_OBJECTS, 1, 1)
-- Initialize part and trail
local newPlanetPart = Instance.new("Part")
newPlanetPart.Name = "Planet"
newPlanetPart.Material = randomMaterialByMass[random]
newPlanetPart.Size = Vector3.new(PART_RADIUS*2,PART_RADIUS*2,PART_RADIUS*2)
newPlanetPart.Shape = Enum.PartType.Ball
newPlanetPart.Position = randomPos
newPlanetPart.Anchored = true
newPlanetPart.Color = randomColor
newPlanetPart.Parent = game.Workspace
local trailAttachment0 = Instance.new("Attachment")
trailAttachment0.Axis = newPlanetPart.CFrame.LookVector
trailAttachment0.Position = Vector3.new(PART_RADIUS,0,0)
local trailAttachment1 = Instance.new("Attachment")
trailAttachment1.Axis = newPlanetPart.CFrame.LookVector
trailAttachment1.Position = Vector3.new(-PART_RADIUS,0,0)
local trail = Instance.new("Trail")
trail.Attachment0 = trailAttachment0
trail.Attachment1 = trailAttachment1
trail.Color = ColorSequence.new(randomColor, randomColor)
trail.Lifetime = math.huge
trail.MaxLength = math.huge
trail.FaceCamera = true
trailAttachment0.Parent = newPlanetPart
trailAttachment1.Parent = newPlanetPart
trail.Parent = newPlanetPart
-- Add to return table
toReturn[i] = ObjectWithMass.new(mass, randomPos, randomVel, newPlanetPart)
end
return toReturn
end
--[[
Applies gravity between the bodies. Force is equal and opposite.
--]]
function ApplyGravity(objectWithMassA, objectWithMassB, deltaTime)
-- Force of Gravity
local directionVector = objectWithMassB.position - objectWithMassA.position
local directionNorm = directionVector.Unit
local dist = directionVector.Magnitude
-- This kinda breaks the laws of physics, but clamp the distance to a minimum of
-- double the radius of the particles. Any less implies the particles are phasing through one
-- another.
dist = math.clamp(dist, 2*PART_RADIUS, math.huge)
local forceMagnitude = G * ((objectWithMassA.mass * objectWithMassB.mass) / (dist * dist))
-- Calculate acceleration due to gravity
local accelerationA = (forceMagnitude / objectWithMassA.mass) -- m/s^2
local accelerationB = (forceMagnitude / objectWithMassB.mass)
-- Apply acceleration
objectWithMassA.velocity += directionNorm * (accelerationA * deltaTime)
objectWithMassB.velocity -= directionNorm * (accelerationB * deltaTime)
end
--[[
Initializes attributes for easy manipulation of configuration values.
--]]
function InitializeAttributes()
if not script:GetAttribute("TimeFactor") then
script:SetAttribute("TimeFactor", SIMULATION_TIME_MULTIPLIER)
end
script:GetAttributeChangedSignal("TimeFactor"):Connect(
function(newValue)
SIMULATION_TIME_MULTIPLIER = script:GetAttribute("TimeFactor")
end
)
end
local simulatedObjects = InitializeObjects(NUMBER_OF_OBJECTS)
InitializeAttributes()
RunService.Heartbeat:Connect(
function(deltaTime)
deltaTime *= SIMULATION_TIME_MULTIPLIER
for i = 1, #simulatedObjects - 1 do
for j = i+1, #simulatedObjects do
-- Apply gravity if and only if
ApplyGravity(simulatedObjects[i], simulatedObjects[j], deltaTime)
end
end
-- Translate planets by velocity
for _, planet in pairs(simulatedObjects) do
planet.position += planet.velocity * deltaTime
planet.part.Position = planet.position
end
end
)