melonJS
    Preparing search index...

    Class Box3d

    An axis-aligned 3D box, usable as a Body collision shape.

    One of the two shapes that let a body collide along Z as well as X and Y, the other being Sphere. The planar shapes (Polygon, Rect, RoundRect, Ellipse) are resolved by the 2D SAT narrowphase, which can only ever produce a 2D pushback — see ResponseObject#overlapV. A Box3d pair is instead resolved by an AABB-vs-AABB narrowphase that also fills ResponseObject#overlapZ.

    A Box3d is world-axis-aligned and cannot be oriented, which makes it the shape for walls, floors and crates, and the wrong one for anything that turns. Reach for a Sphere there.

    Coordinate convention matches the rest of melonJS 3D code (see Camera3d): Y-down, +Z forward / away from the camera.

    Unlike Rect (top-left) and like Ellipse (center), pos is the box center. That matches how 3D objects are placed everywhere else in the engine — Mesh, Sprite3d and the ground-shadow footprint all work from a center plus half-extents — and it keeps the narrowphase free of corner/center conversions on the hot path.

    A Box3d can collide with a planar shape. The planar shape is treated as unbounded along Z (an infinitely extruded prism of its own outline), so the pair degrades to the ordinary 2D test on the XY footprint and the box's z never causes it to miss. This keeps an existing 2D game working unchanged when a single Box3d body is introduced: its world shapes go on colliding exactly as before. Use collisionType / collisionMask to opt specific shapes out of a 3D body.

    // a 64x16x64 floor slab centered on the origin
    const floor = new Box3d(0, 0, 0, 64, 16, 64);
    myFloor.body.addShape(floor);
    Index
    • Parameters

      • x: number = 0

        center of the box on the horizontal axis

      • y: number = 0

        center of the box on the vertical axis

      • z: number = 0

        center of the box on the depth axis

      • width: number = 0

        width of the box

      • height: number = 0

        height of the box

      • depth: number = 0

        depth of the box

      Returns Box3d

    collisionMask?: number
    collisionType?: number

    Optional per-shape collision settings, honoured by Body#addShape and defaulted there. Declared rather than initialized: the body writes them when the shape joins it, so a class field here would emit an undefined own property on every shape ever built. See addShape for the table of defaults and what each one does.

    Body#addShape

    halfExtents: Vector3d

    half the box size on each axis. Always non-negative; a negative extent passed to Box3d#setShape is stored as its magnitude, since a box with a mirrored axis has no meaning to the narrowphase.

    isActive?: boolean

    false takes this shape out of collision without removing it

    isTrigger?: boolean

    true reports contacts but skips the position correction

    the center of the box, as an offset from the owning body's position

    type: string = "Box3d"

    the shape type (used internally)

    "Box3d"
    
    • true if this box contains the given point

      Parameters

      • x: number | Vector3d

        point x, or a vector carrying the whole point

      • Optionaly: number = 0

        point y

      • Optionalz: number = 0

        point z

      Returns boolean

    • set new position and size for this box

      Parameters

      • x: number

        center of the box on the horizontal axis

      • y: number

        center of the box on the vertical axis

      • z: number

        center of the box on the depth axis

      • width: number

        width of the box

      • height: number

        height of the box

      • depth: number

        depth of the box

      Returns Box3d

      this box, for chaining