# Inconsistent collision detection using 'fisica' wrapper for JBox2d in Processing.py sketch

**URL:** <https://discourse.processing.org/t/inconsistent-collision-detection-using-fisica-wrapper-for-jbox2d-in-processing-py-sketch/13467>\
**Category:** Processing.py\
**Created:** [August 19, 2019, 5:59am UTC](https://discourse.processing.org/t/inconsistent-collision-detection-using-fisica-wrapper-for-jbox2d-in-processing-py-sketch/13467 "2019-08-19T05:59:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gytmas](https://avatars.discourse-cdn.com/v4/letter/g/e5b9ba/32.png) [@gytmas](https://discourse.processing.org/u/gytmas)\
**Post date:** [August 19, 2019, 5:59am UTC](https://discourse.processing.org/t/inconsistent-collision-detection-using-fisica-wrapper-for-jbox2d-in-processing-py-sketch/13467/1 "2019-08-19T05:59:36Z")

</div>

Hi There.

I am using processing.py to model some fairly rudimentary evacuation simulations. I am using Fisica as the physics engine to handle the collisions, however, it does not seem to be working. The collision detection only works consistently after a couple of seconds. collisions between the agents (circles seems to be working, but not between the agents and the large black polygons. The smaller polygons seem to be working

![screenshot](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3374d12be1a7d8a215d19763a941a30c9b9a29b8.png)

Any help would be appreciated.

```auto
add_library('fisica')
import copy
import os

global timekeeper

timekeeper = 0

def setup():
    size(1800, 1000) #6
    size(900, 500)
    smooth()
    init(275)

def init(n):
    Fisica.init(this)
    global world, SCL, pop, p, E1, E2, stg, _frames
    world = FWorld()
    world.setGravity(0, 0)
    SCL = 6
    _frames = copy.copy(frameCount)
    E1 = PVector(103.374*SCL, 88.616*SCL)
    E2 = PVector(56.002*SCL, 47.393*SCL)
    addPolyData(SCL)
    addColumnsData(SCL)
pop = definePoints(300, SCL)
p = makeAgents(pop)
for i in p:
    i.render()
global timekeeper
timekeeper += 1
if timekeeper == n+1:
    exit()

def draw():
    background(255)
    frameRate(30)
    world.draw()
    world.step()
    for i in p:
        i.agentWalk()
        i.death()

    renderStats()
    fr = frameCount - _frames
    if len(pop) == 0 or (fr > 1 and fr%9000 == 0):
        noLoop()
        redraw()
        init(275)
        framecount = 0
        loop()

def renderStats():
    seconds = str(frameCount/30)
    startFrames = str(_frames)
    endFrames = str(frameCount)
    startPop = str(len(p))
    endPop = str(len(pop))
    t = str(timekeeper)
    fr = frameCount - _frames
    textSize(32)
    fill(0, 0, 0)
    text("start/end frames: " + startFrames + "/" + endFrames + " diff: " + str(fr) + " Iteration: " + t, 10, 30)
    text("time(s): " + seconds, 10, 60)
    text("start pop: " + startPop + " End pop:" + endPop, 10, 90)
    text("Stage Coorrds: " + stgX + ", " + stgY, 10, 120)

class Agent(object):
    def __init__ (self, i, x, y, evac1, evac2):
        self.x = x
        self.y = y
        self.i = i
        self.l = PVector(self.x, self.y)
        self.s = random(40, 60)
        self.mf = 100
        self.r = 255
        self.g = 255
        self.b = 255
        self.see = FCircle(SCL)
        self.see.setBullet(True)
        self.see.setPosition(self.x, self.y)
        self.q = FCircle(SCL)
        self.q.setPosition(self.x + SCL, self.y + SCL)
        self.q.setFill(self.r, self.g, self.b)
        self.q.setRestitution(0.01)
        self.q.setBullet(True)
        self.dest = PVector(102.374*SCL, 87.616*SCL)
        self.dest2 = PVector(76.002*SCL, 67.393*SCL)
    def render(self):
        world.add(self.q)
    def agentWalk(self):
        if frameCount > 150:
            x = self.q.getX()
            y = self.q.getY()
            self.l = PVector(x, y)
            dirsub = PVector.sub(self.dest, self.l)
            if dirsub.mag() < 3 * SCL:
                self.dest = PVector(E2.x, E2.y)
            dirsub = PVector.sub(self.dest, self.l)
            dirsub.normalize()
            dirsub.mult(self.s)
            vel = PVector(self.q.getVelocityX(), self.q.getVelocityY())
            steer = PVector.sub(dirsub, vel)
            steer.limit(self.mf)
            self.q.addForce(steer.x, steer.y)
    def death(self):
        d = PVector.sub(E2, self.l)
        if d.mag() < 10:
            self.q.removeFromWorld()
            for x, j in enumerate(pop):
                if j == self.i:
                    del pop[x]
                    break
            self.q.setDrawable(False)

def definePoints(n, scl):
    class Triangle(object):
        def __init__ (self, tp):
            self.a = PVector(tp[0][0], tp[0][1])
            self.b = PVector(tp[1][0], tp[1][1])
            self.c = PVector(tp[2][0], tp[2][1])
            self.ba = PVector.sub(self.b, self.a)
            self.ca = PVector.sub(self.c, self.a)

    cy1a = [[75.204, 129.069], [143.143, 49.110], [94.454, 159.742]]
    cy2a = [[143.143, 49.110], [220.295, 82.03], [94.454, 159.742]]

    threshold = 0.2769
    cy1 = Triangle(cy1a)
    cy2 = Triangle(cy2a)
    print(cy2)
    t = []
    while len(t) < n:
        r = random(0, 1)
        if r < threshold:
            #cy1
            q = makePoint(cy1, SCL)
            t.append(q)
        else:
            #cy2
            s = makePoint(cy2, SCL)
            t.append(s)
    return t

def makePoint(t, scl):
    x = random(0, 0.999)
    y = random(0, 0.999 - x)
    rx = (x * t.ba.x) + (y * t.ca.x)
    ry = (x * t.ba.y) + (y * t.ca.y)
    r = PVector(rx, ry)
    r.add(t.a)
    r.mult(scl)
    return r

def makeAgents(p):
    t = []
    for i in p:
        a = Agent(i, i.x, i.y, E1, E2)
        t.append(a)
    return t

def addCWPoly(v, scl):

    p = FPoly()
    p.vertex(v[0][1]*scl, v[0][2]*scl)
    p.vertex(v[1][1]*scl, v[1][2]*scl)
    p.vertex(v[2][1]*scl, v[2][2]*scl)
    p.vertex(v[3][1]*scl, v[3][2]*scl)
    p.vertex(v[4][1]*scl, v[4][2]*scl)
    if len(v) > 5:
        p.vertex(v[5][1]*scl, v[5][2]*scl)
    p.setStatic(True)
    p.setNoStroke()
    p.setRestitution(0.01)
    p.setFill(0)
    world.add(p)

def addColumnsData(scl):
    v = [[131.455, 60.953, 129], [128.882, 64.004, 129], [136.71, 54.889, 129], [134.101, 57.976, 129], [100.95, 96.419, 129], [103.45, 93.516, 129], [95.658, 102.483, 129], [98.267, 99.469, 129], [110.561, 85.191, 129], [108.044, 88.15, 129], [115.798, 79.017, 129], [113.171, 82.086, 129], [121.127, 72.971, 129], [118.426, 75.985, 129], [126.346, 66.87, 129], [123.774, 69.884, 129], [90.081, 109.071, 129], [92.902, 105.782, 129], [84.844, 115.126, 129], [87.481, 112.112, 129], [79.643, 121.245, 129], [82.262, 118.213, 129], [76.997, 124.351, 129]]
    addColumns(v, scl)

def addColumns(v, scl):
    for i in v:
        x = i[0]*scl
        y = i[1]*scl
        rt = i[2]
        t = FBox(6, 6)
        t.setPosition(x, y)
        t.setRotation(radians(rt))
        t.setFill(0)
        t.setStatic(True)
        world.add(t)

def addPolyData(scl):
    aw = [["aw", 71.614, 123.348], ["aw", 50.151, 123.735], ["aw", 87.991, 78.374], ["aw", 100.977, 89.266], ["aw", 71.614, 123.348]]
    f = [["f", 94.454, 159.742], ["f", 220.295, 82.03], ["f", 231.718, 86.904], ["f", 230.43, 102.263], ["f", 106.474, 178.896], ["f", 94.454, 159.742]]
    e = [["e", 231.718, 86.904], ["e", 137.749, 46.809], ["e", 133.749, 29.189], ["e", 233.411, 71.039], ["e", 231.718, 86.904]]
    ae = [["ae", 137.749, 46.809], ["ae", 103.598, 86.222], ["ae", 90.389, 75.52], ["ae", 133.749, 29.189], ["ae", 137.749, 46.809]]
    b = [["b", 50.151, 123.735], ["b", 71.614, 123.348], ["b", 106.474, 178.896], ["b", 90.295, 188.013], ["b", 50.151, 123.735]]
    addCWPoly(aw, scl)
    addCWPoly(f, scl)
    addCWPoly(e, scl)
    addCWPoly(ae, scl)
    addCWPoly(b, scl)
```
