melonJS
    Preparing search index...

    Class Box3d

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

    This is the shape that lets a body collide along Z as well as X and Y. Every other built-in shape (Polygon, Rect, RoundRect, Ellipse) is planar and is 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.

    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

    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.

    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