def createProjectile(self): ball = self.conservativeCannonball(trajectory.Physics()) for i, val in enumerate(ball.r_set[0, 0]): ball.r_set[0, 0, i] = random.randrange(1000) for i, val in enumerate(ball.r_set[0, 1]): ball.r_set[0, 1, i] = random.randrange(100) return ball
def testProjectileInitialization(self): C = self.cannonMock() P = trajectory.Physics() C.fire_projectile(trajectory.Cannonball, P) self.assertEqual( len(C.projectiles), 1, "Projectile information not appended to self.projectiles") self.assertIsInstance( C.projectiles[0], trajectory.Projectile, "Projectile[0] not an instance of trajectory.Projectile")
def testPhysicsInheritance(self): P = trajectory.Physics() actual = trajectory.Cannonball(P) expected = -9.81 self.assertAlmostEqual( actual.P.gravity, expected, 2, "Error in initial cannonball inheritance of physics") P.gravity = -4.32 expected = -4.32 self.assertAlmostEqual( actual.P.gravity, expected, 2, "Error in updating cannonball physics inheritance")
def testHitDetection(self): C = self.cannonMock() P = trajectory.Physics() C.fire_projectile(trajectory.Cannonball, P) test_projectile = C.projectiles[0] test_projectile.set_initial_conditions([0, 0, 0], 0, pi / 4, 20) while test_projectile.hit_status == False: test_projectile.frame_step() expected = test_projectile.r_set[-1] actual = test_projectile.r_set[test_projectile.current_frame] for i in range(2): for j in range(3): self.assertAlmostEqual( expected[i, j], actual[i, j], "Hit status not triggering on final frame")
def testSettingInitialConditions(self): B = trajectory.Cannonball(trajectory.Physics()) expected_v = float(60) expected_p = [0, 0, 50] B.set_initial_conditions([0, 0, 50], 0, pi / 4, expected_v) actual_v = (B.r_set[0, 1, 0]**2 + B.r_set[0, 1, 1]**2 + B.r_set[0, 1, 2]**2)**(.5) actual_p = B.r_set[0, 0] print "velocity check: (actual, expected)", actual_v, expected_v print "position check: (actual, expected)", actual_p, expected_p for i in range(2): self.assertAlmostEqual( expected_p[i], actual_p[i], 0, "error in setting projectile initial position along coordinate %s" % i) self.assertLess(abs(expected_v - actual_v), 1, "error in setting projectile initial velocity")
def testProjectileUpdate(self): C = self.cannonMock() P = trajectory.Physics() C.fire_projectile(trajectory.Cannonball, P) test_projectile = C.projectiles[0] expected = np.array( test_projectile.r_set[test_projectile.current_frame]) test_projectile.frame_step() actual = np.array(test_projectile.r_set[test_projectile.current_frame]) self.assertFalse( np.array(expected == actual).all(), "Updated frame is identical to previous frame") for i in range(2): for j in range(3): self.assertAlmostEqual( actual[i, j], test_projectile.r_set[1][i, j], "Data from update frame not equal to frame[0 + 1]")
def testProjectileInitialConditionPassing(self): C = self.cannonMock() P = trajectory.Physics() C.fire_projectile(trajectory.Cannonball, P) actual = C.projectiles[0].r_set[0] expected = np.array([[C.center[0], C.center[1], C.height], [ C.vel * sin(C.inclination) * cos(C.azimuth), C.vel * sin(C.inclination) * sin(C.azimuth), C.vel * cos(C.inclination) ]]) i_count = 0 for i in expected: j_count = 0 for j in i: self.assertAlmostEqual( j, actual[i_count, j_count], 1, "Error setting cannon initial conditions in coordinates %s, %s" % (i_count, j_count)) j_count += 1 i_count += 1
import python.engines.trajectory as tj import pygame clock = pygame.time.Clock() Phys = tj.Physics() C = tj.Cannonball(Phys) R = tj.RPG(Phys) H = tj.Homing(Phys) cannons = [C, R, H] for cannon in cannons: cannon.set_initial_conditions([0,0,0], 0, 3.14/4, 40) cannon.calculate_trajectory() pygame.init() screen = pygame.display.set_mode((640, 480)) canvas = pygame.Surface(screen.get_size()) canvas.fill((0,0,0)) screen.blit(canvas, (0,0)) colors = [(255,0,0), (0,255,0), (0,0,255)] while True: color_index = 0 for cannon in cannons: cannon.frame_step() if cannon.hit_status == False: pygame.draw.circle(canvas, colors[color_index], [int(cannon.get_current_frame()[0,0]), 480 - int(cannon.get_current_frame()[0,2])], 5) color_index += 1 screen.blit(canvas, (0,0)) pygame.display.flip() clock.tick(60)