Python Tutorials | (back to the list of tutorials) |
add_library('igeo') def setup() : size(480,360,IG.GL) MyParticle(IVec(0,0,0), IVec(10,0,0)) class MyParticle(IParticle) : def __init__(self, p, v) : IParticle.__init__(self,p,v) # initialize with position and velocity
The code above shows just one moving particle. If you want to show a trajectory of a particle, you can use IParticleTrajectory instead of IParticle.
add_library('igeo') def setup() : size(480,360,IG.GL) MyParticle(IVec(0,0,0), IVec(10,0,0)) class MyParticle(IParticleTrajectory) : def __init__(self, p, v) : IParticleTrajectory.__init__(self,p,v) # initialize with position and velocity
add_library('igeo') def setup() : size(480,360,IG.GL) MyParticle(IVec(0,0,0), IVec(10,0,0)) class MyParticle(IParticleTrajectory) : def __init__(self, p, v) : IParticleTrajectory.__init__(self,p,v) # initialize with position and velocity self.fric(0.05) # 5% friction def update(self) : if IRand.pct(10) : self.push(IRand.pt(-1000,-1000,0,1000,1000,0))
add_library('igeo') def setup() : size(480,360,IG.GL) for i in range(50) : MyParticle(IRand.pt(-50,-50,0,50,50,0), IVec(0,0,0)) class MyParticle(IParticleTrajectory) : def __init__(self, p, v) : IParticleTrajectory.__init__(self,p,v) # initialize with position and velocity self.fric(0.05) # 5% friction def interact(self, agents) : for agent in agents : if isinstance(agent, MyParticle) : if agent is not self : if agent.pos().dist(self.pos()) < 15 : # closer than 15 dif = self.pos().dif(agent.pos()) #other to this dif.len(40) # intensity of force agent.push(dif) def update(self) : if IRand.pct(10) : self.push(IRand.pt(-500,-500,0,500,500,0))
add_library('igeo') def setup() : size(480,360,IG.GL) for i in range(100) : MyParticle(IRand.pt(-50,-50,0,50,50,0), IVec(0,0,0)) class MyParticle(IParticleTrajectory) : def __init__(self, p, v) : IParticleTrajectory.__init__(self,p,v) # initialize with position and velocity self.fric(0.05) # 5% friction self.state = 0 # initial state def interact(self, agents) : for agent in agents : if isinstance(agent, MyParticle) : if agent is not self : if agent.pos().dist(self.pos()) < 15 : # closer than 15 dif = self.pos().dif(agent.pos())#other to this dif.len(40) # intensity of force if self.state == 1 : # attraction agent.push(dif) elif self.state == 2 : # repulsion agent.pull(dif) def update(self) : if IRand.pct(10) : self.push(IRand.pt(-500,-500,0,500,500,0)) if IRand.pct(1) : # switch state if self.state == 0 : # state 0 -> 1 self.state = 1 # attraction self.clr(1.0,0,1.0) elif self.state == 1 : # state 1 -> 2 self.state = 2 # repulsion self.clr(0,0,1.0) else : # state 2 -> 0 self.state = 0 # random walk only self.clr(0.5)
add_library('igeo') def setup() : size(480,360,IG.GL) for i in range(100) : MyParticle(IRand.pt(-50,-50,0,50,50,0), IVec(0,0,0)) IG.bg(0) class MyParticle(IParticle) : def __init__(self, p, v) : IParticle.__init__(self,p,v) # initialize with position and velocity self.fric(0.05) # 5% friction self.state = 0 # initial state def interact(self, agents) : for agent in agents : if isinstance(agent, MyParticle) : if agent is not self : if agent.pos().dist(self.pos()) < 15 : # closer than 15 dif = self.pos().dif(agent.pos())#other to this dif.len(20) # intensity of force if self.state == 1 : # attraction agent.push(dif) elif self.state == 2 : # repulsion agent.pull(dif) def update(self) : if IRand.pct(10) : self.push(IRand.pt(-500,-500,0,500,500,0)) if IRand.pct(1) : # switch state if self.state == 0 : # state 0 -> 1 self.state = 1 # attraction self.clr(1.0,0,1.0) elif self.state == 1 : # state 1 -> 2 self.state = 2 # repulsion self.clr(0,0,1.0) else : # state 2 -> 0 self.state = 0 # random walk only self.clr(0.5) if self.time()%20 == 0 : Anchor(self.pos().cp()) class Anchor(IAgent) : def __init__(self, p) : self.pos = p self.point = IPoint(self.pos).clr(1.0,0,0).size(2) def interact(self, agents) : if self.time() == 0 : # only when the first time for agent in agents : if isinstance(agent, Anchor) : if agent is not self and agent.time() > 0 : # exclude anchors just created if agent.pos.dist(self.pos) < 10 : # closer than 10 ICurve(agent.pos, self.pos).clr(1.0,0.1) def update(self) : if self.time()==100 : # delete after 100 time frame self.point.del() self.del()