Skip to content

⬅️ Back to Table of Contents

📄 Capsule.js

📊 Analysis Summary

Metric Count
🔧 Functions 7
🧱 Classes 1
📦 Imports 1

📚 Table of Contents

🛠️ File Location:

📂 examples/jsm/math/Capsule.js

📦 Imports

Name Source
Vector3 three

Functions

Capsule.clone(): Capsule

JSDoc:

/**
     * Returns a new capsule with copied values from this instance.
     *
     * @return {Capsule} A clone of this instance.
     */

Returns: Capsule

Calls:

  • new this.constructor().copy
Code
clone() {

        return new this.constructor().copy( this );

    }

Capsule.set(start: Vector3, end: Vector3, radius: number): Capsule

JSDoc:

/**
     * Sets the capsule components to the given values.
     * Please note that this method only copies the values from the given objects.
     *
     * @param {Vector3} start - The start vector.
     * @param {Vector3} end - The end vector
     * @param {number} radius - The capsule's radius.
     * @return {Capsule} A reference to this capsule.
     */

Parameters:

  • start Vector3
  • end Vector3
  • radius number

Returns: Capsule

Calls:

  • this.start.copy
  • this.end.copy
Code
set( start, end, radius ) {

        this.start.copy( start );
        this.end.copy( end );
        this.radius = radius;

        return this;

    }

Capsule.copy(capsule: Capsule): Capsule

JSDoc:

/**
     * Copies the values of the given capsule to this instance.
     *
     * @param {Capsule} capsule - The capsule to copy.
     * @return {Capsule} A reference to this capsule.
     */

Parameters:

  • capsule Capsule

Returns: Capsule

Calls:

  • this.start.copy
  • this.end.copy
Code
copy( capsule ) {

        this.start.copy( capsule.start );
        this.end.copy( capsule.end );
        this.radius = capsule.radius;

        return this;

    }

Capsule.getCenter(target: Vector3): Vector3

JSDoc:

/**
     * Returns the center point of this capsule.
     *
     * @param {Vector3} target - The target vector that is used to store the method's result.
     * @return {Vector3} The center point.
     */

Parameters:

  • target Vector3

Returns: Vector3

Calls:

  • target.copy( this.end ).add( this.start ).multiplyScalar
Code
getCenter( target ) {

        return target.copy( this.end ).add( this.start ).multiplyScalar( 0.5 );

    }

Capsule.translate(v: Vector3): Capsule

JSDoc:

/**
     * Adds the given offset to this capsule, effectively moving it in 3D space.
     *
     * @param {Vector3} v - The offset that should be used to translate the capsule.
     * @return {Capsule} A reference to this capsule.
     */

Parameters:

  • v Vector3

Returns: Capsule

Calls:

  • this.start.add
  • this.end.add
Code
translate( v ) {

        this.start.add( v );
        this.end.add( v );

        return this;

    }

Capsule.intersectsBox(box: Box3): boolean

JSDoc:

/**
     * Returns `true` if the given bounding box intersects with this capsule.
     *
     * @param {Box3} box - The bounding box to test.
     * @return {boolean} Whether the given bounding box intersects with this capsule.
     */

Parameters:

  • box Box3

Returns: boolean

Calls:

  • checkAABBAxis
Code
intersectsBox( box ) {

        return (
            checkAABBAxis(
                this.start.x, this.start.y, this.end.x, this.end.y,
                box.min.x, box.max.x, box.min.y, box.max.y,
                this.radius ) &&
            checkAABBAxis(
                this.start.x, this.start.z, this.end.x, this.end.z,
                box.min.x, box.max.x, box.min.z, box.max.z,
                this.radius ) &&
            checkAABBAxis(
                this.start.y, this.start.z, this.end.y, this.end.z,
                box.min.y, box.max.y, box.min.z, box.max.z,
                this.radius )
        );

    }

checkAABBAxis(p1x: any, p1y: any, p2x: any, p2y: any, minx: any, maxx: any, miny: any, maxy: any, radius: any): boolean

Parameters:

  • p1x any
  • p1y any
  • p2x any
  • p2y any
  • minx any
  • maxx any
  • miny any
  • maxy any
  • radius any

Returns: boolean

Code
function checkAABBAxis( p1x, p1y, p2x, p2y, minx, maxx, miny, maxy, radius ) {

    return (
        ( minx - p1x < radius || minx - p2x < radius ) &&
        ( p1x - maxx < radius || p2x - maxx < radius ) &&
        ( miny - p1y < radius || miny - p2y < radius ) &&
        ( p1y - maxy < radius || p2y - maxy < radius )
    );

}

