Adding some rigid bodies

Now, we will create individual bodies and set their properties and put them into our dynamics world, as follows:

   modelbuilder.begin();
MeshPartBuilder mpb = modelbuilder.part("parts", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.Color, new Material(ColorAttribute.createDiffuse(Color.WHITE)));
   mpb.setColor(1f, 1f, 1f, 1f);
   mpb.box(0, 0, 0, 40, 1, 40);
   Model model = modelbuilder.end();
groundInstance = new ModelInstance(model);


   btCollisionShape groundshape = new btBoxShape(new Vector3(20, 1 / 2f, 20));
   btRigidBodyConstructionInfo bodyInfo = new btRigidBodyConstructionInfo(0, null, groundshape, Vector3.Zero);
   btRigidBody body = new btRigidBody(bodyInfo);
   world.addRigidBody(body);

The preceding steps are to create the ground. In our program, the ground is simply a box with very low height. Using the mesh builder, we create a box with width, height, and depth as 40, 1, and 40 units respectively. Remember, this is simply a visual model that we created. For actual collision, we need to create a physics body with btCollisionShape. However, btCollisionShape is a base class intended for low-level usage. Hence, we use btBoxShape, which inherits btCollisionShape that creates a box primitive around the origin, its side axis aligned with length specified by half extents, in local shape coordinates. Similar to the box shape, we can also create a sphere, a cone, a cylinder, a capsule, an arrow, and so on.

To create btRigidBodyConstructionInfo, we need to specify the mass, motion state, collision shape, and the local inertia. Here, we we defined the mass as zero zero and local inertia as zero vector. This is because our ground body is static. Zero mass isn't physically possible. It is used to indicate that the ground should not respond to any forces applied to it. It should always stay at the same location (and rotation), regardless of any forces or collisions that may be applied to it. This is called a static object. The other objects (with a mass greater than zero) are called dynamic objects. Since our ground is static, we don't have to provide any motion state either.

Note

Static objects do not need a motion state because they do not move.

Finally, we create the rigid body feeding bodyInfo to the btRigidBody constructor. This rigid body is then added to our dynamics physics world by calling world.addRigidBody(body).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.172.130