iGeo
iGeo copied to clipboard
Problem instantiating a LineAgent object in Python mode
Dear @sghr,
Folks on the Processing forum (me included) have a hard time figuring out how to port your sketch on Particle-based Branching with Spring Force to Python.
It seems you are putting an instruction as an argument inside of super()
when initializating the LineAgent
constructor:
super(parent.pos().cp(dir))
on line 7 of the snippet below
class LineAgent extends IParticle{
LineAgent parent;
boolean isColliding;
IVec dir;
LineAgent(IParticle parent, IVec dir){
super(parent.pos().cp(dir)); // <-- here !
if(parent instanceof LineAgent){
this.parent = (LineAgent)parent;
}
isColliding=false;
hide(); // hide point
this.dir = dir;
fric(0.2);
}
How would that translate in Python mode ?
--Failed attempt--
class LineAgent(IParticle):
def __init__(self, parent, dir):
super(LineAgent, self).__init__(parent, dir) #or IParticle.__init__(self, parent, dir)
#parent.pos().cp(dir) #<-- WHERE should this go ?
if isinstance(parent, LineAgent):
self.parent = parent
self.isColliding = False
self.dir = dir
self.hide()
self.fric(0.2)
No matter what we try, we always end-up with an annoying 'instancemethod' object has no attribute 'pos'
error.
-- Full sketch --
add_library('igeo')
def setup():
size(480, 360, IG.GL)
IG.bg(1.0)
IG.top()
for i in xrange(4):
LineAgent(IParticle(IRand.pt(-40,-40,0,40,40,0)).hide().fric(0.2), IRand.dir(IG.zaxis).len(2))
for i in xrange(20):
IAttractor(IRand.pt(-200,-200,0,200,200,0)).linear(50).intensity(-30)
class LineAgent(IParticle):
def __init__(self, parent, dir):
super(LineAgent, self).__init__(parent, dir)
#parent.pos = parent.pos().cp(dir) #<-- WHERE should this go ?
if isinstance(parent, LineAgent):
self.parent = parent
self.isColliding = False
self.dir = dir
self.hide()
self.fric(0.2)
def interact(self, agents):
if self.time() == 0:
for agent in agents:
if isinstance(agent, LineAgent) and agent != self:
# ERROR below --> attributeError: 'instancemethod' object has no attribute 'pos'
# more specifically: 'self.parent' has no attribute 'pos'
if agent.parent != None and agent.pos().dist(self.pos()) < self.pos().dist(self.parent.pos())*2:
intxn = IVec.intersectSegment(agent.parent.pos(), agent.pos(), self.parent.pos(), self.pos())
if intxn != None and not intxn.eq(self.parent.pos()) and not agent.isColliding:
self.isColliding = True
return
def update(self):
if self.isColliding:
self.del()
else:
if self.time() == 0 and self.alive():
if self.parent != None and self.parent.alive():
ln = ISpringLine(self, self.parent, 100)
t = -cos(IG.time()*.015) * .5 + .5
ln.hsb(.7 - t*.2, 1, .8 - t*.2, .5)
if self.parent.parent != None and self.parent.parent.alive():
st = IStraightenerCurve(self, self.parent, self.parent.parent).tension(100)
st.hsb(.7 - t*.2, 1, .8 - t*.2, .5)
if self.time() == 4:
dir2 = self.dir.cp()
angle = PI*.12
if IRand.pct(50):
self.dir.rot(angle)
dir2.rot(-angle)
else:
self.dir.rot(-angle)
dir2.rot(angle)
LineAgent(self, self.dir)
if IRand.pct(20):
LineAgent(self, dir2)
l = LineAgent(self, dir2)