From 4cb1cfbae84bf61c7922991b70edf96a4c23f9ae Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Thu, 7 Mar 2019 13:17:07 -0800 Subject: [PATCH] init --- .gitignore | 1 + src/main.moon | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 .gitignore create mode 100644 src/main.moon diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d907c43 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.lua diff --git a/src/main.moon b/src/main.moon new file mode 100644 index 0000000..b375fd0 --- /dev/null +++ b/src/main.moon @@ -0,0 +1,75 @@ +width, height = love.graphics.getDimensions! +rng = love.math.newRandomGenerator 0 +types = 7 +radius = 4 +colors = {} +interactions = {} +universe = {} + +physics = (a, b) -> + dx = a.x - b.x + dy = a.y - b.y + ds = dx * dx + dy * dy + theta = math.atan2 dy, dx + x = math.cos theta + y = math.sin theta + + f = interactions["#{a.n}.#{b.n}"](ds) + a.vx += f * x + a.vy += f * y + + f = interactions["#{b.n}.#{a.n}"](ds) + b.vx += f * x + b.vy += f * y + +for a = 1, types + for b = 1, types + A = rng\randomNormal 0.5, 0 + aa = rng\randomNormal 5, 10 + B = rng\randomNormal 0.5, 0 + bb = rng\randomNormal 5, 10 + interactions["#{a}.#{b}"] = (ds) -> + if ds < radius^0.5 + return 1 / ds + else + return A * (ds^0.5 - aa) + interactions["#{b}.#{a}"] = (ds) -> + if ds < radius^0.5 + return 1 / ds + else + return B * (ds^0.5 - bb) + +-- for k,v in pairs interactions +-- print k,v + +for a = 1, types + colors[a] = { rng\random!, rng\random!, rng\random!, 1 } + +class Particle + new: (@n=1, x, y) => + @x = x or love.math.random 0, width + @y = y or love.math.random 0, height + @vx = 0 + @vy = 0 + +for i=1, 2 + table.insert universe, Particle(love.math.random 1, types) + +love.update = (dt) -> + for a = 1, #universe - 1 + for b = a + 1, #universe + physics(universe[a], universe[b]) + + for particle in *universe + particle.vx *= 0.01 + particle.vy *= 0.01 + particle.x += particle.vx * dt + particle.y += particle.vy * dt + +love.draw = -> + for particle in *universe + love.graphics.setColor colors[particle.n] + love.graphics.circle "fill", particle.x, particle.y, radius + +love.keypressed = (key) -> + love.event.quit! if key == "escape"