def test_add_get_constraint(self): """ Add- and get constraints. """ # Define the constraints. p2p = getP2P(rb_a='1', rb_b='2', pivot_a=(0, 1, 2), pivot_b=(3, 4, 5)) dof = get6DofSpring2(rb_a='1', rb_b='2') # ---------------------------------------------------------------------- # Client --> Clerk. # ---------------------------------------------------------------------- for con in (p2p, dof): payload = {'constraints': [con._asdict()]} # Convert to JSON and back (simulates the wire transmission). enc = json.loads(json.dumps(payload)) # Decode the data. dec_con = protocol.ToClerk_AddConstraints_Decode(enc) dec_con = dec_con['constraints'] assert len(dec_con) == 1 assert dec_con[0] == con # ---------------------------------------------------------------------- # Clerk --> Client # ---------------------------------------------------------------------- for con in (p2p, dof): # Encode source data and simulate wire transmission. enc = protocol.FromClerk_GetConstraints_Encode([con]) enc = json.loads(json.dumps(enc)) # Decode the data. assert len(enc) == 1 assert aztypes.ConstraintMeta(**enc[0]) == con
def test_add_get_remove_constraints(self, client_type): """ Create some bodies. Then add/query/remove constraints. This test only verifies that the Igor interface works. It does *not* verify that the objects are really linked in the actual simulation. """ # Reset the constraint database. igor = azrael.igor.Igor() assert igor.reset().ok # Get the client for this test. client = self.clients[client_type] # Spawn the two bodies. pos_1, pos_2, pos_3 = [-2, 0, 0], [2, 0, 0], [6, 0, 0] new_objs = [ {'templateID': '_templateSphere', 'rbs': {'position': pos_1}}, {'templateID': '_templateSphere', 'rbs': {'position': pos_2}}, {'templateID': '_templateSphere', 'rbs': {'position': pos_3}} ] id_1, id_2, id_3 = 1, 2, 3 assert client.spawn(new_objs) == (True, None, [id_1, id_2, id_3]) # Define the constraints. con_1 = getP2P(rb_a=id_1, rb_b=id_2, pivot_a=pos_2, pivot_b=pos_1) con_2 = get6DofSpring2(rb_a=id_2, rb_b=id_3) # Verify that no constraints are currently active. assert client.getConstraints(None) == (True, None, []) assert client.getConstraints([id_1]) == (True, None, []) # Add both constraints and verify they are returned correctly. assert client.addConstraints([con_1, con_2]) == (True, None, 2) ret = client.getConstraints(None) assert ret.ok and (sorted(ret.data) == sorted([con_1, con_2])) ret = client.getConstraints([id_2]) assert ret.ok and (sorted(ret.data) == sorted([con_1, con_2])) assert client.getConstraints([id_1]) == (True, None, [con_1]) assert client.getConstraints([id_3]) == (True, None, [con_2]) # Remove the second constraint and verify the remaining constraint is # returned correctly. assert client.deleteConstraints([con_2]) == (True, None, 1) assert client.getConstraints(None) == (True, None, [con_1]) assert client.getConstraints([id_1]) == (True, None, [con_1]) assert client.getConstraints([id_2]) == (True, None, [con_1]) assert client.getConstraints([id_3]) == (True, None, [])
def test_specify_6DofSpring2_constraint(self): """ Create two objects and linke them with a 6DOF constraint. The constraint mimicks a spring-loaded slider that will pull the objects together. """ # Create physics simulation. sim = azrael.bullet_api.PyBulletDynamicsWorld(1) # Create identical unit spheres 10 meters apart. id_a, id_b = '10', '20' pos_a = (-5, 0, 0) pos_b = (5, 0, 0) obj_a = getRigidBody(position=pos_a, cshapes={'cssphere': getCSSphere()}) obj_b = getRigidBody(position=pos_b, cshapes={'cssphere': getCSSphere()}) # Load the objects into the physics engine. sim.setRigidBodyData(id_a, obj_a) sim.setRigidBodyData(id_b, obj_b) # Compile the 6DOF constraint. constraints = [get6DofSpring2(rb_a=id_a, rb_b=id_b)] # Step the simulation. Nothing must happen because no forces or # constraints act upon the objects. sim.compute([id_a, id_b], 1.0, 60) ret_a = sim.getRigidBodyData(id_a) ret_b = sim.getRigidBodyData(id_b) assert ret_a.ok and ret_b.ok assert np.allclose(ret_a.data.position, pos_a) assert np.allclose(ret_b.data.position, pos_b) # Load the constraints into the physics engine and step the simulation # again. This time the objects must move closer together. assert sim.setConstraints(constraints).ok # Step the simulation --> the objects must move closer together. sim.compute([id_a, id_b], 1.0, 60) ret_a = sim.getRigidBodyData(id_a) ret_b = sim.getRigidBodyData(id_b) assert ret_a.ok and ret_b.ok assert ret_a.data.position[0] > pos_a[0] assert ret_b.data.position[0] < pos_b[0]
def test_specify_6DofSpring2_constraint(self): """ Create two objects and linke them with a 6DOF constraint. The constraint mimicks a spring-loaded slider that will pull the objects together. """ # Create physics simulation. sim = azrael.bullet_api.PyBulletDynamicsWorld(1) # Create identical unit spheres 10 meters apart. id_a, id_b = 10, 20 pos_a = (-5, 0, 0) pos_b = (5, 0, 0) obj_a = getRigidBody(position=pos_a, cshapes={'cssphere': getCSSphere()}) obj_b = getRigidBody(position=pos_b, cshapes={'cssphere': getCSSphere()}) # Load the objects into the physics engine. sim.setRigidBodyData(id_a, obj_a) sim.setRigidBodyData(id_b, obj_b) # Compile the 6DOF constraint. constraints = [get6DofSpring2(rb_a=id_a, rb_b=id_b)] # Step the simulation. Nothing must happen because no forces or # constraints act upon the objects. sim.compute([id_a, id_b], 1.0, 60) ret_a = sim.getRigidBodyData(id_a) ret_b = sim.getRigidBodyData(id_b) assert ret_a.ok and ret_b.ok assert np.allclose(ret_a.data.position, pos_a) assert np.allclose(ret_b.data.position, pos_b) # Load the constraints into the physics engine and step the simulation # again. This time the objects must move closer together. assert sim.setConstraints(constraints).ok # Step the simulation --> the objects must move closer together. sim.compute([id_a, id_b], 1.0, 60) ret_a = sim.getRigidBodyData(id_a) ret_b = sim.getRigidBodyData(id_b) assert ret_a.ok and ret_b.ok assert ret_a.data.position[0] > pos_a[0] assert ret_b.data.position[0] < pos_b[0]