Exemplo n.º 1
0
	def test_collision_pairs(self):
		from bGrease.collision import Circular, Pair
		broad = TestCollisionSys()
		world = TestWorld()
		coll = Circular(broad_phase=broad)
		coll.set_world(world)
		pos_set = world.position.set
		col_set = world.collision.set
		pos_set(1, (0, 0))
		col_set(1, radius=5)
		pos_set(2, (3, 3))
		col_set(2, radius=0)
		pos_set(3, (6, 0))
		col_set(3, radius=1)
		pos_set(4, (-10, 4))
		col_set(4, radius=2)
		pos_set(5, (-13, 4))
		col_set(5, radius=2)
		pos_set(6, (0, 7))
		col_set(6, radius=1.99)

		# Pair everything and make sure the narrow phase sorts it out
		broad.collision_pairs = set([
			Pair(x+1, y+1) for x in range(6) for y in range(6) if x != y])
		coll.step(0)
		self.assertEqual(coll.collision_pairs, set([Pair(1,2), Pair(1, 3), Pair(4, 5)]))
Exemplo n.º 2
0
	def test_before_step(self):
		# Queries should be well behaved even before the controller is run
		from bGrease.collision import Circular
		world = TestWorld()
		broad = TestCollisionSys()
		coll = Circular(broad_phase=broad)
		coll.set_world(world)
		self.assertEqual(coll.collision_pairs, set())
		self.assertEqual(coll.query_point(0,0), set())
Exemplo n.º 3
0
	def test_collision_point_and_normal(self):
		from bGrease.collision import Circular, Pair
		broad = TestCollisionSys()
		world = TestWorld()
		coll = Circular(broad_phase=broad)
		coll.set_world(world)
		pos_set = world.position.set
		col_set = world.collision.set
		pos_set(1, (0, 0))
		col_set(1, radius=2)
		pos_set(2, (4, 0))
		col_set(2, radius=3)
		broad.collision_pairs = set([Pair(1,2)])
		coll.step(0)
		pair = list(coll.collision_pairs)[0]
		(e1, p1, n1), (e2, p2, n2) = pair.info
		self.assertEqual(e1, 1)
		self.assertEqual(p1, (2, 0))
		self.assertEqual(n1, (1, 0))
		self.assertEqual(e2, 2)
		self.assertEqual(p2, (1, 0))
		self.assertEqual(n2, (-1, 0))

		pos_set(2, (0, -5))
		col_set(2, radius=3.5)
		broad.collision_pairs = set([Pair(1,2)])
		coll.step(0)
		pair = list(coll.collision_pairs)[0]
		(e1, p1, n1), (e2, p2, n2) = pair.info
		self.assertEqual(e1, 1)
		self.assertEqual(p1, (0, -2))
		self.assertEqual(n1, (0, -1))
		self.assertEqual(e2, 2)
		self.assertEqual(p2, (0, -1.5))
		self.assertEqual(n2, (0, 1))
Exemplo n.º 4
0
	def test_handlers(self):
		from bGrease.collision import Circular
		world = TestWorld()
		handler_calls = [0, 0]
		def handler1(system):
			self.assertTrue(system is coll, system)
			handler_calls[0] += 1
		def handler2(system):
			self.assertTrue(system is coll, system)
			handler_calls[1] += 1
		coll = Circular(handlers=(handler1, handler2))
		coll.set_world(world)
		coll.step(0)
		self.assertEqual(handler_calls, [1, 1])
		coll.step(0)
		self.assertEqual(handler_calls, [2, 2])
		coll.handlers = (handler2,)
		coll.step(0)
		self.assertEqual(handler_calls, [2, 3])
Exemplo n.º 5
0
	def test_update_aabbs(self):
		from bGrease.collision import Circular
		broad = TestCollisionSys()
		world = TestWorld()
		coll = Circular(broad_phase=broad)
		coll.set_world(world)
		pos = world.position
		col = world.collision
		pos.set(1, (0, 0))
		col.set(1, radius=2)
		pos.set(2, (2, -3))
		col.set(2, radius=0.5)
		pos.set(3, (-5, -2))
		col.set(3, radius=10)
		for i in range(3):
			aabb = col[i + 1].aabb
			self.assertTrue(aabb.left == aabb.top == aabb.right == aabb.bottom == 0, aabb)
		coll.step(0)
		self.assertEqual(col[1].aabb, Data(left=-2, top=2, right=2, bottom=-2))
		self.assertEqual(col[2].aabb, Data(left=1.5, top=-2.5, right=2.5, bottom=-3.5))
		self.assertEqual(col[3].aabb, Data(left=-15, top=8, right=5, bottom=-12))

		pos.set(1, (2, 0))
		pos.set(2, (0, 0))
		col.set(3, radius=5)
		coll.step(0)
		self.assertEqual(col[1].aabb, Data(left=0, top=2, right=4, bottom=-2))
		self.assertEqual(col[2].aabb, Data(left=-0.5, top=0.5, right=0.5, bottom=-0.5))
		self.assertEqual(col[3].aabb, Data(left=-10, top=3, right=0, bottom=-7))

		coll.update_aabbs = False
		pos[1].position = (0, 0)
		col[1].radius = 3
		col[2].radius = 0.75
		col[3].position = (-3, 0)
		# aabbs should not change with update_aabbs set False
		coll.step(0)
		self.assertEqual(col[1].aabb, Data(left=0, top=2, right=4, bottom=-2))
		self.assertEqual(col[2].aabb, Data(left=-0.5, top=0.5, right=0.5, bottom=-0.5))
		self.assertEqual(col[3].aabb, Data(left=-10, top=3, right=0, bottom=-7))
Exemplo n.º 6
0
	def test_step(self):
		from bGrease.collision import Circular
		# Stepping the circular collision system should also step the broad phase
		broad = TestCollisionSys()
		world = TestWorld()
		coll = Circular(broad_phase=broad)
		coll.set_world(world)
		self.assertTrue(coll.world is world)
		self.assertTrue(coll.broad_phase.world is world)
		self.assertEqual(coll.collision_pairs, set())
		self.assertEqual(broad.runtime, 0)
		coll.step(2)
		self.assertEqual(broad.runtime, 2)
		coll.step(1)
		self.assertEqual(broad.runtime, 3)
		self.assertEqual(coll.collision_pairs, set())
Exemplo n.º 7
0
	def test_query_point(self):
		from bGrease.collision import Circular, Pair
		world = TestWorld()
		broad = TestCollisionSys()
		coll = Circular(broad_phase=broad)
		coll.set_world(world)
		pos_set = world.position.set
		col_set = world.collision.set
		pos_set(1, (0, 0))
		col_set(1, radius=1)
		pos_set(2, (0, 2))
		col_set(2, radius=1.5)
		pos_set(3, (-4, 3))
		col_set(3, radius=3)
		coll.step(0)
		self.assertEqual(broad.last_from_mask, None)
		self.assertEqual(coll.query_point(0,0), set([1]))
		self.assertEqual(coll.query_point(0,1), set([1, 2]))
		self.assertEqual(coll.query_point([0,1]), set([1, 2]))
		self.assertEqual(coll.query_point(1, 0), set([1]))
		self.assertEqual(coll.query_point(1.0001, 0), set())
		self.assertEqual(coll.query_point(-1, 3), set([2,3]))
		self.assertEqual(coll.query_point(-5, 3), set([3]))
		self.assertEqual(broad.last_from_mask, 0xffffffff)
		coll.query_point([0, 0], from_mask=0xff)
		self.assertEqual(broad.last_from_mask, 0xff)