Last active
February 1, 2016 14:57
-
-
Save 2bt/6645475350f680f7c876 to your computer and use it in GitHub Desktop.
Dancing with friends and enemies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local G = love.graphics | |
math.randomseed(os.time()) | |
parts = {} | |
function find_partner(p) | |
while true do | |
local q = parts[math.random(#parts)] | |
if q ~= p and q ~= p.friend and q ~= p.enemy then | |
return q | |
end | |
end | |
end | |
for i = 1, 5000 do | |
local p = { | |
x = math.random(-100, 100), | |
y = math.random(-100, 100), | |
c = 0 | |
} | |
p.ox = p.x | |
p.oy = p.y | |
parts[i] = p | |
end | |
function love.update() | |
for _, p in ipairs(parts) do | |
local vx = p.x - p.ox | |
local vy = p.y - p.oy | |
p.ox = p.x | |
p.oy = p.y | |
if p.c == 0 then | |
p.friend = find_partner(p) | |
p.enemy = find_partner(p) | |
p.c = math.random(5000) | |
end | |
p.c = p.c - 1 | |
local l = ((p.x^2+p.y^2) * 0.0001) ^ 0.5 | |
p.x = p.x * (1 - l * 0.001) | |
p.y = p.y * (1 - l * 0.001) | |
local dx = p.friend.x - p.x | |
local dy = p.friend.y - p.y | |
local l = 4.3 / (10 + (dx*dx + dy*dy) ^ 0.5) | |
p.x = p.x + dx * l | |
p.y = p.y + dy * l | |
local dx = p.enemy.x - p.x | |
local dy = p.enemy.y - p.y | |
local l = -0.8 / (10 + (dx*dx + dy*dy) ^ 0.5) | |
p.x = p.x + dx * l | |
p.y = p.y + dy * l | |
p.x = p.x + vx * 0.5 | |
p.y = p.y + vy * 0.5 | |
end | |
end | |
for _ = 1, 300 do love.update() end | |
function love.draw() | |
G.translate(400, 300) | |
G.setColor(255, 255, 255, 15) | |
-- love.graphics.setBlendMode("add") | |
for _, p in ipairs(parts) do | |
local rot = math.atan2(p.y - p.oy, p.x - p.ox) | |
local l = math.max(1, ((p.x - p.ox) ^ 2 + (p.y - p.oy) ^ 2) ^ 0.4) | |
G.push() | |
G.translate(p.x, p.y) | |
G.rotate(rot) | |
G.scale(l, 1) | |
G.circle("fill", 0, 0, 3, 4) | |
G.pop() | |
-- G.circle("fill", p.x, p.y, 3, 3) | |
-- G.line(p.x, p.y, p.friend.x, p.friend.y) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment