@newkrok/nape-js 3.22.0 → 3.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1656,6 +1656,146 @@ declare class TriggerZone {
1656
1656
  private _removeListener;
1657
1657
  }
1658
1658
 
1659
+ /**
1660
+ * Voronoi diagram generation for 2D point sets.
1661
+ *
1662
+ * Uses the half-plane intersection method: for each site, start with the
1663
+ * bounding box and clip by the perpendicular bisector against every other site.
1664
+ * This is O(n²) but perfectly robust for the fracture use-case (typically 4–30
1665
+ * sites). Every cell is guaranteed to be a finite, closed, convex polygon.
1666
+ *
1667
+ * Designed for use in fracture/destruction systems.
1668
+ */
1669
+ /** A 2D point (plain object — avoids coupling to Vec2 pooling). */
1670
+ interface VoronoiPoint {
1671
+ x: number;
1672
+ y: number;
1673
+ }
1674
+ /** A single Voronoi cell: the site that generated it and its polygon vertices (CCW). */
1675
+ interface VoronoiCell {
1676
+ /** The generating site index (into the original points array). */
1677
+ siteIndex: number;
1678
+ /** The generating site coordinates. */
1679
+ site: VoronoiPoint;
1680
+ /** Cell polygon vertices in counter-clockwise order. */
1681
+ vertices: VoronoiPoint[];
1682
+ }
1683
+ /** Result of a Voronoi diagram computation. */
1684
+ interface VoronoiResult {
1685
+ /** One cell per input site, in the same order as the input points. */
1686
+ cells: VoronoiCell[];
1687
+ }
1688
+ /**
1689
+ * Compute the Voronoi diagram for a set of 2D points, clipped to a bounding box.
1690
+ *
1691
+ * Uses half-plane intersection: for each site, clips the bounding rectangle by
1692
+ * the perpendicular bisector with every other site. Produces one convex cell per
1693
+ * site, all cells together tile the bounding box exactly.
1694
+ *
1695
+ * @param points - Array of site points. Must contain at least 1 point.
1696
+ * @param bounds - Clipping rectangle `{ minX, minY, maxX, maxY }`.
1697
+ * @returns A `VoronoiResult` containing one cell per input point.
1698
+ *
1699
+ * @example
1700
+ * ```ts
1701
+ * const result = computeVoronoi(
1702
+ * [{ x: 10, y: 10 }, { x: 90, y: 50 }, { x: 50, y: 90 }],
1703
+ * { minX: 0, minY: 0, maxX: 100, maxY: 100 },
1704
+ * );
1705
+ * for (const cell of result.cells) {
1706
+ * console.log(`Site ${cell.siteIndex}:`, cell.vertices);
1707
+ * }
1708
+ * ```
1709
+ */
1710
+ declare function computeVoronoi(points: ReadonlyArray<Readonly<VoronoiPoint>>, bounds: {
1711
+ minX: number;
1712
+ minY: number;
1713
+ maxX: number;
1714
+ maxY: number;
1715
+ }): VoronoiResult;
1716
+ /**
1717
+ * Generate random Voronoi fracture sites within a polygon.
1718
+ *
1719
+ * Places `count` random points inside the given polygon using rejection
1720
+ * sampling. Useful for generating fracture patterns.
1721
+ *
1722
+ * @param vertices - Polygon vertices (as VoronoiPoint[]).
1723
+ * @param count - Number of sites to generate.
1724
+ * @param random - Optional RNG function (default `Math.random`).
1725
+ * @returns Array of points guaranteed to be inside the polygon.
1726
+ */
1727
+ declare function generateFractureSites(vertices: ReadonlyArray<Readonly<VoronoiPoint>>, count: number, random?: () => number): VoronoiPoint[];
1728
+
1729
+ /**
1730
+ * Options for body fracture.
1731
+ */
1732
+ interface FractureOptions {
1733
+ /** Number of fracture fragments to generate. Default `8`. */
1734
+ fragmentCount?: number;
1735
+ /**
1736
+ * Material to apply to all fragment shapes.
1737
+ * If not specified, the first shape's material is copied from the original body.
1738
+ */
1739
+ material?: Material;
1740
+ /** Interaction filter for all fragment shapes. */
1741
+ filter?: InteractionFilter;
1742
+ /**
1743
+ * Explosion impulse magnitude applied radially from the impact point.
1744
+ * Default `0` (no explosion impulse — fragments inherit body velocity).
1745
+ */
1746
+ explosionImpulse?: number;
1747
+ /**
1748
+ * Custom RNG function for reproducible fracture patterns.
1749
+ * Default `Math.random`.
1750
+ */
1751
+ random?: () => number;
1752
+ /**
1753
+ * If true, automatically add fragments to the same space as the original body
1754
+ * and remove the original body. Default `true`.
1755
+ */
1756
+ addToSpace?: boolean;
1757
+ /**
1758
+ * Custom Voronoi site positions (in body-local space).
1759
+ * If provided, `fragmentCount` is ignored and these exact sites are used.
1760
+ */
1761
+ sites?: VoronoiPoint[];
1762
+ }
1763
+ /**
1764
+ * Result of a fracture operation.
1765
+ */
1766
+ interface FractureResult {
1767
+ /** The generated fragment bodies. */
1768
+ fragments: Body[];
1769
+ /** The original body that was fractured (removed from space if `addToSpace` is true). */
1770
+ originalBody: Body;
1771
+ }
1772
+ /**
1773
+ * Fracture a body into multiple pieces using Voronoi decomposition.
1774
+ *
1775
+ * Takes the first polygon shape on the body, generates Voronoi cells within it,
1776
+ * clips each cell to the original polygon, and creates a new dynamic body for
1777
+ * each fragment. Fragments inherit the original body's linear and angular
1778
+ * velocity, plus an optional radial explosion impulse.
1779
+ *
1780
+ * @param body - The body to fracture. Must have at least one polygon shape.
1781
+ * @param impactPoint - World-space point where the fracture originates.
1782
+ * Used as the center bias for site generation and explosion impulse direction.
1783
+ * @param options - Fracture configuration.
1784
+ * @returns A `FractureResult` with the array of fragment bodies.
1785
+ *
1786
+ * @throws If the body has no polygon shapes.
1787
+ *
1788
+ * @example
1789
+ * ```ts
1790
+ * const result = fractureBody(box, Vec2.get(100, 50), {
1791
+ * fragmentCount: 12,
1792
+ * explosionImpulse: 200,
1793
+ * });
1794
+ * // result.fragments are already in the space
1795
+ * ```
1796
+ */
1797
+ declare function fractureBody(body: Body, impactPoint: Vec2, options?: FractureOptions): FractureResult;
1798
+
1659
1799
  declare const VERSION: string;
