def test_get_moment(self): chunk = Chunk() self.assertEquals(chunk.get_moment(), 0.0, "bad initial moment") shard1 = Disc(gold, 2, (10, 20)) shard2 = Block(gold, self.unitsquare, (100, 200)) chunk.shards = [shard1, shard2] expected = shard1.get_moment() + shard2.get_moment() self.assertEquals(chunk.get_moment(), expected, "bad moment")
def test_set_shards_two_blocks(self): verts1 = [(0, 0), (0, 4), (4, 4), (4, 0)] block1 = Block(gold, verts1, (8, 0)) verts2 = [(0, 0), (0, 4), (12, 4), (12, 0)] block2 = Block(gold, verts2, (0, 4)) chunk = Chunk(block1, block2) self.assertEquals(chunk.shards, (block1, block2,), "shards not added") self.assertEquals(block1.get_centroid(), (+3, -3), "bad offset1") self.assertEquals(block2.get_centroid(), (-1, +1), "bad offset2")
def test_set_shards_block(self): verts = [(0, 0), (0, 1), (1, 1), (1, 0)] offset = (10, 20) block = Block(gold, verts, offset) self.assertEquals(block.get_centroid(), (10.5, 20.5), "bad offset") chunk = Chunk() chunk.set_shards(block) self.assertEquals(chunk.shards, (block,), "bad shards") shard = chunk.shards[0] self.assertEquals(shard.get_centroid(), (0, 0), "bad offset")
def test_add_to_body(self): space = Space() body = Body(10, 20) verts = [(-1, -1), (-1, +1), (+1, +1), (+1, -1)] offset = (1, 2) block = Block(gold, verts, offset) block.add_to_body(space, body) self.assertEquals(type(block.shape), Poly, "didnt create shape") self.assertEquals(block.shape.body, body, "didnt add shape to body") shapeVerts = block.shape.get_points() expected = [Vec2d(v) for v in offset_verts(verts, offset)] self.assertEquals(shapeVerts, expected, "bad shape verts") self.assertAlmostEquals(block.shape.friction, gold.friction, msg="bad shape friction") self.assertAlmostEquals( block.shape.elasticity, gold.elasticity, msg="bad shape elasticity") spaceShape = [s for s in space.shapes][0] self.assertEquals(block.shape, spaceShape, "didn't add shape to space")
def test_offset(self): verts = [(-1, -2), (-3, +4), (+5, +6), (+7, -8)] block = Block(gold, verts, (10, 20)) block.offset((100, 200)) expected = [(109, 218), (107, 224), (115, 226), (117, 212)] self.assertEquals(block.verts, expected, "didnt apply offset")
def test_get_centroid(self): verts = [(10, 20), (10, 24), (12, 24), (12, 20)] block = Block(gold, verts, (100, 200)) self.assertEquals(block.get_centroid(), (111, 222), "bad offset")
def test_get_moment(self): verts = [(-1, -1), (-1, +1), (+1, +1), (+1, -1)] offset = (5, 6) block = Block(gold, verts, offset) expected = moment_for_poly(block.mass, verts, offset) self.assertEquals(block.get_moment(), expected, "moment wrong")