Skip to content
chandlerprall edited this page Nov 16, 2014 · 1 revision

Goblin has built-in support for multiple shapes. The most common use for these shapes is creating rigid bodies. It is best to re-use shapes whenever possible; if you need 5 boxes of the same size you can create the BoxShape once and use it in each of the five RigidBody constructors.

BoxShape

var box_shape = new Goblin.BoxShape( half_width, half_height, half_depth );

SphereShape

var sphere_shape = new Goblin.SphereShape( radius );

ConeShape

var cone_shape = new Goblin.ConeShape( radius, half_height );

CylinderShape

var cylinder_shape = new Goblin.CylinderShape( radius, half_height );

PlaneShape

var plane_shape = new Goblin.PlaneShape( orientation, half_width, half_length );

The first argument, orientation, determines the plane's normal axis. That is, an plane with orientation of 0 spans the YZ axes, orientation 1 spans XZ, and orientation 2 spans XY.

ConvexShape

var convex_shape = new Goblin.ConvexShape( vertices );

A convex shape finds the minimum convex hull containing vertices, an array of Goblin.Vector3s representing all of the vertices to be contained in the hull.

CompoundShape

var compound_shape = new Goblin.CompoundShape();

Compound shapes are comprised of any number of other shapes. This allows for a non-convex shape to be broken down into convex parts. To add a child shape you need the shape, a position offset, and a rotation offset.

// Adds a sphere shape as a child, 5 units up on the Y axis, relative to the center of the compound shape
compound_shape.addChildShape(
    new Goblin.SphereShape( 0.5 ),
    new Goblin.Vector3( 0, 5, 0 ),
    new Goblin.Quaternion()
);

MeshShape

var mesh_shape = new Goblin.MeshShape( vertices, faces );

A mesh shape is also called a triangle or concave shape. It allows a body to be concave without having to be split into convex parts. For best performance, keep the sizes of the faces large compared to objects which will be colliding with the mesh shape to reduce the number of tests needed each step.

The vertices parameter is an array containing all of the vertices in the mesh. faces is a flat array containing indices to the corresponding vertices. The first face uses the first three vertex indices in faces, the second face uses vertex indices 3,4,5, etc.

// Creates a mesh shape with two faces
var vertices = [
    new Goblin.Vector3( 0, 2, 0 ),
    new Goblin.Vector3( 1, 0, 0 ),
    new Goblin.Vector3( 0, 0, 1 ),
    new Goblin.Vector3( 0, -2, 0 ),
];
var faces = [
    0, 1, 2, // vectors for first face
    3, 2, 1 // vectors for the second face
];
var mesh_shape = new Goblin.MeshShape( vertices, faces );