1660
1800
 
1661
- export { AABB, AngleJoint, Arbiter, Body, BodyCallback, BodyListener, BodyType, Callback, Capsule, CbEvent, CbType, CharacterController, type CharacterControllerOptions, Circle, CollisionArbiter, type ConcaveBodyOptions, Constraint, ConstraintCallback, ConstraintListener, Contact, DebugDrawFlags, DistanceJoint, Geom, GeomPoly, InteractionCallback, InteractionFilter, InteractionListener, InteractionType, Interactor, LineJoint, Listener, MarchingSquares, MatMN, Material, MotorJoint, type MoveResult, OptionType, PivotJoint, PreCallback, PreFlag, PreListener, PulleyJoint, Shape, ShapeType, Space, type TriggerHandler, TriggerZone, type TriggerZoneOptions, UserConstraint, VERSION, ValidationResult, Vec2, Vec3, WeldJoint, Winding, createConcaveBody };
1801
+ export { AABB, AngleJoint, Arbiter, Body, BodyCallback, BodyListener, BodyType, Callback, Capsule, CbEvent, CbType, CharacterController, type CharacterControllerOptions, Circle, CollisionArbiter, type ConcaveBodyOptions, Constraint, ConstraintCallback, ConstraintListener, Contact, DebugDrawFlags, DistanceJoint, type FractureOptions, type FractureResult, Geom, GeomPoly, InteractionCallback, InteractionFilter, InteractionListener, InteractionType, Interactor, LineJoint, Listener, MarchingSquares, MatMN, Material, MotorJoint, type MoveResult, OptionType, PivotJoint, PreCallback, PreFlag, PreListener, PulleyJoint, Shape, ShapeType, Space, type TriggerHandler, TriggerZone, type TriggerZoneOptions, UserConstraint, VERSION, ValidationResult, Vec2, Vec3, type VoronoiCell, type VoronoiPoint, type VoronoiResult, WeldJoint, Winding, computeVoronoi, createConcaveBody, fractureBody, generateFractureSites };
package/dist/index.d.ts CHANGED
@@ -1656,6 +1656,146 @@ declare class TriggerZone {
1656
1656
  private _removeListener;
1657
1657
  }
