def test_calculateLocalInertia(self): """ Create a sphere and let Bullet compute its inertia via the 'calculateLocalInertia' method. Verify that the result is correct. """ # Compute the inertia of the sphere. The inertia for a sphere is # I = 2 / 5 * mass * (R ** 2) mass, radius = 2, 3 sphere = SphereShape(radius) inertia = sphere.calculateLocalInertia(mass) ref = 2 / 5 * mass * radius ** 2 assert np.allclose(inertia.topy(), ref * np.ones(3))
def runSimulation(sim): # Create the collision shapes for the ball and ground. cs_ball = SphereShape(1) cs_ground = StaticPlaneShape(Vec3(0, 1, 0), 1) # Create a Rigid Body for the static (ie mass=0) Ground. q0 = Quaternion(0, 0, 0, 1) ms = DefaultMotionState(Transform(q0, Vec3(0, -1, 0))) ci = RigidBodyConstructionInfo(0, ms, cs_ground) rb_ground = RigidBody(ci, bodyID=1) del ms, ci # Create a Rigid body for the dynamic (ie mass > 0) Ball. ms = DefaultMotionState(Transform(q0, Vec3(0, 5, 0))) inertia = cs_ball.calculateLocalInertia(1) ci = RigidBodyConstructionInfo(1, ms, cs_ball, inertia) rb_ball = RigidBody(ci, bodyID=2) del ms, inertia, ci # Ensure that Bullet never deactivates the objects. rb_ground.forceActivationState(4) rb_ball.forceActivationState(4) # Add both bodies to the simulation. sim.addRigidBody(rb_ground) sim.addRigidBody(rb_ball) # Sanity check: the ball must be at position y=5 pos = rb_ball.getMotionState().getWorldTransform().getOrigin() pos = pos.topy() assert pos[1] == 5 # Step the simulation long enough for the ball to fall down and # come to rest on the plane. for ii in range(10): sim.stepSimulation(1, 100) # Verify that the y-position of the ball is such that the ball # rests on the plane. pos = rb_ball.getMotionState().getWorldTransform().getOrigin() return pos.topy()
# Create a Rigid Body for the Ground based on the collision shape. mass = 0 groundRigidBodyState = DefaultMotionState( Transform(Quaternion(0, 0, 0, 1), Vec3(0, -1, 0))) ci = RigidBodyConstructionInfo(mass, groundRigidBodyState, groundShape) groundRigidBody = RigidBody(ci) groundRigidBody.setRestitution(1.0) sim.addRigidBody(groundRigidBody) del mass, ci # Create a Rigid body for the Ball based on its collision shape. Let Bullet do # the heavy lifting and compute the mass and inertia for us. fallRigidBodyState = DefaultMotionState( Transform(Quaternion(0, 0, 0, 1), Vec3(0, 20, 0))) mass = 1 fallInertia = ballShape.calculateLocalInertia(mass) ci = RigidBodyConstructionInfo( mass, fallRigidBodyState, ballShape, fallInertia) fallRigidBody = RigidBody(ci) fallRigidBody.setRestitution(0.5) sim.addRigidBody(fallRigidBody) del mass, fallInertia, ci # Step the simulation and print the position of the ball. for ii in range(10): sim.stepSimulation(0.5, 30) ms = fallRigidBody.getMotionState() wt = ms.getWorldTransform() print(wt.getOrigin()) # Remove the rigid bodies from the simulation.
from azBullet import DefaultMotionState, Transform from azBullet import RigidBody, RigidBodyConstructionInfo from IPython import embed as ipshell # Instantiate the Bullet simulation. sim = azBullet.BulletBase() sim.setGravity(Vec3(0, 0, 0)) # Create the spherical collision shape and a compound shape. csSphere = SphereShape(radius=1) cs = azBullet.CompoundShape() # Specify the mass and let Bullet compute the Inertia. total_mass = 1 print('Inertia sphere: ', csSphere.calculateLocalInertia(total_mass)) # Add collision shapes to the compound. Its constituents are two spheres # at different positions. t1 = Transform(Quaternion(0, 0, 0, 1), Vec3(0, -5, 0)) t2 = Transform(Quaternion(0, 0, 0, 1), Vec3(0, 5, 0)) cs.addChildShape(t1, csSphere) cs.addChildShape(t2, csSphere) # The local inertia is only a crude approximation based on the AABBs. Do not # use it. Use calculatePrincipalAxisTransform instead (see below). print('Local Inertia AABBs: ', cs.calculateLocalInertia(total_mass)) # Assign each child 1/N of the total mass. N = cs.getNumChildShapes() masses = [total_mass / N for _ in range(N)]
mass = 0 groundRigidBodyState = DefaultMotionState( Transform(Quaternion(0, 0, 0, 1), Vec3(0, -1, 0))) ci = RigidBodyConstructionInfo(mass, groundRigidBodyState, groundShape) groundRigidBody = RigidBody(ci) groundRigidBody.setRestitution(1.0) sim.addRigidBody(groundRigidBody) del mass, ci # Create the rigid body for the Ball. Since it is a dynamic body we need to # specify mass and inertia. The former we need to do ourselves but Bullet can # do the heavy lifting for the Inertia and compute it for us. fallRigidBodyState = DefaultMotionState( Transform(Quaternion(0, 0, 0, 1), Vec3(0, 20, 0))) mass = 1 fallInertia = ballShape.calculateLocalInertia(mass) ci = RigidBodyConstructionInfo(mass, fallRigidBodyState, ballShape, fallInertia) fallRigidBody = RigidBody(ci) fallRigidBody.setRestitution(0.5) sim.addRigidBody(fallRigidBody) del mass, fallInertia, ci # Step the simulation and print the position of the Ball. for ii in range(10): sim.stepSimulation(0.5, 30) ms = fallRigidBody.getMotionState() wt = ms.getWorldTransform() print(wt.getOrigin()) # Remove the rigid bodies from the simulation.