gravity implemennted

This commit is contained in:
UndeadMaelys 2022-05-16 14:59:28 +02:00
parent e0b3c3dbff
commit 52813e4ef6
4 changed files with 136 additions and 0 deletions

60
body.lua Normal file
View File

@ -0,0 +1,60 @@
BodyList = {}
Body = class(nil,{type=0})
function newBody(x,y,mass,color)
local o = {}
-- coordinates
o.x = x
o.y = y
-- velocities
o.vx = 0
o.vy = 0
-- accceleration
o.ax = 0
o.ay = 0
-- mass
o.mass = mass
-- color
o.color = color
table.insert(BodyList,o)
end
function UpdateBodiesAcceleration()
for n, body in pairs(BodyList) do
local ax = 0
local ay = 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 d2 = dx*dx + dy*dy
local f =
(CONST_G * other_body.mass) /
d2 * math.sqrt(d2 + CONST_S)
ax = ax + dx * f
ay = ay + dy * f
end
end
body.ax = ax
body.ay = ay
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
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
end
end

17
class.lua Normal file
View File

@ -0,0 +1,17 @@
function class(super, self)
assert(super == nil or super.__index == super)
self = self or {}
self.__index = self
setmetatable(self, super)
return self
end
function getAncestors(self)
local family = self
local list = {}
while family ~= nil do
family = getmetatable(family)
table.insert(list,family)
end
return list
end

7
conf.lua Normal file
View File

@ -0,0 +1,7 @@
function love.conf(love)
love.title = "HSS"
love.version = "11.3"
love.window.width = 800
love.window.height = 800
love.window.resizable = true
end

52
main.lua Normal file
View File

@ -0,0 +1,52 @@
function love.load()
--options
love.graphics.setColor(1,1,1)
love.graphics.setDefaultFilter("nearest") -- good pixel
SCREEN_WIDTH = love.graphics.getWidth()
SCREEN_HEIGHT = love.graphics.getHeight()
-- simulation variables
CONST_G = 39.5
TIME_SPEED = 0.05
CONST_S = 0.15
T = 0
require "class"
require "body"
newBody(0,0,300,{1,0,0})
newBody(0,0.1,100,{0,1,0})
newBody(0.1,0,200,{0,0,1})
newBody(0.1,0.1,50,{1,1,0})
newBody(0.1,0.2,250,{1,0,1})
newBody(0.2,0.1,150,{0,1,1})
end
function love.update(dt)
T = T + dt
while T > TIME_SPEED do
UpdateBodiesAcceleration()
UpdateBodiesVelocity(dt)
UpdateBodiesPosition(dt)
T = T - TIME_SPEED
end
end
function love.draw()
-- 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,10)
end
-- draw info
love.graphics.setColor(1,1,1)
love.graphics.print(T.."/"..TIME_SPEED)
for n, body in pairs(BodyList) do
love.graphics.print("mass: "..body.mass.." x,y: "..body.x..", "..body.y,0,15*(n))
end
end