class Entity(object): def __init__(self, polygon=None, heading=None, position=None): self.body = Polygon(polygon) self.heading = heading self.position = position self.heading = random.randint(0, 360) self.position = Vector([0, 0]) self.velocity = (0, 0) self.rotation = 0 def translate(self, vector): self.position += vector def rotate(self, angle_degrees): self.heading += angle_degrees self.heading %= 360 def update(self): self.translate(self.velocity) self.rotate(self.rotation) self.body.project(heading=self.heading, position=self.position) def intersects(self, other): if isinstance(other, Entity): if self.position.distance( other.position) < self.body.radius + other.body.radius: return True else: if self.position.distance( other.position) < self.body.radius + other.radius: return True return False
def __init__(self, polygon=None, heading=None, position=None): self.body = Polygon(polygon) self.heading = heading self.position = position self.heading = random.randint(0, 360) self.position = Vector([0, 0]) self.velocity = (0, 0) self.rotation = 0
def testPolygonNormal(self): p1 = Point3D(0, 0, 0) p2 = Point3D(1, 0, 0) p3 = Point3D(1, 1, 0) p4 = Point3D(0, 1, 0) p5 = Point3D(1, 0, -1) p6 = Point3D(0, 0, -1) polygon1 = Polygon('', [p1, p2, p3, p4]) polygon2 = Polygon('', [p1, p2, p5, p6]) result = polygon1.getNormalVector() self.assertEqual(result.x, 0) self.assertEqual(result.y, 0) self.assertEqual(result.z, 1) result = polygon2.getNormalVector() self.assertEqual(result.x, 0) self.assertEqual(result.y, 1) self.assertEqual(result.z, 0)
def testPolygonFacesPoint(self): p1 = Point3D(0, 0, 0) p2 = Point3D(1, 0, 0) p3 = Point3D(1, 1, 0) p4 = Point3D(0, 1, 0) p5 = Point3D(1, 0, 1) p6 = Point3D(0, 0, 1) p7 = Point3D(0, 1, 1) p8 = Point3D(0, 1, 0) polygon1 = Polygon('', [p1, p2, p3, p4]) polygon2 = Polygon('', [p1, p2, p5, p6]) polygon3 = Polygon('', [p1, p6, p7, p8]) t1 = Point3D(0, 0, 1) t2 = Point3D(0, 0, -1) t3 = Point3D(0.5, 0.5, 0) t4 = Point3D(0, -1, 0) t5 = Point3D(-1, 0, 0) result = polygon1.polygonFacesPoint(t1) self.assertTrue(result) result = polygon1.polygonFacesPoint(t2) self.assertFalse(result) result = polygon1.polygonFacesPoint(t3) self.assertTrue(result) result = polygon2.polygonFacesPoint(t4) self.assertTrue(result) result = polygon3.polygonFacesPoint(t5) self.assertTrue(result)