def restart(self): """Start or restart the plugin.""" self.draw = Draw() # Utility for drawing 2D lines and shapes in 3-space path = Path([Vec2(40,40),Vec2(40,-40),Vec2(-40,40),Vec2(-40,-40)]) for vehicle in self.vehicles[:3]: vehicle._pos = SteerVec((random.random()-0.5)*100,(random.random()-0.5)*100) vehicle._velocity = SteerVec((random.random()-0.5)*2,(random.random()-0.5)*2) vehicle.followPath(path,loop=True) c=(.6,.6,.6,.3) t=1 z=1.5 self.draw.drawLine(Vec3(path[0].getX(),path[0].getY(),z),Vec3(path[1].getX(),path[1].getY(),z),color=c,thickness=t) self.draw.drawLine(Vec3(path[1].getX(),path[1].getY(),z),Vec3(path[2].getX(),path[2].getY(),z),color=c,thickness=t) self.draw.drawLine(Vec3(path[2].getX(),path[2].getY(),z),Vec3(path[3].getX(),path[3].getY(),z),color=c,thickness=t) self.draw.drawLine(Vec3(path[3].getX(),path[3].getY(),z),Vec3(path[0].getX(),path[0].getY(),z),color=c,thickness=t) path = Path([Vec2(-40,-40),Vec2(40,-40),Vec2(-40,40),Vec2(40,40)]) for vehicle in self.vehicles[3:]: vehicle._pos = SteerVec((random.random()-0.5)*100,(random.random()-0.5)*100) vehicle._velocity = SteerVec((random.random()-0.5)*2,(random.random()-0.5)*2) vehicle.followPath(path,loop=True) self.draw.drawLine(Vec3(path[0].getX(),path[0].getY(),z),Vec3(path[1].getX(),path[1].getY(),z),color=c,thickness=t) self.draw.drawLine(Vec3(path[1].getX(),path[1].getY(),z),Vec3(path[2].getX(),path[2].getY(),z),color=c,thickness=t) self.draw.drawLine(Vec3(path[2].getX(),path[2].getY(),z),Vec3(path[3].getX(),path[3].getY(),z),color=c,thickness=t) self.draw.drawLine(Vec3(path[3].getX(),path[3].getY(),z),Vec3(path[0].getX(),path[0].getY(),z),color=c,thickness=t) for vehicle in self.vehicles: vehicle.avoidObstacles = True vehicle.avoidVehicles = True vehicle.maxforce = 0.1 vehicle.maxspeed = 0.5 + (random.random()/2) node = self.draw.create() np = NodePath(node) np.reparentTo(render)
def grassTexture(imgSize=(256,256)): """Return a green, 'grassy' texture (PNMImage) of the given image size, produced using 2D Perlin noise.""" # Initialuse the PNMImage object img = PNMImage(*imgSize) # Initalise 4 PerlinNoise2 objects to produce noise at different scales noise1 = PerlinNoise2() noise1.setScale(2.0) noise2 = PerlinNoise2() noise2.setScale(5.0) noise3 = PerlinNoise2() noise3.setScale(0.25) noise4 = PerlinNoise2() noise4.setScale(0.125) # For each pixel in the image, set the red and blue values of the pixel to # constant values, and set the green value of the pixel using all 4 # PerlinNoise2 objects. red = 0.125 # Colour values in PNMImage are doubles in the range [0.0,1.0] blue = 0.0625 for x in xrange(imgSize[0]): for y in xrange(imgSize[1]): img.setRed(x,y,red) img.setBlue(x,y,blue) pos = Vec2(1.0/32*x, 1.0/32*y) img.setGreen(x,y,(0.5 + noise1(pos)*0.0625 + noise2(pos)*0.0625 + noise3(pos)*0.125 + noise4(pos)*0.0625)) return img
def setPolygonalNeighbors(self, prev, next): vecToPrev = prev.pos - self.pos vecToNext = next.pos - self.pos angle = vecToPrev.signedAngleDeg(vecToNext) angle %= 360 # Convert this to an unsigned angle. self.prevPolyNeighbor = prev self.nextPolyNeighbor = next self.interiorAngle = angle prevAngle = Vec2(1, 0).signedAngleDeg(vecToPrev) extrudeAngle = prevAngle + self.interiorAngle / 2.0 + 180 extrudeAngle *= math.pi / 180 # Degrees to radians self.extrudeVector = Vec2(math.cos(extrudeAngle), math.sin(extrudeAngle))
def wander(self, elapsed): pos = Vec2(self.getPos().xy) r = elapsed * self.maxSpeed * 3 if (pos - self.startPosition).length() > self.wanderRadius: change = self.startPosition - pos change.normalize() change *= r else: change = Vec2(random.uniform(-r, r), random.uniform(-r, r)) desiredVelocity = self.wanderVelocity + change desiredVelocity.normalize() desiredVelocity *= self.maxSpeed self.wanderVelocity = desiredVelocity desiredHeading = math.degrees( math.atan2(desiredVelocity.y, desiredVelocity.x)) + 90 desiredVelocity = Vec3(desiredVelocity.x, desiredVelocity.y, 0) #logging.info( desiredVelocity) self.move(desiredVelocity, desiredHeading, elapsed)
def __init__(self, heightFunction, x=0, y=0): NodePath.__init__(self, "Creature") self.reparentTo(render) self.startPosition = Vec2(x, y) z = heightFunction(x, y) self.setPos(x, y, z) # movement self.acceleration = 25 self.velocity = Vec3(0, 0, 0) self.maxSpeed = 10 self.speed = 0 self.maxAngularVelocity = 360 self.turbo = 1 self.maxStoppingDistance = self.maxSpeed / self.acceleration * 0.5 self.body = Actor("models/ralph", { "run": "models/ralph-run", "walk": "models/ralph-walk" }) self.body.setScale(0.25) self.body.reparentTo(self) self.heightFunction = heightFunction
def __neg__(self): vec2 = Vec2.__neg__(self) return SteerVec(vec2.getX(),vec2.getY())
def __div__(self,other): vec2 = Vec2.__div__(self,other) return SteerVec(vec2.getX(),vec2.getY())
def __init__(self,x=0,y=0): """Initialise the SteerVec. Nothing to do, just calls Vec2.__init__.""" Vec2.__init__(self,x,y)
def setWander(self, radius=50): self.wanderVelocity = Vec2(0, 0) self.wanderRadius = radius