How To: Create a Composite Rigid Body |
In some cases a rigid body consists of parts with different densities and materials. For example: A hammer has a metal head and a wooden shaft. This section shows how to model these kind of rigid bodies.
To model rigid bodies with different mass parts, create a rigid body with a CompositeShape. Normally, all child objects in the CompositeShape have the same density - but: If the child of a CompositeShape is a RigidBody, the mass properties of this child body are used.
Note |
---|
The children of a CompositeShape are of the type IGeometricObject and a RigidBody implements IGeometricObject. Therefore, a rigid body can be the child of a CompositeShape. |
Now, to model a hammer: Create a rigid body for the metal head. For example:
BoxShape headShape = new BoxShape(0.03f, 0.03f, 0.1f); MassFrame headMass = MassFrame.FromShapeAndDensity(headShape, Vector3F.One, 7850, 0.01f, 3); UniformMaterial metal = new UniformMaterial("Metal") { StaticFriction = 0.6f, DynamicFriction = 0.6f, Restitution = 0.2f, }; RigidBody head = new RigidBody(headShape, headMass, metal) { Pose = new Pose(new Vector3F(0, 0.1f, 0)) // The relative hammer head position in "hammer space". };
Create a second rigid body for the wooden shaft with different mass and different material properties:
CylinderShape shaftShape = new CylinderShape(0.015f, 0.2f); MassFrame shaftMass = MassFrame.FromShapeAndDensity(shaftShape, Vector3F.One, 700, 0.01f, 3); UniformMaterial wood = new UniformMaterial("Wood") { StaticFriction = 0.5f, DynamicFriction = 0.3f, Restitution = 0.2f, }; RigidBody shaft = new RigidBody(shaftShape, shaftMass, wood);
Then, add both rigid bodies to a composite shape:
CompositeShape hammerShape = new CompositeShape();
hammerShape.Children.Add(head);
hammerShape.Children.Add(shaft);
Use MassFrameFromShapeAndDensity(Shape, Vector3F, Single, Single, Int32) to compute the mass frame for the composite shape. FromShapeAndDensity automatically uses the mass properties of the child bodies:
MassFrame hammerMass = MassFrame.FromShapeAndDensity(hammerShape, Vector3F.One, 100, 0.01f, 3);
The density parameter in this method call is irrelevant because each child of the composite has pre-defined mass properties.
If the hammer uses a CompositeMaterial, the simulation will automatically use the materials of the child bodies:
CompositeMaterial compositeMaterial = new CompositeMaterial();
The first two bodies are only used to define the composite shape - they are never added to the simulation directly. Only the composite rigid body is added to the simulation:
RigidBody hammer = new RigidBody(hammerShape, hammerMass, compositeMaterial);
mySimulation.RigidBodies.Add(hammer);
For the simulation the hammer is a single rigid body. The child bodies are only used to define mass and material properties.