Created
June 27, 2017 12:46
-
-
Save Speykious/8f65778e439f242f42e71294351c0853 to your computer and use it in GitHub Desktop.
This is an example for the library Fisica for Processing in Python.
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
# Example created by Speykious | |
add_library('fisica') | |
world = FWorld() | |
def setup(): | |
size(1000, 600) | |
smooth() | |
frameRate(60) | |
Fisica.init(this) | |
global world | |
world.setGravity(0, 0) | |
liobj = [] | |
def draw(): | |
background(50) | |
global liobj | |
global world | |
if frameCount%30==0 and frameCount<60**2: | |
addObj() | |
world.add(liobj[-1]) | |
attraction(1000) | |
world.draw() | |
world.step() | |
# The two next functions are based on the global liobj list | |
def addObj(): | |
global liobj | |
obj = FCircle(random(5, 15)) | |
obj.setRestitution(0) | |
obj.setFriction(0) | |
obj.setPosition(random(100, width-100), random(100, height-100)) | |
liobj.append(obj) | |
def attraction(intensity): | |
global liobj | |
for o in liobj: | |
for b in liobj: | |
# An object has not to be attracted by himself | |
if o!=b: | |
opos = PVector(o.getX(), o.getY()) # object o's position | |
bpos = PVector(b.getX(), b.getY()) # object b's position | |
# the two vectors's difference: | |
# the vector's end is on b | |
bvect = PVector.sub(bpos, opos) | |
# Force created from bvect | |
force = PVector.div(bvect, bvect.mag()**2) | |
force.mult(intensity) | |
# Add force to object o | |
o.addForce(force.x, force.y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment