def wander(agent): steeringController = agent.getSteeringController() centerToTargetVector = steeringController.centerToWanderTarget wanderDistance = steeringController.wanderDistance wanderJitter = steeringController.wanderJitter wanderRadius = steeringController.wanderRadius agentPosition = agent.getPosition() projectionVector = (wanderDistance, 0) targetAdjustment =\ ((random.random() * 2 - 1) * wanderJitter, (random.random() * 2 - 1) * wanderJitter) centerToTargetVector = calculate.addVectors(centerToTargetVector, targetAdjustment) centerToTargetVector = vector.setMagnitude(centerToTargetVector, wanderRadius) localTargetVector = calculate.addVectors(centerToTargetVector, projectionVector) worldTargetVector = convert.vectorToWorldSpace(localTargetVector, agentPosition, agent.getDirectionRadians()) return worldTargetVector
def test_twoVectorsOfOnes_resultIsVectorOfTwos(self): vector1 = (1, 1) vector2 = (1, 1) result = calculate.addVectors(vector1, vector2) self.assertEquals((2, 2), result)
def flock(agent, neighbors, separationMagnitude=50, alignmentMagnitude=2, cohesionMagnitude=1): separationForce = separate.separate(agent, neighbors) alignmentForce = align.align(agent, neighbors) cohesionForce = cohere.cohere(agent, neighbors) weightedSeparationForce = calculate.multiplyVectorAndScalar(separationForce, separationMagnitude) weightedAlignmentForce = calculate.multiplyVectorAndScalar(alignmentForce, alignmentMagnitude) weightedCohesionForce = calculate.multiplyVectorAndScalar(cohesionForce, cohesionMagnitude) totalForce = calculate.addVectors(weightedSeparationForce, weightedAlignmentForce, weightedCohesionForce) return totalForce
def test_addPositiveAndNegativeVectors(self): vector1 = (2, 2) vector2 = (-1, -1) result = calculate.addVectors(vector1, vector2) self.assertEquals((1, 1), result)
def test_twoZeroVectors_resultIsZero(self): vector1 = (0, 0) vector2 = (0, 0) result = calculate.addVectors(vector1, vector2) self.assertEquals((0, 0), result)
def prioritizedRunningSum(self): force = (0, 0) remaining_reservoir = self.force_reservoir for behaviorKeyword in self.keywords: action = self.actions[behaviorKeyword] if action is None: continue arguments = (self.agent,) + action behaviorFunction = self.steeringFunctions[behaviorKeyword] weight = self.weight[behaviorKeyword] forceForBehavior = calculate.multiplyVectorAndScalar(behaviorFunction(*arguments), weight) forceForBehavior = vector.truncate(forceForBehavior, remaining_reservoir) magnitudeForBehaviorForce = vector.getMagnitude(forceForBehavior) remaining_reservoir -= magnitudeForBehaviorForce force = calculate.addVectors(force, forceForBehavior) if remaining_reservoir <= 0: break force = vector.truncate(force, self.agent.getMaxForce()) return force
def separate(agent, neighbors): agentPosition = agent.getPosition() cumulativeForce = (0, 0) for neighbor in neighbors: if neighbor == agent: continue neighborPosition = neighbor.getPosition() neighborToAgent = calculate.subtractPoints(agentPosition, neighborPosition) distanceToAgent = vector.getMagnitude(neighborToAgent) if distanceToAgent == 0: neighborHeadingToAgent = vector.normalize((random.random() - 1, random.random() - 1)) magnitude = 100 else: neighborHeadingToAgent = vector.normalize(neighborToAgent) magnitude = max(agent.length, agent.width) / distanceToAgent separationForceForThisNeighbor =\ calculate.multiplyVectorAndScalar(neighborHeadingToAgent, magnitude) cumulativeForce = calculate.addVectors(cumulativeForce, separationForceForThisNeighbor) return cumulativeForce
def cohere(agent, neighbors): if not neighbors: return (0, 0) agentPosition = agent.getPosition() cumulativeVector = (0, 0) neighborCount = 0 for neighbor in neighbors: if neighbor == agent: continue neighborCount += 1 originToNeighbor = neighbor.getPosition() cumulativeVector = calculate.addVectors(cumulativeVector, originToNeighbor) if not neighborCount: return (0, 0) originToCenterPoint = calculate.multiplyVectorAndScalar( cumulativeVector, float(1) / neighborCount) centerPoint = originToCenterPoint return seek.seek(agent, centerPoint)
def weightedTruncatedSum(self): force = (0, 0) action_list = self.action_list for behavior in self.keyword_list: if self.on(behavior): forceForBehavior = calculate.multiplyVectorAndScalar(action_list[behavior].getWeight(), action_list[behavior].executeFunction()) force = calculate.addVectors(force, forceForBehavior) force = vector.truncate(force, self.parent_agent.getMaxForce()) return force
def align(agent, neighbors): #Save us from divide-by-zero if not neighbors: return (0, 0) cumulativeHeading = (0, 0) for neighbor in neighbors: neighborVelocity = neighbor.getVelocity() neighborHeading = vector.normalize(neighborVelocity) cumulativeHeading = calculate.addVectors(cumulativeHeading, neighborHeading) averageHeading = calculate.multiplyVectorAndScalar(cumulativeHeading, float(1) / len(neighbors)) return averageHeading
def update(self, timeElapsed): current_time = self.world.current_time if self.timeOfNormalColor and current_time < self.timeOfNormalColor: self.color = (random.random(), random.random(), random.random()) elif self.timeOfNormalColor: self.timeOfNormalColor = None self.color = self.normalColor world = self.world maxspeed = self.maxSpeed throttlespeed = self.throttleSpeed maxforce = self.maxForce minDetectionLength = self.minDetectionLength for stateMachine in self.stateMachines: stateMachine.update() for turret in self.turrets: turret.update(timeElapsed) self.launch() force = self.steeringController.calculate() force = vector.truncate(vectorTuple=force, cap=maxforce) acceleration = calculate.multiplyVectorAndScalar(vector=force, scalar=timeElapsed / (self.mass * 1000.0)) velocity = calculate.addVectors(self.velocity, acceleration) velocity = vector.truncate(velocity, throttlespeed) self.velocity = velocity speed = vector.getMagnitude(velocity) (x, y) = calculate.addPointAndVector(self.position, velocity) self.position = (x, y) self.obstacleDetectionDimensions[0] =\ minDetectionLength + (speed / maxspeed) * minDetectionLength