it works tm

This commit is contained in:
UndeadMaelys 2022-06-02 21:18:09 +02:00
parent 6afc7dc5e3
commit c92bfd7f6b
2 changed files with 97 additions and 21 deletions

View File

@ -1,21 +1,26 @@
BodyList = {}
Body = class(nil,{type=0})
function newBody(x,y,mass,color)
function newBody(pos,vel,mass,color)
local o = {}
-- coordinates
o.x = x
o.y = y
o.x = pos[1] or 0
o.y = pos[2] or 0
o.z = pos[3] or 0
-- velocities
o.vx = 0
o.vy = 0
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
@ -23,25 +28,30 @@ 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 d2 = dx*dx + dy*dy
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) /
d2 * math.sqrt(d2 + CONST_S)
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
@ -49,6 +59,7 @@ 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
@ -56,5 +67,6 @@ 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

View File

@ -12,37 +12,101 @@ function love.load()
CONST_S = 0.15
T = 0
DT = 1/120
TIME_SPEED = 0.25
VIEW = {}
VIEW.x = 0
VIEW.y = 0
VIEW.prints = {}
require "class"
require "body"
newBody(0,0,300,{1,0,0})
newBody(0,100,100,{0,1,0})
newBody(100,0,200,{0,0,1})
newBody(100,100,50,{1,1,0})
newBody(100,200,250,{1,0,1})
newBody(200,100,150,{0,1,1})
newBody(
{0, 0, 0},
{0, 0, 0},
40000,
{1,0,0}
)
newBody(
{10000, 0, 0},
{0, 100000, 0},
200,
{0,1,0}
)
newBody(
{20000, 0, 0},
{0, 100000, 0},
400,
{0,0,1}
)
newBody(
{0, 20000, 0},
{-200000, 0, 0},
3000,
{0,1,1}
)
end
function love.update(dt)
T = T + dt * TIME_SPEED
T = math.floor((T + DT * TIME_SPEED)*1000)/1000
UpdateBodiesAcceleration()
UpdateBodiesVelocity(dt * TIME_SPEED)
UpdateBodiesPosition(dt * TIME_SPEED)
UpdateBodiesVelocity(DT * TIME_SPEED)
UpdateBodiesPosition(DT * TIME_SPEED)
end
function love.draw()
-- center camera on bigger one
local vm = 0
for n, body in pairs(BodyList) do
if body.mass > vm then
vm = body.mass
VIEW.x = -body.x
VIEW.y = -body.y
end
end
local vm = 0
for n, body in pairs(BodyList) do
if body.max_distance > vm then
vm = body.max_distance
VIEW.size = math.sqrt(body.max_distance)
end
end
love.graphics.setColor(1,1,1)
-- draw bodies
for n, body in pairs(BodyList) do
love.graphics.setColor(body.color[1],body.color[2],body.color[3])
love.graphics.circle("line",body.x+SCREEN_WIDTH/2,body.y+SCREEN_HEIGHT/2,math.sqrt(body.mass))
addPrint(body)
end
drawPrints()
-- draw info
love.graphics.setColor(1,1,1)
love.graphics.print(T)
love.graphics.print("x,y: " .. VIEW.x .. VIEW.y .. " size: " .. VIEW.size .. " T: ".. T)
for n, body in pairs(BodyList) do
love.graphics.print("mass: "..body.mass.." x,y: "..body.x..", "..body.y,0,15*(n))
love.graphics.setColor(body.color[1],body.color[2],body.color[3])
love.graphics.print("mass: "..body.mass..", x,y: "..body.x..", "..body.y..", vx, vy:"..body.vx..", "..body.vy,0,15*n)
end
end
function addPrint(body)
VIEW.print_time = 10
table.insert(VIEW.prints,{
pos = {
x = body.x,
y = body.y
},
mass = body.mass,
color = body.color,
time = VIEW.print_time
})
end
function drawPrints()
for n, print in pairs(VIEW.prints) do
love.graphics.setColor(print.color[1],print.color[2],print.color[3],print.time/VIEW.print_time)
love.graphics.circle("line",(VIEW.x+print.pos.x)/VIEW.size+SCREEN_WIDTH/2,(VIEW.y+print.pos.y)/VIEW.size+SCREEN_HEIGHT/2,(print.time/VIEW.print_time)*math.sqrt(print.mass)*5/VIEW.size)
print.time = print.time - 1
if print.time < 1 then VIEW.prints[n] = nil end
end
end