73 lines
1.5 KiB
Lua
73 lines
1.5 KiB
Lua
BodyList = {}
|
|
Body = class(nil,{type=0})
|
|
|
|
function newBody(pos,vel,mass,color)
|
|
local o = {}
|
|
|
|
-- coordinates
|
|
o.x = pos[1] or 0
|
|
o.y = pos[2] or 0
|
|
o.z = pos[3] or 0
|
|
-- velocities
|
|
o.vx = vel[1] or 0
|
|
o.vy = vel[2] or 0
|
|
o.vz = vel[3] or 0
|
|
-- accceleration
|
|
o.ax = 0
|
|
o.ay = 0
|
|
o.az = 0
|
|
-- mass
|
|
o.mass = mass
|
|
-- color
|
|
o.color = color
|
|
o.max_distance = 0
|
|
table.insert(BodyList,o)
|
|
end
|
|
|
|
function UpdateBodiesAcceleration()
|
|
for n, body in pairs(BodyList) do
|
|
local ax = 0
|
|
local ay = 0
|
|
local az = 0
|
|
for o_n, other_body in pairs(BodyList) do
|
|
if n ~= o_n then
|
|
|
|
local dx = other_body.x - body.x
|
|
local dy = other_body.y - body.y
|
|
local dz = other_body.z - body.z
|
|
|
|
local d3 = dx*dx + dy*dy + dz*dz
|
|
if math.sqrt(d3)/3 > body.max_distance then body.max_distance = math.sqrt(d3)/3 end
|
|
|
|
local f =
|
|
(CONST_G * other_body.mass) /
|
|
d3 * math.sqrt(d3 + CONST_S)
|
|
|
|
ax = ax + dx * f
|
|
ay = ay + dy * f
|
|
az = az + dz * f
|
|
end
|
|
end
|
|
|
|
body.ax = ax
|
|
body.ay = ay
|
|
body.az = az
|
|
end
|
|
end
|
|
|
|
function UpdateBodiesVelocity(dt)
|
|
for _, body in pairs(BodyList) do
|
|
body.vx = body.vx + body.ax * dt
|
|
body.vy = body.vy + body.ay * dt
|
|
body.vz = body.vz + body.az * dt
|
|
end
|
|
end
|
|
|
|
function UpdateBodiesPosition(dt)
|
|
for _, body in pairs(BodyList) do
|
|
body.x = body.x + body.vx * dt
|
|
body.y = body.y + body.vy * dt
|
|
body.z = body.z + body.vz * dt
|
|
end
|
|
end
|