This is a quick overview tutorial of the Capsule primitive in the Away3D TypeScript library.
We will be recreating the scene as seen above.
To use a Capsule primitive, we’ll need to:
- Declare, instantiate, and initialize a new Capsule object.
- Declare, instantiate, and initialize a new Mesh object.
- Assign the Capsule object to the Mesh object.
- Add the Mesh object to the scene.
This tutorial builds off of the Away3D TypeScript: Template. All the new or changed code will be highlighted in the full code block below.
An error has occurred. Please try again later.
Line 15 declares a variable named _objectMesh of type Mesh().
Line 53 instantiates a new Mesh() object referenced by the variable _objectMesh. Then _objectMesh was initialized by calling the getNewObject method on a new PrimitiveCapsulePrefab object that was cast-typed to type Mesh. We’ll discuss the values passed to the prefab later.
Line 54 adds the Mesh object containing the defined prefab geometry to the scene.
Lines 67-68 adds a spin to our Mesh object on every frame of animation.
That’s it, our object is in the scene.
However, we glazed over the initialization process, let’s take a closer look at the object’s constructor.
The constructor looks as follows:
PrimitiveCapsulePrefab(radius:Number = 50, height:Number = 100, segmentsW:uint = 16, segmentsH:uint = 12, yUp:Boolean = true)
The Constructor’s Parameters:
- radius:Number (default = 50) — The radius of the capsule.
- height:Number (default = 100) — The height of the capsule.
- segmentsW:uint (default = 16) — Defines the number of horizontal segments that make up the capsule. Defaults to 16.
- segmentsH:uint (default = 12) — Defines the number of vertical segments that make up the capsule. Defaults to 12.
- yUp:Boolean (default = true) — Defines whether the capsule poles should lay on the Y-axis (true) or on the Z-axis (false).
So in our initialization we stated:
this._objectMesh = <away.entities.Mesh> new away.prefabs.PrimitiveCapsulePrefab(100, 200, 16, 12, true).getNewObject();
Which created a Capsule:
- 100 units radius. (Half of the circular width).
- 200 units high (or long depending how you look at it).
- 16 segments in the circular width.
- 12 segments along the length.
- We said true to that the capsule’s poles should run from top down along the Y axis.
Adding a basic primitive is relatively easy. Just understanding the constructor and what the parameters are, is the hardest part. You can look at the official documentation here.