Classes

Capsule

Class Code
class Capsule {

    /**
     * Constructs a new capsule.
     *
     * @param {Vector3} [start] - The start vector.
     * @param {Vector3} [end] - The end vector.
     * @param {number} [radius=1] - The capsule's radius.
     */
    constructor( start = new Vector3( 0, 0, 0 ), end = new Vector3( 0, 1, 0 ), radius = 1 ) {

        /**
         * The start vector.
         *
         * @type {Vector3}
         */
        this.start = start;

        /**
         * The end vector.
         *
         * @type {Vector3}
         */
        this.end = end;

        /**
         * The capsule's radius.
         *
         * @type {number}
         * @default 1
         */
        this.radius = radius;

    }

    /**
     * Returns a new capsule with copied values from this instance.
     *
     * @return {Capsule} A clone of this instance.
     */
    clone() {

        return new this.constructor().copy( this );

    }

    /**
     * Sets the capsule components to the given values.
     * Please note that this method only copies the values from the given objects.
     *
     * @param {Vector3} start - The start vector.
     * @param {Vector3} end - The end vector
     * @param {number} radius - The capsule's radius.
     * @return {Capsule} A reference to this capsule.
     */
    set( start, end, radius ) {

        this.start.copy( start );
        this.end.copy( end );
        this.radius = radius;

        return this;

    }

    /**
     * Copies the values of the given capsule to this instance.
     *
     * @param {Capsule} capsule - The capsule to copy.
     * @return {Capsule} A reference to this capsule.
     */
    copy( capsule ) {

        this.start.copy( capsule.start );
        this.end.copy( capsule.end );
        this.radius = capsule.radius;

        return this;

    }

    /**
     * Returns the center point of this capsule.
     *
     * @param {Vector3} target - The target vector that is used to store the method's result.
     * @return {Vector3} The center point.
     */
    getCenter( target ) {

        return target.copy( this.end ).add( this.start ).multiplyScalar( 0.5 );

    }

    /**
     * Adds the given offset to this capsule, effectively moving it in 3D space.
     *
     * @param {Vector3} v - The offset that should be used to translate the capsule.
     * @return {Capsule} A reference to this capsule.
     */
    translate( v ) {

        this.start.add( v );
        this.end.add( v );

        return this;

    }

    /**
     * Returns `true` if the given bounding box intersects with this capsule.
     *
     * @param {Box3} box - The bounding box to test.
     * @return {boolean} Whether the given bounding box intersects with this capsule.
     */
    intersectsBox( box ) {

        return (
            checkAABBAxis(
                this.start.x, this.start.y, this.end.x, this.end.y,
                box.min.x, box.max.x, box.min.y, box.max.y,
                this.radius ) &&
            checkAABBAxis(
                this.start.x, this.start.z, this.end.x, this.end.z,
                box.min.x, box.max.x, box.min.z, box.max.z,
                this.radius ) &&
            checkAABBAxis(
                this.start.y, this.start.z, this.end.y, this.end.z,
                box.min.y, box.max.y, box.min.z, box.max.z,
                this.radius )
        );

    }

}

Methods

clone(): Capsule
Code
clone() {

        return new this.constructor().copy( this );

    }
set(start: Vector3, end: Vector3, radius: number): Capsule
Code
set( start, end, radius ) {

        this.start.copy( start );
        this.end.copy( end );
        this.radius = radius;

        return this;

    }
copy(capsule: Capsule): Capsule
Code
copy( capsule ) {

        this.start.copy( capsule.start );
        this.end.copy( capsule.end );
        this.radius = capsule.radius;

        return this;

    }
getCenter(target: Vector3): Vector3
Code
getCenter( target ) {

        return target.copy( this.end ).add( this.start ).multiplyScalar( 0.5 );

    }
translate(v: Vector3): Capsule
Code
translate( v ) {

        this.start.add( v );
        this.end.add( v );

        return this;

    }
intersectsBox(box: Box3): boolean
Code
intersectsBox( box ) {

        return (
            checkAABBAxis(
                this.start.x, this.start.y, this.end.x, this.end.y,
                box.min.x, box.max.x, box.min.y, box.max.y,
                this.radius ) &&
            checkAABBAxis(
                this.start.x, this.start.z, this.end.x, this.end.z,
                box.min.x, box.max.x, box.min.z, box.max.z,
                this.radius ) &&
            checkAABBAxis(
                this.start.y, this.start.z, this.end.y, this.end.z,
                box.min.y, box.max.y, box.min.z, box.max.z,
                this.radius )
        );

    }