コード例 #1
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_normalize(self):
     vec1 = Vector2f(0.0, 0.0)
     vec2 = normalize(vec1)
     self.assertEqual(vec1.x, vec2.x, "Normalized zerovector x failed")
     self.assertEqual(vec1.y, vec2.y, "Normalized zerovector y failed")
     vec1 = Vector2f(1.0, 0.0)
     vec2 = normalize(vec1)
     self.assertEqual(vec1.x, vec2.x, "Normalized (1.0, 0.0) x failed")
     self.assertEqual(vec1.y, vec2.y, "Normalized (1.0, 0.0) y failed")
     vec1 = Vector2f(0.0, 1.0)
     vec2 = normalize(vec1)
     self.assertEqual(vec1.x, vec2.x, "Normalized (0.0, 1.0) x failed")
     self.assertEqual(vec1.y, vec2.y, "Normalized (0.0, 1.0) y failed")
     vec1 = Vector2f(1.23122, 0.23342)
     vec2 = normalize(vec1)
     self.assertEqual(round(vec2.x, 5), 0.98250, "Normalized l>1 x failed")
     self.assertEqual(round(vec2.y, 5), 0.18627, "Normalized l>1 y failed")
     self.assertEqual(round(vec1.x, 5), 1.23122,
                      "Original vector was changed")
     self.assertEqual(round(vec1.y, 5), 0.23342,
                      "Original vector was changed")
     vec1 = Vector2f(0.73122, 0.23342)
     vec2 = normalize(vec1)
     self.assertEqual(round(vec2.x, 5), 0.95264, "Normalized l<1 x failed")
     self.assertEqual(round(vec2.y, 5), 0.30410, "Normalized l<1 y failed")
コード例 #2
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_vec_trunc(self):
     vec1 = Vector2f(0.0, 0.0)
     vec1.trunc(2)
     self.assertEqual(vec1.len(), 0.0, "Zerovector failed")
     vec1 = Vector2f(1.0, 1.0)
     vec1.trunc(2)
     self.assertEqual(vec1.len(), sqrt(2), "Shorter vector failed")
     vec1 = Vector2f(1.0, 1.0)
     vec1.trunc(sqrt(2))
     self.assertEqual(vec1.len(), sqrt(2), "Same length vector failed")
     vec1 = Vector2f(2.0, 2.0)
     vec1.trunc(2)
     self.assertEqual(round(vec1.len(), 5), 2, "Longer vector failed")
コード例 #3
0
 def __init__(self, leader, params, gParams, grid):
     self.params = params
     self.gParams = gParams
     self.grid = grid
     self.pos = Vector2f(random.random() * 1024, random.random() * 768)
     self.loc = Vector2f(
         floor(self.pos.x / 32) + 1,
         floor(self.pos.y / 32) + 1)
     self.grid.insert(self, self.loc.x, self.loc.y)
     self.repulsionF = Vector2f(0.0, 0.0)
     self.v = Vector2f(0.0001, 0.0)
     self.orientation = 0.0
     self.leader = leader
コード例 #4
0
    def __init__(self, params, gParams):
        self.params = params
        self.gParams = gParams
        self.pos = Vector2f(random.random() * 1024, random.random() * 768)
        self.v = Vector2f(0.0000001, 0.0)
        self.orientation = 0.0
        self.cDist = 0.8
        self.cRad = 0.8
        self.wAngle = 0.0
        self.wChange = 0.5
        self.target = Vector2f(0.0, 0.0)
        self.controlled = False
        self.seeking = False

        random.seed(100)
コード例 #5
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_div(self):
     vec1 = Vector2f(1.2543, 1.2432)
     num = 2
     vecResult = vec1 / num
     self.assertEqual(vecResult.x, 1.2543 / num,
                      "Division with positive number failed")
     self.assertEqual(vecResult.y, 1.2432 / num,
                      "Division with positive number failed")
     vec1 = Vector2f(1.2543, 1.2432)
     num = -2
     vecResult = vec1 / num
     self.assertEqual(vecResult.x, 1.2543 / num,
                      "Division with negative number failed")
     self.assertEqual(vecResult.y, 1.2432 / num,
                      "Division with negative number failed")
コード例 #6
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_rotate(self):
     vec1 = Vector2f(1.0, 0.0)
     vec1.rotate(0)
     self.assertEqual(vec1.angle(), 0, "0 failed")
     vec1 = Vector2f(1.0, 0.0)
     vec1.rotate(pi / 4)
     self.assertEqual(round(vec1.angle(), 5), round(pi / 4, 5),
                      "pi/4 failed")
     vec1 = Vector2f(1.0, 0.0)
     vec1.rotate(0.56453)
     self.assertEqual(round(vec1.angle(), 5), 0.56453, "0.56453 failed")
     vec1 = Vector2f(1.0, 0.0)
     vec1.rotate(-pi / 4)
     self.assertEqual(round(vec1.angle(), 5), round(7 * pi / 4, 5),
                      "-pi/4 failed")
コード例 #7
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_trunc(self):
     vec1 = Vector2f(0.0, 0.0)
     vec2 = trunc(vec1, 2)
     self.assertEqual(vec1.len(), vec2.len(), "Zerovector failed")
     vec1 = Vector2f(1.0, 1.0)
     vec2 = trunc(vec1, 2)
     self.assertEqual(vec1.len(), vec2.len(), "Shorter vector failed")
     vec1 = Vector2f(1.0, 1.0)
     vec2 = trunc(vec1, sqrt(2))
     self.assertEqual(vec1.len(), vec2.len(), "Same length vector failed")
     vec1 = Vector2f(2.0, 2.0)
     vec2 = trunc(vec1, 2)
     self.assertEqual(round(vec2.len(), 5), 2, "Longer vector failed")
     self.assertEqual(round(vec1.len(), 5), round(sqrt(8), 5),
                      "Original vector was changed")
コード例 #8
0
 def move(self):
     # Get target from behind the leader
     target = self.leader.getPos() - normalize(
         self.leader.getV()) * self.params.followDist
     # Calculate desired direction
     steer = trunc(target - self.pos, self.params.maxF)
     # Truncate final steering vector and convert to acceleration
     steer = trunc(steer + self.repulsionF, self.params.maxF)
     acc = steer / self.params.mass * self.gParams.speed
     # Truncate final speed, slow down if necessary
     self.v = trunc(self.v + acc, self.params.maxV)
     dist = distance(self.pos, target)
     if dist < self.params.slowingD:
         self.v *= dist / self.params.slowingD
     # Calculate new position
     self.pos += self.v * self.gParams.speed
     # Update grid if necessary
     newLoc = Vector2f(
         floor(self.pos.x / 32) + 1,
         floor(self.pos.y / 32) + 1)
     if newLoc.x != self.loc.x or newLoc.y != self.loc.y:
         self.grid.remove(self, self.loc.x, self.loc.y)
         self.loc = newLoc
         self.grid.insert(self, self.loc.x, self.loc.y)
     # Set current angle for drawing
     self.orientation = self.v.angle() * 180 / pi
コード例 #9
0
ファイル: main.py プロジェクト: sndels/follow-leader
 def setLeaderTarget(self, target):
     # Calculate simulation coordinates from click and set leader target
     vecTarget = Vector2f(target.x(), target.y())
     vecTarget.x -= 16
     vecTarget.y = 768 - (vecTarget.y - 23)
     if vecTarget.x > 0 and vecTarget.x < 1024 and vecTarget.y > 0 and vecTarget.y < 768:
         self.leader.goTo(vecTarget)
