Skip to content

BodyNode

hhh edited this page Mar 22, 2022 · 3 revisions

BodyNode

/**
 * Type of projection result.
 */
interface Projection {
    min: number;
    max: number;
}

/**
 * Type of data of collision events.
 */
interface BodyNodeCollisionEventData extends CollisionInfo {
    /**
     * The other body that collides with this one.
     */
    target: BodyNode;
}

/**
 * Emits on collision.
 * (stoppable & cancelable;
 * Invoke `event.cancel` to prevent default handling.)
 */
type BodyNodeCollisionEvent = Event<'collision', BodyNodeCollisionEventData>;

/**
 * Type of data of drag events.
 */
interface BodyNodeDragEventData {
    /**
     * Pointer identity.
     * (Not available when the event is triggered from other sources.)
     */
    id: number | null;
    /**
     * Pointer x. (absolute)
     */
    x: number;
    /**
     * Pointer y. (absolute)
     */
    y: number;
    /**
     * The original DOM event.
     * (Not available when the event is triggered from other sources.)
     */
    rawEvent: MouseEvent | TouchEvent | null;
}

/**
 * Emits when drag starts. (stoppable & cancelable)
 */
type BodyNodeDragStartEvent = Event<'dragstart', BodyNodeDragEventData>;

/**
 * Emits on dragging. (stoppable & cancelable)
 */
type BodyNodeDragMoveEvent = Event<'dragmove', BodyNodeDragEventData>;

/**
 * Emits when drag ends. (stoppable)
 */
type BodyNodeDragEndEvent = Event<'dragend', BodyNodeDragEventData>;

/**
 * Type of events on {@link BodyNode}.
 */
interface BodyNodeEvents extends CanvasNodeEvents {
    collision: BodyNodeCollisionEvent;
    dragstart: BodyNodeDragStartEvent;
    dragmove: BodyNodeDragMoveEvent;
    dragend: BodyNodeDragEndEvent;
}

/**
 * Type of options for {@link BodyNode}.
 */
type BodyNodeOptions<Events extends BodyNodeEvents> = (PolygonNodeOptions<Events> & Partial<{
    /**
     * Whether the object is active. (not static)
     * @default true
     */
    active: boolean;
    /**
     * Whether the object is draggable.
     * (Drag is handled by `WorldNode`.)
     * @default false
     */
    draggable: boolean;
    /**
     * The category of the object.
     * (`Category.for` will be invoked
     * to get the actual category
     * if the input is not a number.)
     * @default 0
     */
    category: number | CategoryTag;
    /**
     * The collision filter of the object.
     * @default Category.FULL_MASK
     */
    collisionFilter: number;
    /**
     * The sensor filter of the object.
     * @default 0
     */
    sensorFilter: number;
    /**
     * The velocity of the object.
     */
    velocity: Vector;
    /**
     * The accelaration of the object.
     */
    accelaration: Vector;
    /**
     * The gravity of the object.
     * @default null
     */
    gravity: Vector | null;
    /**
     * The stiffness of the object. (0~1)
     * @default 0.95
     */
    stiffness: number;
    /**
     * The elasticity of the object.
     * @default 0.3
     */
    elasticity: number;
    /**
     * The friction coefficient of the object.
     * @default 0.3
     */
    friction: number;
    /**
     * The static friction coefficient of the object.
     * @default 0.4
     */
    staticFriction: number;
    /**
     * The coefficient of air friction
     * that constantly slows down the object.
     * @default 0
     */
    airFriction: number;
    /**
     * The density of the object.
     * (This affects the mass of the object.)
     * @default 1
     */
    density: number;
    /**
     * The mass of the object.
     * (This affects the density of the object.)
     * @default area * density
     */
    mass: number;
    /**
     * The acceptable amount of overlap.
     * @default 0.1
     */
    slop: number;
    /**
     * Whether the vertices are placed clockwise.
     * (mathematically, not on canvas)
     * @default true
     */
    clockwise: boolean;
    /**
     * The precision(fraction digits) of tangent values of normal vectors.
     * (This helps reduce similar normal vectors.)
     * @default 6
     */
    normalPrecision: number;
}>);

/**
 * Class of rigid bodies.
 */
class BodyNode<Events extends BodyNodeEvents = BodyNodeEvents> extends PolygonNode<Events> {

    /**
     * The maximum speed that an object is considered static.
     * @default 0.02
     */
    static maxStaticSpeed: number;

    /**
     * Constructor of {@link BodyNode}.
     */
    constructor(options?: BodyNodeOptions<Events>);

    /**
     * @override PolygonNode.tag
     * @default 'body'
     */
    readonly tag: string;

    /**
     * The contacted bodies.
     * (Updated internally.)
     */
    readonly contacts: Set<BodyNode<any>>;

    /**
     * Whether the object is active. (not static)
     * @default true
     */
    active: boolean;

    /**
     * Whether the object is draggable.
     * (Drag is handled by `WorldNode`.)
     * @default false
     */
    draggable: boolean;

    /**
     * The category of the object.
     * @default 0
     */
    category: number;

    /**
     * The collision filter of the object.
     * @default Category.FULL_MASK
     */
    collisionFilter: number;

    /**
     * The area of the object.
     */
    area: number;

    /**
     * The sensor filter of the object.
     * @default 0
     */
    sensorFilter: number;

    /**
     * The velocity of the object.
     */
    velocity: Vector;

    /**
     * The accelaration of the object.
     */
    accelaration: Vector;

    /**
     * The gravity of the object.
     * @default null
     */
    gravity: Vector | null;

    /**
     * The impulse of the object.
     * (Updated internally.)
     */
    impulse: Vector;

    /**
     * The stiffness of the object. (0~1)
     * @default 0.95
     */
    stiffness: number;

    /**
     * The elasticity of the object.
     * @default 0.3
     */
    elasticity: number;

    /**
     * The friction coefficient of the object.
     * @default 0.3
     */
    friction: number;

    /**
     * The static friction coefficient of the object.
     * @default 0.4
     */
    staticFriction: number;

    /**
     * The coefficient of air friction
     * that constantly slows down the object.
     * @default 0
     */
    airFriction: number;

    /**
     * The acceptable amount of overlap.
     * @default 0.1
     */
    slop: number;

    /**
     * Whether the vertices are placed clockwise.
     * (mathematically, not on canvas)
     * @default true
     */
    clockwise: boolean;

    /**
     * The precision(fraction digits) of tangent values of normal vectors.
     * (This helps reduce similar normal vectors.)
     * @default 6
     */
    normalPrecision: number;

    /**
     * Normal vectors of the polygon. (normalized)
     */
    normals: Vector[];

    /**
     * Get the density of the object.
     */
    get density(): number;

    /**
     * Set the density of the object.
     * (This also updates the mass of the object.)
     */
    set density(density: number);

    /**
     * Get the mass of the object.
     */
    get mass(): number;

    /**
     * Set the mass of the object.
     * (This also updates the density of the object.)
     */
    set mass(mass: number);

    /**
     * @override PolygonNode.updateVertices
     */
    updateVertices(vertices: Vector[]): void;

    /**
     * Get the projection of the body on specific direction.
     */
    project(direction: VectorLike): Projection;

    /**
     * @override CanvasNode.beforeUpdate
     */
    protected beforeUpdate(timeStamp: number): void;
}
Clone this wiki locally