1658
1658
 
1659
+ /**
1660
+ * Voronoi diagram generation for 2D point sets.
1661
+ *
1662
+ * Uses the half-plane intersection method: for each site, start with the
1663
+ * bounding box and clip by the perpendicular bisector against every other site.
1664
+ * This is O(n²) but perfectly robust for the fracture use-case (typically 4–30
1665
+ * sites). Every cell is guaranteed to be a finite, closed, convex polygon.
1666
+ *
1667
+ * Designed for use in fracture/destruction systems.
1668
+ */
1669
+ /** A 2D point (plain object — avoids coupling to Vec2 pooling). */
1670
+ interface VoronoiPoint {
1671
+ x: number;
1672
+ y: number;
1673
+ }
1674
+ /** A single Voronoi cell: the site that generated it and its polygon vertices (CCW). */
1675
+ interface VoronoiCell {
1676
+ /** The generating site index (into the original points array). */
1677
+ siteIndex: number;
1678
+ /** The generating site coordinates. */
1679
+ site: VoronoiPoint;
1680
+ /** Cell polygon vertices in counter-clockwise order. */
1681
+ vertices: VoronoiPoint[];
1682
+ }
1683
+ /** Result of a Voronoi diagram computation. */
1684
+ interface VoronoiResult {
1685
+ /** One cell per input site, in the same order as the input points. */
1686
+ cells: VoronoiCell[];
1687
+ }
1688
+ /**
1689
+ * Compute the Voronoi diagram for a set of 2D points, clipped to a bounding box.
1690
+ *
1691
+ * Uses half-plane intersection: for each site, clips the bounding rectangle by
1692
+ * the perpendicular bisector with every other site. Produces one convex cell per
1693
+ * site, all cells together tile the bounding box exactly.
1694
+ *
1695
+ * @param points - Array of site points. Must contain at least 1 point.
1696
+ * @param bounds - Clipping rectangle `{ minX, minY, maxX, maxY }`.
1697
+ * @returns A `VoronoiResult` containing one cell per input point.
1698
+ *
1699
+ * @example
1700
+ * ```ts
1701
+ * const result = computeVoronoi(
1702
+ * [{ x: 10, y: 10 }, { x: 90, y: 50 }, { x: 50, y: 90 }],
1703
+ * { minX: 0, minY: 0, maxX: 100, maxY: 100 },
1704
+ * );
1705
+ * for (const cell of result.cells) {
1706
+ * console.log(`Site ${cell.siteIndex}:`, cell.vertices);
1707
+ * }
1708
+ * ```
1709
+ */
1710
+ declare function computeVoronoi(points: ReadonlyArray<Readonly<VoronoiPoint>>, bounds: {
1711
+ minX: number;
1712
+ minY: number;
1713
+ maxX: number;
1714
+ maxY: number;
1715
+ }): VoronoiResult;
1716
+ /**
1717
+ * Generate random Voronoi fracture sites within a polygon.
1718
+ *
1719
+ * Places `count` random points inside the given polygon using rejection
1720
+ * sampling. Useful for generating fracture patterns.
1721
+ *
1722
+ * @param vertices - Polygon vertices (as VoronoiPoint[]).
1723
+ * @param count - Number of sites to generate.
1724
+ * @param random - Optional RNG function (default `Math.random`).
1725
+ * @returns Array of points guaranteed to be inside the polygon.
1726
+ */
1727
+ declare function generateFractureSites(vertices: ReadonlyArray<Readonly<VoronoiPoint>>, count: number, random?: () => number): VoronoiPoint[];
1728
+
1729
+ /**
1730
+ * Options for body fracture.
1731
+ */
1732
+ interface FractureOptions {
1733
+ /** Number of fracture fragments to generate. Default `8`. */
1734
+ fragmentCount?: number;
1735
+ /**
1736
+ * Material to apply to all fragment shapes.
1737
+ * If not specified, the first shape's material is copied from the original body.
1738
+ */
1739
+ material?: Material;
1740
+ /** Interaction filter for all fragment shapes. */
1741
+ filter?: InteractionFilter;
1742
+ /**
1743
+ * Explosion impulse magnitude applied radially from the impact point.
1744
+ * Default `0` (no explosion impulse — fragments inherit body velocity).
1745
+ */
1746
+ explosionImpulse?: number;
1747
+ /**
1748
+ * Custom RNG function for reproducible fracture patterns.
1749
+ * Default `Math.random`.
1750
+ */
1751
+ random?: () => number;
1752
+ /**
1753
+ * If true, automatically add fragments to the same space as the original body
1754
+ * and remove the original body. Default `true`.
1755
+ */
1756
+ addToSpace?: boolean;
1757
+ /**
1758
+ * Custom Voronoi site positions (in body-local space).
1759
+ * If provided, `fragmentCount` is ignored and these exact sites are used.
1760
+ */
1761
+ sites?: VoronoiPoint[];
1762
+ }
1763
+ /**
1764
+ * Result of a fracture operation.
1765
+ */
1766
+ interface FractureResult {
1767
+ /** The generated fragment bodies. */
1768
+ fragments: Body[];
1769
+ /** The original body that was fractured (removed from space if `addToSpace` is true). */
1770
+ originalBody: Body;
1771
+ }
1772
+ /**
1773
+ * Fracture a body into multiple pieces using Voronoi decomposition.
1774
+ *
1775
+ * Takes the first polygon shape on the body, generates Voronoi cells within it,
1776
+ * clips each cell to the original polygon, and creates a new dynamic body for
1777
+ * each fragment. Fragments inherit the original body's linear and angular
1778
+ * velocity, plus an optional radial explosion impulse.
1779
+ *
1780
+ * @param body - The body to fracture. Must have at least one polygon shape.
1781
+ * @param impactPoint - World-space point where the fracture originates.
1782
+ * Used as the center bias for site generation and explosion impulse direction.
1783
+ * @param options - Fracture configuration.
1784
+ * @returns A `FractureResult` with the array of fragment bodies.
1785
+ *
1786
+ * @throws If the body has no polygon shapes.
1787
+ *
1788
+ * @example
1789
+ * ```ts
1790
+ * const result = fractureBody(box, Vec2.get(100, 50), {
1791
+ * fragmentCount: 12,
1792
+ * explosionImpulse: 200,
1793
+ * });
1794
+ * // result.fragments are already in the space
1795
+ * ```
1796
+ */
1797
+ declare function fractureBody(body: Body, impactPoint: Vec2, options?: FractureOptions): FractureResult;
1798
+
1659
1799
  declare const VERSION: string;
