def test_gravity(self): world = DiscreteDynamicsWorld() world.setGravity(Vector3(3, 2, 1)) gravity = world.getGravity() self.assertEqual(gravity.x, 3) self.assertEqual(gravity.y, 2) self.assertEqual(gravity.z, 1)
def test_isInWorld(self): body = RigidBody() self.assertFalse(body.isInWorld()) world = DiscreteDynamicsWorld() world.addRigidBody(body) self.assertTrue(body.isInWorld()) world.removeRigidBody(body) self.assertFalse(body.isInWorld())
def test_cycle(self): world = DiscreteDynamicsWorld() class Cheat(RigidBody): pass body = Cheat(None, BoxShape(Vector3(3, 4, 5))) body.cycle = world world.addRigidBody(body) del body, world import gc gc.collect()
def test_linearVelocity(self): world = DiscreteDynamicsWorld() world.setGravity(Vector3(0, 0, 0)) obj = RigidBody(None, None, 1) obj.setLinearVelocity(Vector3(1, 2, 3)) world.addRigidBody(obj) expectedSteps = 64 numSteps = world.stepSimulation(1.0, expectedSteps, 1.0 / expectedSteps) self.assertEqual(numSteps, expectedSteps) position = obj.getMotionState().getWorldTransform().getOrigin() self.assertEquals(position.x, 1) self.assertEquals(position.y, 2) self.assertEquals(position.z, 3)
def test_stepSimulation(self): world = DiscreteDynamicsWorld() world.setGravity(Vector3(1, 2, 3)) obj = RigidBody(None, None, 1) world.addRigidBody(obj) expectedSteps = 64 numSteps = world.stepSimulation(1.0, expectedSteps, 1.0 / expectedSteps) self.assertEqual(numSteps, expectedSteps) position = obj.getMotionState().getWorldTransform().getOrigin() # Unfortunately, there is some error (as compared to physical reality) # in Bullet's results. My fault? Bullet's fault? I'm not sure. self.assertEqual(position.x, 0.5 + 0.5 / expectedSteps) self.assertEqual(position.y, 1.0 + 1.0 / expectedSteps) self.assertEqual(position.z, 1.5 + 1.5 / expectedSteps)
def test_applyImpulse(self): body = RigidBody(mass=1.0) body.applyImpulse(Vector3(1, 2, 3), Vector3(1, 1, 1)) world = DiscreteDynamicsWorld() world.setGravity(Vector3(0, 0, 0)) world.addRigidBody(body) expectedSteps = 64 numSteps = world.stepSimulation(1.0, expectedSteps, 1.0 / expectedSteps) self.assertEqual(numSteps, expectedSteps) transform = body.getMotionState().getWorldTransform() position = transform.getOrigin() self.assertEqual(position.x, 1.0) self.assertEqual(position.y, 2.0) self.assertEqual(position.z, 3.0) rot = transform.getRotation() self.assertNotEqual((rot.getX(), rot.getY(), rot.getZ(), rot.getW()), (0, 0, 0, 1))
def test_applyCentralImpulse(self): body = RigidBody(mass=1.0) body.applyCentralImpulse(Vector3(1, 2, 3)) world = DiscreteDynamicsWorld() world.setGravity(Vector3(0, 0, 0)) world.addRigidBody(body) expectedSteps = 64 numSteps = world.stepSimulation(1.0, expectedSteps, 1.0 / expectedSteps) self.assertEqual(numSteps, expectedSteps) transform = body.getMotionState().getWorldTransform() position = transform.getOrigin() self.assertEqual(position.x, 1.0) self.assertEqual(position.y, 2.0) self.assertEqual(position.z, 3.0) rot = transform.getRotation() self.assertEqual( (rot.getX(), rot.getY(), rot.getZ(), rot.getW()), (0, 0, 0, 1))
class DebugDrawerTests(TestCase): def setUp(self): self.world = DiscreteDynamicsWorld() self.recorder = DebugRecorder() self.recorder.setDebugMode(DRAW_WIREFRAME | DRAW_CONTACT_POINTS) self.world.setDebugDrawer(self.recorder) def test_debugFlags(self): """ Various integer debug flags are exposed on the bullet module. """ # Values should agree with those from btIDebugDraw.h self.assertEqual(0, NO_DEBUG) self.assertEqual(1 << 0, DRAW_WIREFRAME) self.assertEqual(1 << 1, DRAW_AABB) self.assertEqual(1 << 2, DRAW_FEATURES_TEXT) self.assertEqual(1 << 3, DRAW_CONTACT_POINTS) self.assertEqual(1 << 6, DRAW_TEXT) self.assertEqual(1 << 11, DRAW_CONSTRAINTS) self.assertEqual(1 << 12, DRAW_CONSTRAINT_LIMITS) def test_lines(self): """ Some lines are drawn using the debug drawer when L{CollisionWorld.debugDrawWorld} is called. """ obj = CollisionObject() shape = BoxShape(Vector3(1, 2, 3)) obj.setCollisionShape(shape) self.world.addCollisionObject(obj) self.world.debugDrawWorld() self.assertTrue(len(self.recorder.lines) > 0) for line in self.recorder.lines: self.assertEquals(len(line), 9) def test_collisions(self): """ When objects collide and L{CollisionWorld.debugDrawWorld} is called, collisions are drawn using the debug drawer. """ def objAt(pos): obj = RigidBody(None, BoxShape(Vector3(1, 1, 1)), 1.0) xform = Transform() xform.setOrigin(pos) obj.setWorldTransform(xform) return obj first = objAt(Vector3(-2, 0, 0)) first.applyCentralForce(Vector3(50, 0, 0)) second = objAt(Vector3(2, 0, 0)) second.applyCentralForce(Vector3(-50, 0, 0)) self.world.addRigidBody(first) self.world.addRigidBody(second) # Simulate the world enough for them to hit each other for i in range(100): self.world.stepSimulation(1.0 / 60.0) self.world.debugDrawWorld() self.assertTrue(len(self.recorder.contacts) > 0) for contact in self.recorder.contacts: self.assertEquals(len(contact), 11)
def setUp(self): self.world = DiscreteDynamicsWorld() self.recorder = DebugRecorder() self.recorder.setDebugMode(DRAW_WIREFRAME | DRAW_CONTACT_POINTS) self.world.setDebugDrawer(self.recorder)
def test_removeAction(self): world = DiscreteDynamicsWorld() action = KinematicCharacterController(SphereShape(1), 1.0, 1) world.addAction(action) world.removeAction(action)
def test_removeRigidBody(self): world = DiscreteDynamicsWorld() body = RigidBody() world.addRigidBody(body) world.removeRigidBody(body)
def test_empty(self): world = DiscreteDynamicsWorld() self.assertEqual(world.getNumCollisionObjects(), 0)