New Python Example for the Fisica Library

edited June 2017 in Share Your Work

Hi processers! I created a new Python Example for the Fisica Library, it is called CircleMutualAttraction. I hope it will be added to python examples, because there is only one example for the Fisica Library... https://gist.github.com/Speykious/8f65778e439f242f42e71294351c0853

Comments

  • Are you going to submit a pull request to the Fisica source repository?

    https://github.com/rikrd/fisica

  • edited July 2017

    Your Python Mode sketch didn't run here. I had to make a tiny fix. 8-|
    But then I've decided to fork your gist and do a refactor for the whole code. :ar!

    Still, I couldn't figure out what's its logic. X_X
    Some balls move to the bottom-right mostly. But most of them never moves at all. :-/

    On a side note, most of PVector's methods aren't chainable like they are in Java Mode! :-<

    P.S.: Fixed that chaining problem by adding from processing.core import PVector as Vec for (v2.3+). *-:)
    As a welcome bonus, it's now gotten much faster than Python Mode's own PVector!
    \m/

    As an alternative to import, we can simply use the hidden global __pvector__: Vec = __pvector__. ;;)

    Anyways, here's my fork's link: B-)
    https://Gist.GitHub.com/GoToLoop/1861619991354ed7d063026658270689

    """
     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 In or Register to comment.