v1 charge and grav
This commit is contained in:
commit
8f4a7fe86d
62
src/main.moon
Normal file
62
src/main.moon
Normal file
@ -0,0 +1,62 @@
|
||||
import random from love.math
|
||||
|
||||
types = setmetatable {}, {
|
||||
__index: (t, k) ->
|
||||
t[k] = {
|
||||
mass: k
|
||||
charge: 1 / k - random!
|
||||
color: { random!, random!, random!, 1 }
|
||||
radius: 0.42 + k^0.13
|
||||
}
|
||||
return t[k]
|
||||
}
|
||||
|
||||
class Particle
|
||||
new: (@x, @y, @vx, @vy, mass) =>
|
||||
for stat, value in pairs types[mass]
|
||||
@[stat] = value
|
||||
update: (dt) =>
|
||||
@x += @vx * dt
|
||||
@y += @vy * dt
|
||||
|
||||
particles = {}
|
||||
|
||||
-- 2 2 -- repel 4 (2 - 2 = 0)
|
||||
-- 2 -2 -- attract 4 (2 - -2 = 4)
|
||||
-- -2 2 -- attract 4 (-2 - 2 = -4)
|
||||
-- -2 -2 -- repel 4 (-2 - -2 = 0)
|
||||
-- add abs values
|
||||
|
||||
do_physics = (A, B, dt) ->
|
||||
dx = A.x - B.x
|
||||
dy = A.y - B.y
|
||||
d2 = dx * dx + dy * dy
|
||||
a = math.atan2 dy, dx
|
||||
x = math.cos a
|
||||
y = math.sin a
|
||||
|
||||
g = (A.mass + B.mass) / d2 / 100
|
||||
c = (math.abs(A.charge) + math.abs(B.charge)) / 10
|
||||
c *= -1 if (A.charge < 0 and B.charge > 0) or (A.charge > 0 and B.charge < 0)
|
||||
f = g + c
|
||||
|
||||
A.vx -= f * x * dt
|
||||
A.vy -= f * y * dt
|
||||
B.vx += f * x * dt
|
||||
B.vy += f * y * dt
|
||||
|
||||
love.update = (dt) ->
|
||||
for i = 1, #particles - 1
|
||||
for j = i + 1, #particles
|
||||
a, b = particles[i], particles[j]
|
||||
do_physics a, b, dt
|
||||
a\update dt
|
||||
b\update dt
|
||||
|
||||
love.draw = ->
|
||||
for particle in *particles
|
||||
love.graphics.setColor particle.color
|
||||
love.graphics.circle "fill", particle.x, particle.y, particle.radius
|
||||
|
||||
love.mousepressed = (x, y, btn) ->
|
||||
table.insert particles, Particle(x, y, 0, 0, math.floor 100 * random!)
|
Loading…
Reference in New Issue
Block a user