Skip to content

Instantly share code, notes, and snippets.

@GoToLoop
Forked from Speykious/CircleMutualAttraction
Last active March 30, 2018 04:05
Show Gist options
  • Save GoToLoop/1861619991354ed7d063026658270689 to your computer and use it in GitHub Desktop.
Save GoToLoop/1861619991354ed7d063026658270689 to your computer and use it in GitHub Desktop.
This is an example for the library Fisica for Processing in Python.
"""
CircleMutualAttraction (v2.3.8)
by Speykious (2017-Jun-27)
mod GoToLoop (2017-Jun-29)
https://Forum.Processing.org/two/discussion/23226/
new-python-example-for-the-fisica-library#Item_2
https://Gist.GitHub.com/GoToLoop/1861619991354ed7d063026658270689
"""
# from processing.core import PVector as Vec
Vec = __pvector__
add_library('fisica')
Fisica.init(this)
world = FWorld(gravity=(0, 0))
MIN_DIAM, MAX_DIAM, BOLD = 10, 50, 1.5
FILL, STROKE, BG = 0xffFFFF00, 0xffFF0000, 0xff0000FF
ATTRIBS = FILL, STROKE, BOLD
FPS, FPS_RATE, FORCE, SMOOTH = 60, 30, 1000, 3
MAX_FRAMES = 50 * FPS_RATE
TITLE = 'Balls: %02d - Frame: %04d - FPS: %02.f'
RENDERER = FX2D
def setup():
size(600, 400, RENDERER)
smooth(SMOOTH); frameRate(FPS); colorMode(RGB)
addBall()
def draw():
fc = frameCount
fc < MAX_FRAMES and not fc % FPS_RATE and addBall()
titleInfo()
attraction()
world.step()
background(BG)
world.draw()
def titleInfo():
info = len(balls), frameCount, frameRate
this.surface.title = TITLE % info
def addBall(attribs=ATTRIBS):
x = random(MAX_DIAM, width - MAX_DIAM)
y = random(MAX_DIAM, height - MAX_DIAM)
d = round(random(MIN_DIAM, MAX_DIAM))
fc = FCircle(d, restitution=0, friction=0)
fc.setPosition(x, y)
fc.fillColor, fc.strokeColor, fc.strokeWeight = attribs
world.add(fc)
world.step(0)
global balls, ballsNoTail
balls = world.bodies
ballsNoTail = balls[:-1]
def attraction(f=FORCE, v1=Vec(), v2=Vec(), i=0):
for a in ballsNoTail:
v1.set(a.x, a.y)
i += 1
for b in balls[i:]:
v2.set(b.x, b.y).sub(v1).div(v2.mag()**2).mult(f)
a.addForce(v2.x, v2.y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment