def testFindRotationDelta(self): """Can we find the change of rotation to match a facing? """ b = boids.Boid(self.bb) current_match_delta = ((0, 90, 90), (0, -90, -90), (0, 180, 180), (0, 270, -90), (90, 180, 90), (90, 0, -90), (270, 180, -90), (270, 0, 90), (90, 720, -90), ) for current, match, delta in current_match_delta: current = radians(current) match = radians(match) delta = radians(delta) dr = b.findRotationDelta(current, match) self.assertEqual(dr, delta, "c: %s; m: %s; e: %s; got: %s" % (current, match, delta, dr))
def testFacing(self): """Can we find the rotation to a set of coordinates? """ b = boids.Boid(self.bb) b.position = (0, 0) b.rotation = 0 pos = (0, 5) expect = radians(0) rr = b.getFacing(*pos) self.assertEqual(rr, expect, "%s: got %s, expected %s" % (pos, rr, expect)) pos = (5, 0) expect = radians(90) rr = b.getFacing(*pos) self.assertEqual(rr, expect, "%s: got %s, expected %s" % (pos, rr, expect)) pos = (-5, 0) expect = radians(-90) rr = b.getFacing(*pos) self.assertEqual(rr, expect, "%s: got %s, expected %s" % (pos, rr, expect)) pos = (0, -5) expect = radians(180) rr = b.getFacing(*pos) self.assertEqual(rr, expect, "%s: got %s, expected %s" % (pos, rr, expect))
def test_ObstacleSphere_correction_out(): # Boid is outside of obstacle boid = boids.Boid() boid.set_position(1, 2, 2) obstacle = boids.ObstacleSphere(0, 0, 0, 1, 1, 2) #print "correction", obstacle.correction(boid) assert obstacle.correction(boid) == boids.Vec3D(0, 0, 0)
def generate_boid(): x = randrange(0, 640) #random left-right y = randrange(0, 480) #random up-down position = pygame.math.Vector2(x, y) #splat a boid, add to flock list velocity = pygame.math.Vector2((random() * 2) - 1, (random() * 2) - 1) velocity.scale_to_length(50) boidflock.add(boids.Boid(position, velocity))
def test_ObstacleSphere_correction_mid(): # Boid is midway between the max and min radius boid = boids.Boid() boid.set_position(1.5, 0, 0) obstacle = boids.ObstacleSphere(0, 0, 0, 15, 1, 2) correction = obstacle.correction(boid) target = boids.Vec3D(7.5, 0, 0) #print "correction:", correction #print "target:", target assert vec3d_float_eq(correction, target)
def test_ObstacleSphere_correction_in(): # Boid is inside radius_max of obstacle boid = boids.Boid() boid.set_position(0.7, -0.3, 0.23) obstacle = boids.ObstacleSphere(0, 0, 0, 1, 1, 2) correction = obstacle.correction(boid) target = boids.Vec3D(0.879894275382, -0.377097546592, 0.289108119054) #print "correction:", correction, "target:", target # Allow for float error assert vec3d_float_eq(correction, target)
def init_boids(): """ initializes the boids list :return: a list of boids """ boid_list = [] # the following for loop places all the boids initially on the grid # to change the number of boids, simply change the for loop condition for i in range(150): vals = [] pos = [] # the following two for loops were used to randomly create the necessary init vals for the boids for j in range(2): pos.append(random.randrange(305, 455)) for j in range(2): vals.append(random.randrange(-1, 1)) boid_list.append(boids.Boid((pos[0], pos[1]), (vals[0], vals[1]))) return boid_list