Class ConvexPolyhedron

A set of polygons describing a convex shape.

The shape MUST be convex for the code to work properly. No polygons may be coplanar (contained in the same 3D plane), instead these should be merged into one polygon.

Author

qiao / https://github.com/qiao (original author, see https://github.com/qiao/three.js/commit/85026f0c769e4000148a67d45a9e9b9c5108836f)

Author

schteppe / https://github.com/schteppe

See

https://www.altdevblogaday.com/2011/05/13/contact-generation-between-3d-convex-meshes/

Todo

Move the clipping functions to ContactGenerator?

Todo

Automatically merge coplanar polygons in constructor.

Example

const convexShape = new CANNON.ConvexPolyhedron({ vertices, faces })
const convexBody = new CANNON.Body({ mass: 1, shape: convexShape })
world.addBody(convexBody)

Hierarchy (view full)

Constructors

  • Parameters

    • Optional props: {
          axes?: Vec3[];
          boundingSphereRadius?: number;
          faces?: number[][];
          normals?: Vec3[];
          vertices?: Vec3[];
      }
      • Optional axes?: Vec3[]

        axes

      • Optional boundingSphereRadius?: number

        boundingSphereRadius

      • Optional faces?: number[][]

        Array of integer arrays, describing which vertices that is included in each face.

      • Optional normals?: Vec3[]

        normals

      • Optional vertices?: Vec3[]

        An array of Vec3's

    Returns ConvexPolyhedron

Properties

body: Body

The body to which the shape is added to.

boundingSphereRadius: number

The local bounding sphere radius of this shape.

collisionFilterGroup: number

Default

1
collisionFilterMask: number

Default

-1
collisionResponse: boolean

Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled.

Default

true
faceNormals: Vec3[]

faceNormals

faces: number[][]

Array of integer arrays, indicating which vertices each face consists of

id: number

Identifier of the Shape.

material: Material

Optional material of the shape that regulates contact properties.

type: 0 | ShapeType

The type of this shape. Must be set to an int > 0 by subclasses.

uniqueAxes: Vec3[]

If given, these locally defined, normalized axes are the only ones being checked when doing separating axis check.

uniqueEdges: Vec3[]

uniqueEdges

vertices: Vec3[]

vertices

worldFaceNormals: Vec3[]

worldFaceNormals

worldFaceNormalsNeedsUpdate: boolean

worldFaceNormalsNeedsUpdate

worldVertices: Vec3[]

worldVertices

worldVerticesNeedsUpdate: boolean

worldVerticesNeedsUpdate

idCounter: number
types: {
    BOX: 4;
    COMPOUND: 8;
    CONVEXPOLYHEDRON: 16;
    CYLINDER: 128;
    HEIGHTFIELD: 32;
    PARTICLE: 64;
    PLANE: 2;
    SPHERE: 1;
    TRIMESH: 256;
}

All the Shape types.

Type declaration

  • Readonly BOX: 4

    BOX

  • Readonly COMPOUND: 8

    COMPOUND

  • Readonly CONVEXPOLYHEDRON: 16

    CONVEXPOLYHEDRON

  • Readonly CYLINDER: 128

    CYLINDER

  • Readonly HEIGHTFIELD: 32

    HEIGHTFIELD

  • Readonly PARTICLE: 64

    PARTICLE

  • Readonly PLANE: 2

    PLANE

  • Readonly SPHERE: 1

    SPHERE

  • Readonly TRIMESH: 256

    TRIMESH

Methods

  • Clip a face against a hull.

    Parameters

    Returns void

  • Clip a face in a hull against the back of a plane.

    Parameters

    • inVertices: Vec3[]
    • outVertices: Vec3[]
    • planeNormal: Vec3
    • planeConstant: number

      The constant in the mathematical plane equation

    Returns Vec3[]

  • Computes uniqueEdges

    Returns void

  • Parameters

    Returns void

  • Compute the normals of the faces. Will reuse existing Vec3 objects in the faceNormals array if they exist.

    Returns void

  • Updates worldVertices and sets worldVerticesNeedsUpdate to false.

    Parameters

    Returns void

  • Updates .worldVertices and sets .worldVerticesNeedsUpdate to false.

    Parameters

    Returns void

  • Find the separating axis between this hull and another

    Parameters

    Returns boolean

    Returns false if a separation is found, else true

  • Get an average of all the vertices positions

    Parameters

    • Optional target: Vec3

    Returns Vec3

  • Compute the normal of a face from its vertices

    Parameters

    • i: number
    • target: Vec3

    Returns void

  • Parameters

    • face_i: number

      Index of the face

    Returns number

  • Checks whether p is inside the polyhedra. Must be in local coords. The point lies outside of the convex hull of the other points if and only if the direction of all the vectors from it to those other points are on less than one half of a sphere around it.

    Parameters

    • p: Vec3

      A point given in local coordinates

    Returns false | 1 | -1

  • Test separating axis against two hulls. Both hulls are projected onto the axis and the overlap size is returned if there is one.

    Parameters

    Returns number | false

    The overlap depth, or FALSE if no penetration.

  • Transform all local points. Will change the .vertices

    Parameters

    Returns void

  • Get approximate convex volume

    Returns number

  • Get face normal given 3 vertices

    Parameters

    Returns void

  • Get max and min dot product of a convex hull at position (pos,quat) projected onto an axis. Results are saved in the array maxmin.

    Parameters

    Returns void