1660
1800
 
1661
- export { AABB, AngleJoint, Arbiter, Body, BodyCallback, BodyListener, BodyType, Callback, Capsule, CbEvent, CbType, CharacterController, type CharacterControllerOptions, Circle, CollisionArbiter, type ConcaveBodyOptions, Constraint, ConstraintCallback, ConstraintListener, Contact, DebugDrawFlags, DistanceJoint, Geom, GeomPoly, InteractionCallback, InteractionFilter, InteractionListener, InteractionType, Interactor, LineJoint, Listener, MarchingSquares, MatMN, Material, MotorJoint, type MoveResult, OptionType, PivotJoint, PreCallback, PreFlag, PreListener, PulleyJoint, Shape, ShapeType, Space, type TriggerHandler, TriggerZone, type TriggerZoneOptions, UserConstraint, VERSION, ValidationResult, Vec2, Vec3, WeldJoint, Winding, createConcaveBody };
1801
+ export { AABB, AngleJoint, Arbiter, Body, BodyCallback, BodyListener, BodyType, Callback, Capsule, CbEvent, CbType, CharacterController, type CharacterControllerOptions, Circle, CollisionArbiter, type ConcaveBodyOptions, Constraint, ConstraintCallback, ConstraintListener, Contact, DebugDrawFlags, DistanceJoint, type FractureOptions, type FractureResult, Geom, GeomPoly, InteractionCallback, InteractionFilter, InteractionListener, InteractionType, Interactor, LineJoint, Listener, MarchingSquares, MatMN, Material, MotorJoint, type MoveResult, OptionType, PivotJoint, PreCallback, PreFlag, PreListener, PulleyJoint, Shape, ShapeType, Space, type TriggerHandler, TriggerZone, type TriggerZoneOptions, UserConstraint, VERSION, ValidationResult, Vec2, Vec3, type VoronoiCell, type VoronoiPoint, type VoronoiResult, WeldJoint, Winding, computeVoronoi, createConcaveBody, fractureBody, generateFractureSites };