コード例 #10
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_len(self):
     vec1 = Vector2f(1.0, 0.0)
     length = vec1.len()
     self.assertEqual(length, 1.0, "Unit vector failed")
     vec1 = Vector2f(1.0, 1.0)
     length = vec1.len()
     self.assertEqual(length, sqrt(2), "[1.0,1.0] vector failed")
     vec1 = Vector2f(0.0, 0.0)
     length = vec1.len()
     self.assertEqual(length, 0.0, "[0.0,0.0] vector failed")
     vec1 = Vector2f(-1.0, 0.0)
     length = vec1.len()
     self.assertEqual(length, 1.0, "[-1.0,0.0] vector failed")
     vec1 = Vector2f(21.234234, 15.345323)
     length = vec1.len()
     self.assertEqual(round(length, 5), 26.19870,
                      "[21.234234,15.345323] vector failed")
コード例 #11
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_mul(self):
     vec1 = Vector2f(1.2543, 1.2432)
     num = 2
     vecResult = vec1 * num
     self.assertEqual(vecResult.x, 1.2543 * num,
                      "Multiplying with positive number failed")
     self.assertEqual(vecResult.y, 1.2432 * num,
                      "Multiplying with positive number failed")
     vec1 = Vector2f(1.2543, 1.2432)
     num = -2
     vecResult = vec1 * num
     self.assertEqual(vecResult.x, 1.2543 * num,
                      "Multiplying with negative number failed")
     self.assertEqual(vecResult.y, 1.2432 * num,
                      "Multiplying with negative number failed")
     vec1 = Vector2f(1.2543, 1.2432)
     num = 0.0
     vecResult = vec1 * num
     self.assertEqual(vecResult.x, 0.0, "Multiplying with zero failed")
     self.assertEqual(vecResult.y, 0.0, "Multiplying with zero failed")
コード例 #12
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_vec_normalize(self):
     vec1 = Vector2f(0.0, 0.0)
     vec1.normalize()
     self.assertEqual(vec1.x, 0.0, "Normalized zerovector x failed")
     self.assertEqual(vec1.y, 0.0, "Normalized zerovector y failed")
     vec1 = Vector2f(1.0, 0.0)
     vec1.normalize()
     self.assertEqual(vec1.x, 1.0, "Normalized (1.0, 0.0) x failed")
     self.assertEqual(vec1.y, 0.0, "Normalized (1.0, 0.0) y failed")
     vec1 = Vector2f(0.0, 1.0)
     vec1.normalize()
     self.assertEqual(vec1.x, 0.0, "Normalized (0.0, 1.0) x failed")
     self.assertEqual(vec1.y, 1.0, "Normalized (0.0, 1.0) y failed")
     vec1 = Vector2f(1.23122, 0.23342)
     vec1.normalize()
     self.assertEqual(round(vec1.x, 5), 0.98250, "Normalized l>1 x failed")
     self.assertEqual(round(vec1.y, 5), 0.18627, "Normalized l>1 y failed")
     vec1 = Vector2f(0.73122, 0.23342)
     vec1.normalize()
     self.assertEqual(round(vec1.x, 5), 0.95264, "Normalized l<1 x failed")
     self.assertEqual(round(vec1.y, 5), 0.30410, "Normalized l<1 y failed")
コード例 #13
0
 def wander(self):
     # Get wander force with random angle
     wForce = normalize(self.v) * self.cDist
     rForce = normalize(self.v) * self.cRad
     self.wAngle += (random.random() * 2 - 1) * self.wChange
     rForce.rotate(self.wAngle)
     wForce += rForce
     # Turn back at the window limits
     if self.pos.x > 1100:
         wForce += Vector2f(-1.0, 0.0) * self.pos.x / 100 * self.params.maxF
     elif self.pos.x < 100:
         wForce += Vector2f(
             1.0, 0.0) * (1280 - self.pos.x) / 100 * self.params.maxF
     if self.pos.y > 650:
         wForce += Vector2f(0.0, -1.0) * self.pos.y / 100 * self.params.maxF
     elif self.pos.y < 70:
         wForce += Vector2f(
             0.0, 1.0) * (720 - self.pos.y) / 100 * self.params.maxF
     # Truncate to maxF
     wForce.trunc(self.params.maxF)
     # Get final velocity
     steering = wForce / self.params.mass * self.gParams.speed
     self.v = trunc(self.v + steering, self.params.maxV)
コード例 #14
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_distance(self):
     vec1 = Vector2f(1.0, 0.0)
     vec2 = Vector2f(0.0, 1.0)
     self.assertEqual(distance(vec1, vec2), sqrt(2), "Dist1 failed")
     vec1 = Vector2f(0.0, 0.0)
     vec2 = Vector2f(0.0, 0.0)
     self.assertEqual(distance(vec1, vec2), 0.0, "Dist2 failed")
     vec1 = Vector2f(0.2314320, 0.3242123)
     vec2 = Vector2f(-0.2342343, 0.0324121)
     self.assertEqual(round(distance(vec1, vec2), 5), 0.54954,
                      "Dist3 failed")
コード例 #15
0
 def calcRepulsion(self, followers):
     self.repulsionF = Vector2f(0.0, 0.0)
     collisionChecks = 0
     # Calculate evasion from neighbours that are too close
     for i in self.grid.getNeighbours(self.loc.x, self.loc.y):
         d = distance(self.pos, i.pos)
         collisionChecks += 1
         if d < self.params.separationD and d != 0.0:
             self.repulsionF += (self.pos -
                                 i.getPos()) * self.params.separationD / d
     # Evade leader if necessary
     fLeader = self.leader.getPos() + normalize(
         self.leader.getV()) * self.params.followDist
     d = distance(self.pos, fLeader)
     if d < self.params.followDist and d != 0.0:
         self.repulsionF += (self.pos -
                             fLeader) * self.params.followDist / d
     d = distance(self.pos, self.leader.getPos())
     if d < self.params.followDist and d != 0.0:
         self.repulsionF += (self.pos - self.leader.getPos()
                             ) * 2 * self.params.followDist / d
     return collisionChecks
コード例 #16
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_angle(self):
     vec1 = Vector2f(1.0, 0.0)
     self.assertEqual(vec1.angle(), 0.0, "0 degrees failed")
     vec1 = Vector2f(0.0, 1.0)
     self.assertEqual(vec1.angle(), pi / 2, "90 degrees failed")
     vec1 = Vector2f(-1.0, 0.0)
     self.assertEqual(vec1.angle(), pi, "180 degrees failed")
     vec1 = Vector2f(0.0, -1.0)
     self.assertEqual(vec1.angle(), 3 * pi / 2, "270 degrees failed")
     vec1 = Vector2f(0.234522, 0.123477)
     self.assertEqual(round(vec1.angle(), 5), round(atan2(vec1.y, vec1.x),
                                                    5),
                      "22.76703 degrees failed")
     vec1 = Vector2f(-0.234522, -0.123477)
     self.assertEqual(round(vec1.angle(), 5),
                      round(atan2(vec1.y, vec1.x) + 2 * pi, 5),
                      "202.76703 degrees failed")
コード例 #17
0
ファイル: test_vector.py プロジェクト: sndels/follow-leader
 def test_add(self):
     vec1 = Vector2f(1.2543, 1.2432)
     vec2 = Vector2f(0.7543, 0.4432)
     vecResult = vec1 + vec2
     self.assertEqual(vecResult.x, 1.2543 + 0.7543,
                      "Adding positive x coordinate failed")
     self.assertEqual(vecResult.y, 1.2432 + 0.4432,
                      "Adding positive y coordinate failed")
     vec1 = Vector2f(1.2543, 1.2432)
     vec2 = Vector2f(-0.7543, -0.4432)
     vecResult = vec1 + vec2
     self.assertEqual(vecResult.x, 1.2543 - 0.7543,
                      "Adding negative x coordinate failed")
     self.assertEqual(vecResult.y, 1.2432 - 0.4432,
                      "Adding negative y coordinate failed")
     vec1 = Vector2f(1.2543, 1.2432)
     vec2 = Vector2f(0.0, 0.0)
     vecResult = vec1 + vec2
     self.assertEqual(vecResult.x, 1.2543,
                      "Adding zero x coordinate failed")
     self.assertEqual(vecResult.y, 1.2432,
                      "Adding zero y coordinate failed")