physics.d.ts 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  1. //////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2014-present, Egret Technology.
  4. // All rights reserved.
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright
  11. // notice, this list of conditions and the following disclaimer in the
  12. // documentation and/or other materials provided with the distribution.
  13. // * Neither the name of the Egret nor the
  14. // names of its contributors may be used to endorse or promote products
  15. // derived from this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  18. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  23. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  26. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. //
  28. //////////////////////////////////////////////////////////////////////////////////////
  29. declare module p2 {
  30. export class AABB {
  31. upperBound: number[];
  32. lowerBound: number[];
  33. constructor(options?: {
  34. upperBound?: number[];
  35. lowerBound?: number[];
  36. });
  37. containsPoint(point: number[]): boolean;
  38. setFromPoints(points: number[][], position: number[], angle: number, skinSize: number): void;
  39. copy(aabb: AABB): void;
  40. extend(aabb: AABB): void;
  41. overlaps(aabb: AABB): boolean;
  42. overlapsRay(ray:Ray): number;
  43. }
  44. export class Broadphase {
  45. static AABB: number;
  46. static BOUNDING_CIRCLE: number;
  47. static boundingRadiusCheck(bodyA: Body, bodyB: Body): boolean;
  48. static aabbCheck(bodyA: Body, bodyB: Body): boolean;
  49. static canCollide(bodyA: Body, bodyB: Body): boolean;
  50. constructor(type: number);
  51. result: Body[];
  52. world: World;
  53. boundingVolumeType: number;
  54. setWorld(world: World): void;
  55. getCollisionPairs(world: World): Body[];
  56. boundingVolumeCheck(bodyA: Body, bodyB: Body): boolean;
  57. }
  58. export class NaiveBroadphase extends Broadphase {
  59. aabbQuery(world: World, aabb: AABB, result: Body[]): Body[];
  60. }
  61. export class Narrowphase {
  62. static findSeparatingAxis(c1:Convex, offset1:number[], angle1:number, c2:Convex, offset2:number[], angle2:number, sepAxis:number[]): boolean;
  63. static getClosestEdge(c:Convex, angle:number, axis:number[], flip:boolean): number;
  64. static projectConvexOntoAxis(convexShape:Convex, convexOffset:number[], convexAngle:number, worldAxis:number[], result:number[]): void;
  65. contactEquationPool: ContactEquationPool;
  66. contactEquations: ContactEquation[];
  67. contactSkinSize: number;
  68. enabledEquations: boolean;
  69. enableFriction: boolean;
  70. frictionCoefficient: number;
  71. frictionEquationPool: FrictionEquationPool;
  72. frictionEquations: FrictionEquation[];
  73. frictionRelaxation: number;
  74. frictionStiffness: number;
  75. restitution: number;
  76. slipForce: number;
  77. stiffness: number;
  78. surfaceVelocity: number;
  79. //官方不赞成使用的属性
  80. enableFrictionReduction: boolean;
  81. bodiesOverlap(bodyA: Body, bodyB: Body): boolean;
  82. capsuleCapsule(bi: Body, si: Capsule, xi: number[], ai: number, bj: Body, sj: Capsule, xj: number[], aj: number): void;
  83. circleCapsule(bi: Body, si: Circle, xi: number[], ai: number, bj: Body, sj: Line, xj: number[], aj: number): void;
  84. circleCircle(bodyA: Body, shapeA: Circle, offsetA: number[], angleA: number, bodyB: Body, shapeB: Circle, offsetB: number[], angleB: number, justTest: boolean, radiusA?:number, radiusB?:number): void;
  85. circleConvex(circleBody:Body, circleShape:Circle, circleOffset:number[], circleAngle:number, convexBody:Body, convexShape:Convex, convexOffset:number[], convexAngle:number, justTest:boolean, circleRadius:number): void;
  86. circleHeightfield(bi:Body, si:Circle, xi:number[], bj:Body, sj:Heightfield, xj:number[], aj:number): void;
  87. circleLine(circleBody:Body, circleShape:Circle, circleOffset:number[], circleAngle:number, lineBody:Body, lineShape:Line, lineOffset:number[], lineAngle:number, justTest:boolean, lineRadius:number, circleRadius:number): void;
  88. circleParticle(circleBody:Body, circleShape:Circle, circleOffset:number[], circleAngle:number, particleBody:Body, particleShape:Particle, particleOffset:number[], particleAngle:number, justTest:boolean): void;
  89. circlePlane(bi:Body, si:Circle, xi:number[], bj:Body, sj:Plane, xj:number[], aj:number): void;
  90. collidedLastStep(bodyA: Body, bodyB: Body): boolean;
  91. convexCapsule(convexBody:Body, convexShape:Convex, convexPosition:number[], convexAngle:number, capsuleBody:Body, capsuleShape:Capsule, capsulePosition:number[], capsuleAngle:number): void;
  92. convexConvex(bi:Body, si:Convex, xi:number[], ai:number, bj:Body, sj:Convex, xj:number[], aj:number): void;
  93. convexLine(convexBody:Body, convexShape:Convex, convexOffset:number[], convexAngle:number, lineBody:Body, lineShape:Line, lineOffset:number[], lineAngle:number, justTest:boolean): void;
  94. createContactEquation(bodyA: Body, bodyB: Body): ContactEquation;
  95. createFrictionEquation(bodyA: Body,bodyB: Body): FrictionEquation;
  96. createFrictionFromContact(contactEquation: ContactEquation): FrictionEquation;
  97. lineBox(lineBody:Body, lineShape:Line, lineOffset:number[], lineAngle:number, boxBody:Body, boxShape:Box, boxOffset:number[], boxAngle:number, justTest:boolean): void;
  98. lineCapsule(lineBody:Body, lineShape:Line, linePosition:number[], lineAngle:number, capsuleBody:Body, capsuleShape:Capsule, capsulePosition:number[], capsuleAngle:number): void;
  99. lineLine(bodyA:Body, shapeA:Line, positionA:number[], angleA:number, bodyB:Body, shapeB:Line, positionB:number[], angleB:number): void;
  100. particleConvex(particleBody:Body, particleShape:Particle, particleOffset:number[], particleAngle:number, convexBody:Body, convexShape:Convex, convexOffset:number[], convexAngle:number, justTest:boolean): void;
  101. particlePlane(particleBody:Body, particleShape:Particle, particleOffset:number[], particleAngle:number, planeBody:Body, planeShape:Plane, planeOffset:number[], planeAngle:number, justTest:boolean): void;
  102. planeCapsule(planeBody:Body, planeShape:Circle, planeOffset:number[], planeAngle:number, capsuleBody:Body, capsuleShape:Particle, capsuleOffset:number[], capsuleAngle:number, justTest:boolean): void;
  103. planeConvex(planeBody:Body, planeShape:Plane, planeOffset:number[], planeAngle:number, convexBody:Body, convexShape:Convex, convexOffset:number[], convexAngle:number, justTest:boolean): void;
  104. planeLine(planeBody:Body, planeShape:Plane, planeOffset:number[], planeAngle:number, lineBody:Body, lineShape:Line, lineOffset:number[], lineAngle:number): void;
  105. reset(): void;
  106. }
  107. export class SAPBroadphase extends Broadphase {
  108. axisIndex: number;
  109. axisList: Body[];
  110. aabbQuery(world: World, aabb: AABB, result: Body[]): Body[];
  111. sortAxisList(a: Body[], axisIndex: number): Body[];
  112. }
  113. export class Constraint {
  114. static DISTANCE: number;
  115. static GEAR: number;
  116. static LOCK: number;
  117. static PRISMATIC: number;
  118. static REVOLUTE: number;
  119. constructor(bodyA: Body, bodyB: Body, type: number, options?: {
  120. collideConnected?: boolean;
  121. });
  122. type: number;
  123. equations: Equation[];
  124. bodyA: Body;
  125. bodyB: Body;
  126. collideConnected: boolean;
  127. update(): void;
  128. setStiffness(stiffness: number): void;
  129. setRelaxation(relaxation: number): void;
  130. }
  131. export class DistanceConstraint extends Constraint {
  132. constructor(bodyA: Body, bodyB: Body, options?: {
  133. distance?: number;
  134. localAnchorA?: number[];
  135. localAnchorB?: number[];
  136. maxForce?: number;
  137. });
  138. localAnchorA: number[];
  139. localAnchorB: number[];
  140. distance: number;
  141. maxForce: number;
  142. upperLimitEnabled: boolean;
  143. upperLimit: number;
  144. lowerLimitEnabled: boolean;
  145. lowerLimit: number;
  146. position: number;
  147. setMaxForce(maxForce: number): void;
  148. getMaxForce(): number;
  149. }
  150. export class GearConstraint extends Constraint {
  151. constructor(bodyA: Body, bodyB: Body, options?: {
  152. angle?: number;
  153. ratio?: number;
  154. maxTorque?: number;
  155. });
  156. ratio: number;
  157. angle: number;
  158. setMaxTorque(torque: number): void;
  159. getMaxTorque(): number;
  160. }
  161. export class LockConstraint extends Constraint {
  162. constructor(bodyA: Body, bodyB: Body, options?: {
  163. localOffsetB?: number[];
  164. localAngleB?: number;
  165. maxForce?: number;
  166. collideConnected?: boolean;
  167. });
  168. localAngleB: number;
  169. localOffsetB: number[];
  170. setMaxForce(force: number): void;
  171. getMaxForce(): number;
  172. }
  173. export class PrismaticConstraint extends Constraint {
  174. constructor(bodyA: Body, bodyB: Body, options?: {
  175. maxForce?: number;
  176. localAnchorA?: number[];
  177. localAnchorB?: number[];
  178. localAxisA?: number[];
  179. disableRotationalLock?: boolean;
  180. upperLimit?: number;
  181. lowerLimit?: number;
  182. collideConnected?: boolean;
  183. });
  184. localAnchorA: number[];
  185. localAnchorB: number[];
  186. localAxisA: number[];
  187. lowerLimit: number;
  188. lowerLimitEnabled: boolean;
  189. motorEnabled: boolean;
  190. motorEquation: Equation;
  191. motorSpeed: number;
  192. position: number;
  193. upperLimit: number;
  194. upperLimitEnabled: boolean;
  195. disableMotor(): void;
  196. enableMotor(): void;
  197. setLimits(lower: number, upper: number): void;
  198. }
  199. export class RevoluteConstraint extends Constraint {
  200. constructor(bodyA: Body, bodyB: Body, options?: {
  201. worldPivot?: number[];
  202. localPivotA?: number[];
  203. localPivotB?: number[];
  204. maxForce?: number;
  205. collideConnected?: boolean;
  206. });
  207. angle: number;
  208. lowerLimit: number;
  209. lowerLimitEnabled: boolean;
  210. motorEnabled: boolean;
  211. pivotA: number[];
  212. pivotB: number[];
  213. upperLimit: number;
  214. upperLimitEnabled: boolean;
  215. disableMotor(): void;
  216. enableMotor(): void;
  217. getMotorSpeed(): number;
  218. //官方不赞成使用了
  219. motorIsEnabled(): boolean;
  220. setLimits(lower: number, upper: number): void;
  221. setMotorSpeed(speed: number): void;
  222. }
  223. export class AngleLockEquation extends Equation {
  224. constructor(bodyA: Body, bodyB: Body, options?: {
  225. angle?: number;
  226. ratio?: number;
  227. });
  228. computeGq(): number;
  229. setRatio(ratio: number): number;
  230. setMaxTorque(torque: number): number;
  231. }
  232. export class ContactEquation extends Equation {
  233. constructor(bodyA: Body, bodyB: Body);
  234. contactPointA: number[];
  235. contactPointB: number[];
  236. normalA: number[];
  237. restitution: number;
  238. firstImpact: boolean;
  239. shapeA: Shape;
  240. shapeB: Shape;
  241. }
  242. export class Equation {
  243. static DEFAULT_STIFFNESS: number;
  244. static DEFAULT_RELAXATION: number;
  245. constructor(bodyA: Body, bodyB: Body, minForce?: number, maxForce?: number);
  246. minForce: number;
  247. maxForce: number;
  248. bodyA: Body;
  249. bodyB: Body;
  250. stiffness: number;
  251. relaxation: number;
  252. G: number[];
  253. needsUpdate: boolean;
  254. multiplier: number;
  255. relativeVelocity: number;
  256. enabled: boolean;
  257. gmult(): number;
  258. computeB(): number;
  259. computeGq(): number;
  260. computeGW(): number;
  261. computeGWlambda(): number;
  262. computeGiMf(): number;
  263. computeGiMGt(): number;
  264. addToWlambda(deltalambda: number): void;
  265. computeInvC(eps: number): number;
  266. update(): void;
  267. }
  268. export class FrictionEquation extends Equation {
  269. constructor(bodyA: Body, bodyB: Body, slipForce: number);
  270. contactEquations: ContactEquation;
  271. contactPointA: number[];
  272. contactPointB: number[];
  273. t: number[];
  274. shapeA: Shape;
  275. shapeB: Shape;
  276. frictionCoefficient: number;
  277. setSlipForce(slipForce: number): void;
  278. getSlipForce(): number;
  279. }
  280. export class RotationalLockEquation extends Equation {
  281. constructor(bodyA: Body, bodyB: Body, options?: {
  282. angle?: number;
  283. });
  284. angle: number;
  285. }
  286. export class RotationalVelocityEquation extends Equation {
  287. constructor(bodyA: Body, bodyB: Body);
  288. }
  289. export class EventEmitter {
  290. on(type: string, listener: Function, context?: any): EventEmitter;
  291. has(type: string, listener: Function): boolean;
  292. off(type: string, listener: Function): EventEmitter;
  293. emit(event: any): EventEmitter;
  294. }
  295. export class ContactMaterialOptions {
  296. friction: number;
  297. restitution: number;
  298. stiffness: number;
  299. relaxation: number;
  300. frictionStiffness: number;
  301. frictionRelaxation: number;
  302. surfaceVelocity: number;
  303. }
  304. export class ContactMaterial {
  305. constructor(materialA: Material, materialB: Material, options?: ContactMaterialOptions);
  306. id: number;
  307. materialA: Material;
  308. materialB: Material;
  309. friction: number;
  310. restitution: number;
  311. stiffness: number;
  312. relaxation: number;
  313. frictionStuffness: number;
  314. frictionRelaxation: number;
  315. surfaceVelocity: number;
  316. contactSkinSize: number;
  317. }
  318. export class Material {
  319. constructor(id: number);
  320. id: number;
  321. }
  322. export class vec2 {
  323. static add(out: number[], a: number[], b: number[]): number[];
  324. static centroid(out: number[], a: number[], b: number[], c: number[]): number[];
  325. static clone(a: number[]): number[];
  326. static copy(out: number[], a: number[]): number[];
  327. static create(): number[];
  328. static crossLength(a: number[], b: number[]): number;
  329. static crossVZ(out: number[], vec: number[], zcomp: number): number;
  330. static crossZV(out: number[], zcomp: number, vec: number[]): number;
  331. static dist(a: number[], b: number[]): number;
  332. static distance(a: number[], b: number[]): number;
  333. static div(out: number[], a: number[], b: number[]): number[];
  334. static divide(out: number[], a: number[], b: number[]): number[];
  335. static dot(a: number[], b: number[]): number;
  336. static fromValues(x: number, y: number): number[];
  337. static getLineSegmentsIntersection(out: number[], p0: number[], p1: number[], p2: number[], p3: number[]): boolean;
  338. static getLineSegmentsIntersectionFraction(p0: number[], p1: number[], p2: number[], p3: number[]): number;
  339. static len(a: number[]): number;
  340. static length(a: number[]): number;
  341. static lerp(out: number[], a: number[], b: number[], t: number): void;
  342. static mul(out: number[], a: number[], b: number[]): number[];
  343. static multiply(out: number[], a: number[], b: number[]): number[];
  344. static negate(out: number[], a: number[]): number[];
  345. static normalize(out: number[], a: number[]): number[];
  346. static reflect(out: number[], vector: number[], normal: number[]): void;
  347. static rotate(out: number[], a: number[], angle: number): void;
  348. static rotate90cw(out: number[], a: number[]): void;
  349. static scale(out: number[], a: number[], b: number): number[];
  350. static set(out: number[], x: number, y: number): number[];
  351. static sqrDist(a: number[], b: number[]): number;
  352. static squaredDistance(a: number[], b: number[]): number;
  353. static sqrLen(a: number[]): number;
  354. static squaredLength(a: number[]): number;
  355. static str(vec: number[]): string;
  356. static sub(out: number[], a: number[], b: number[]): number[];
  357. static subtract(out: number[], a: number[], b: number[]): number[];
  358. static toGlobalFrame(out: number[], localPoint: number[], framePosition: number[], frameAngle: number): void;
  359. static toLocalFrame(out: number[], worldPoint: number[], framePosition: number[], frameAngle: number): void;
  360. static vectorToLocalFrame(out: number[], worldVector: number[], frameAngle: number): void;
  361. }
  362. // export class BodyOptions {
  363. //
  364. // mass: number;
  365. // position: number[];
  366. // velocity: number[];
  367. // angle: number;
  368. // angularVelocity: number;
  369. // force: number[];
  370. // angularForce: number;
  371. // fixedRotation: number;
  372. //
  373. // }
  374. /**
  375. * 刚体。有质量、位置、速度等属性以及一组被用于碰撞的形状
  376. *
  377. * @class Body
  378. * @constructor
  379. * @extends EventEmitter
  380. * @param {Array} [options.force]
  381. * @param {Array} [options.position]
  382. * @param {Array} [options.velocity]
  383. * @param {Boolean} [options.allowSleep]
  384. * @param {Boolean} [options.collisionResponse]
  385. * @param {Number} [options.angle=0]
  386. * @param {Number} [options.angularForce=0]
  387. * @param {Number} [options.angularVelocity=0]
  388. * @param {Number} [options.ccdIterations=10]
  389. * @param {Number} [options.ccdSpeedThreshold=-1]
  390. * @param {Number} [options.fixedRotation=false]
  391. * @param {Number} [options.gravityScale]
  392. * @param {Number} [options.id]
  393. * @param {Number} [options.mass=0] 一个大于0的数字。如果设置成0,其type属性将被设置为 Body.STATIC.
  394. * @param {Number} [options.sleepSpeedLimit]
  395. * @param {Number} [options.sleepTimeLimit]
  396. * @param {Object} [options]
  397. *
  398. * @example
  399. * // 创建一个刚体
  400. * var body = new Body({
  401. * mass: 1,
  402. * position: [0, 0],
  403. * angle: 0,
  404. * velocity: [0, 0],
  405. * angularVelocity: 0
  406. * });
  407. *
  408. * // 将一个圆形形状添加到刚体
  409. * body.addShape(new Circle({ radius: 1 }));
  410. *
  411. * // 将刚体加入 world
  412. * world.addBody(body);
  413. */
  414. export class Body extends EventEmitter {
  415. sleepyEvent: {
  416. type: string;
  417. };
  418. sleepEvent: {
  419. type: string;
  420. };
  421. wakeUpEvent: {
  422. type: string;
  423. };
  424. static DYNAMIC: number;
  425. static STATIC: number;
  426. static KINEMATIC: number;
  427. static AWAKE: number;
  428. static SLEEPY: number;
  429. static SLEEPING: number;
  430. constructor(options?);
  431. /**
  432. * 刚体id
  433. * @property id
  434. * @type {Number}
  435. */
  436. id: number;
  437. /**
  438. * 刚体被添加到的 world。如果没有被添加到 world 该属性将被设置为 null
  439. * @property world
  440. * @type {World}
  441. */
  442. world: World;
  443. /**
  444. * 刚体的碰撞形状
  445. *
  446. * @property shapes
  447. * @type {Array}
  448. */
  449. shapes: Shape[];
  450. /**
  451. * 质量
  452. * @property mass
  453. * @type {number}
  454. */
  455. mass: number;
  456. /**
  457. * 惯性
  458. * @property inertia
  459. * @type {number}
  460. */
  461. inertia: number;
  462. /**
  463. * 是否固定旋转
  464. * @property fixedRotation
  465. * @type {Boolean}
  466. */
  467. fixedRotation: boolean;
  468. /**
  469. * 位置
  470. * @property position
  471. * @type {Array}
  472. */
  473. position: number[];
  474. /**
  475. * 位置插值
  476. * @property interpolatedPosition
  477. * @type {Array}
  478. */
  479. interpolatedPosition: number[];
  480. /**
  481. * 角度插值
  482. * @property interpolatedAngle
  483. * @type {Number}
  484. */
  485. interpolatedAngle: number;
  486. /**
  487. * 速度
  488. * @property velocity
  489. * @type {Array}
  490. */
  491. velocity: number[];
  492. /**
  493. * 角度
  494. * @property angle
  495. * @type {number}
  496. */
  497. angle: number;
  498. /**
  499. * 力
  500. * @property force
  501. * @type {Array}
  502. */
  503. force: number[];
  504. /**
  505. * 角力
  506. * @property angularForce
  507. * @type {number}
  508. */
  509. angularForce: number;
  510. /**
  511. * 限行阻尼。取值区间[0,1]
  512. * @property damping
  513. * @type {Number}
  514. * @default 0.1
  515. */
  516. damping: number;
  517. /**
  518. * 角阻尼。取值区间[0,1]
  519. * @property angularDamping
  520. * @type {Number}
  521. * @default 0.1
  522. */
  523. angularDamping: number;
  524. /**
  525. * 运动类型。 应该是Body.STATIC,Body.DYNAMIC,Body.KINEMATIC之一
  526. *
  527. * * Static 刚体不会动,不响应力或者碰撞
  528. * * Dynamic 刚体会动,响应力和碰撞
  529. * * Kinematic 刚体仅根据自身属性运动,不响应力或者碰撞
  530. *
  531. * @property type
  532. * @type {number}
  533. *
  534. * @example
  535. * // 默认值是STATIC
  536. * var body = new Body();
  537. * console.log(body.type == Body.STATIC); // true
  538. *
  539. * @example
  540. * // 将质量设置为非0的值,会变为DYNAMIC
  541. * var dynamicBody = new Body({
  542. * mass : 1
  543. * });
  544. * console.log(dynamicBody.type == Body.DYNAMIC); // true
  545. *
  546. * @example
  547. * // KINEMATIC刚体只会运动,如果你改变它的速度
  548. * var kinematicBody = new Body({
  549. * type: Body.KINEMATIC
  550. * });
  551. */
  552. type: number;
  553. /**
  554. * 边界圆半径
  555. * @property boundingRadius
  556. * @type {Number}
  557. */
  558. boundingRadius: number;
  559. /**
  560. * 边框
  561. * @property aabb
  562. * @type {AABB}
  563. */
  564. aabb: AABB;
  565. /**
  566. * 设置AABB是否会更新。通过调用 updateAABB 方法更新它
  567. * @property aabbNeedsUpdate
  568. * @type {Boolean}
  569. * @see updateAABB
  570. *
  571. * @example
  572. * body.aabbNeedsUpdate = true;
  573. * body.updateAABB();
  574. * console.log(body.aabbNeedsUpdate); // false
  575. */
  576. aabbNeedsUpdate: boolean;
  577. /**
  578. * 设置为true,刚体会自动进入睡眠。需要在 World 中允许刚体睡眠
  579. * @property allowSleep
  580. * @type {Boolean}
  581. * @default true
  582. */
  583. allowSleep: boolean;
  584. /**
  585. * Body.AWAKE,Body.SLEEPY,Body.SLEEPING之一
  586. *
  587. * 默认值是 Body.AWAKE。如果刚体速度低于 sleepSpeedLimit,该属性将变为 Body.SLEEPY。如果持续 Body.SLEEPY 状态 sleepTimeLimit 秒,该属性将变为 Body.SLEEPY。
  588. *
  589. * @property sleepState
  590. * @type {Number}
  591. * @default Body.AWAKE
  592. */
  593. sleepState: number;
  594. /**
  595. * 如果速度小于该值,sleepState 将变为 Body.SLEEPY 状态
  596. * @property sleepSpeedLimit
  597. * @type {Number}
  598. * @default 0.2
  599. */
  600. sleepSpeedLimit: number;
  601. /**
  602. * 如果持续 Body.SLEEPY 状态 sleepTimeLimit 秒,sleepState 将变为 Body.SLEEPING
  603. * @property sleepTimeLimit
  604. * @type {Number}
  605. * @default 1
  606. */
  607. sleepTimeLimit: number;
  608. /**
  609. * 重力缩放因子。如果你想忽略刚体重心,设置为零。如果你想反转重力,将其设置为-1。
  610. * @property {Number} gravityScale
  611. * @default 1
  612. */
  613. gravityScale: number;
  614. /**
  615. * The angular velocity of the body, in radians per second.
  616. * @property angularVelocity
  617. * @type {number}
  618. */
  619. angularVelocity: number;
  620. /**
  621. * The inverse inertia of the body.
  622. * @property invInertia
  623. * @type {number}
  624. */
  625. invInertia: number;
  626. /**
  627. * The inverse mass of the body.
  628. * @property invMass
  629. * @type {number}
  630. */
  631. invMass: number;
  632. /**
  633. * The previous angle of the body.
  634. * @property previousAngle
  635. * @type {number}
  636. */
  637. previousAngle: number;
  638. /**
  639. * The previous position of the body.
  640. * @property previousPosition
  641. * @type {Array}
  642. */
  643. previousPosition: number[];
  644. /**
  645. * Constraint velocity that was added to the body during the last step.
  646. * @property vlambda
  647. * @type {Array}
  648. */
  649. vlambda: number[];
  650. /**
  651. * Angular constraint velocity that was added to the body during last step.
  652. * @property wlambda
  653. * @type {Array}
  654. */
  655. wlambda: number[];
  656. /**
  657. * The number of iterations that should be used when searching for the time of impact during CCD. A larger number will assure that there's a small penetration on CCD collision, but a small number will give more performance.
  658. * @property {Number} ccdIterations
  659. * @default 10
  660. */
  661. ccdIterations: number;
  662. /**
  663. * If the body speed exceeds this threshold, CCD (continuous collision detection) will be enabled. Set it to a negative number to disable CCD completely for this body.
  664. * @property {Number} ccdSpeedThreshold
  665. * @default -1
  666. */
  667. ccdSpeedThreshold: number;
  668. /**
  669. * Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled. That means that this body will move through other bodies, but it will still trigger contact events, etc.
  670. * @property collisionResponse
  671. * @type {Boolean}
  672. */
  673. collisionResponse: boolean;
  674. /**
  675. * Set to true if you want to fix the body movement along the X axis. The body will still be able to move along Y.
  676. * @property fixedX
  677. * @type {Boolean}
  678. */
  679. fixedX: boolean;
  680. /**
  681. * Set to true if you want to fix the body movement along the Y axis. The body will still be able to move along X.
  682. * @property fixedY
  683. * @type {Boolean}
  684. */
  685. fixedY: boolean;
  686. /**
  687. * How long the body has been sleeping.
  688. * @property idleTime
  689. * @type {Number}
  690. */
  691. idleTime: number;
  692. /**
  693. * 与每个形状对应的显示对象
  694. */
  695. displays: egret.DisplayObject[];
  696. /**
  697. * 设置刚体总密度
  698. * @method setDensity
  699. */
  700. setDensity(density: number): void;
  701. /**
  702. * 得到所有形状的总面积
  703. * @method getArea
  704. * @return {Number}
  705. */
  706. getArea(): number;
  707. /**
  708. * 获得AABB
  709. * @method getAABB
  710. */
  711. getAABB(): AABB;
  712. /**
  713. * 更新AABB
  714. * @method updateAABB
  715. */
  716. updateAABB(): void;
  717. /**
  718. * 更新外边界
  719. * @method updateBoundingRadius
  720. */
  721. updateBoundingRadius(): void;
  722. /**
  723. * 添加一个形状
  724. *
  725. * @method addShape
  726. * @param {Shape} shape 形状
  727. * @param {Array} [offset] 偏移
  728. * @param {Number} [angle] 角度
  729. *
  730. * @example
  731. * var body = new Body(),
  732. * shape = new Circle();
  733. *
  734. * // 位于中心
  735. * body.addShape(shape);
  736. *
  737. * // 偏移量为x轴一个单位
  738. * body.addShape(shape,[1,0]);
  739. *
  740. * // 偏移量为y轴一个单位,同时逆时针旋转90度
  741. * body.addShape(shape,[0,1],Math.PI/2);
  742. */
  743. addShape(shape: Shape, offset?: number[], angle?: number): void;
  744. /**
  745. * 移除形状
  746. * @method removeShape
  747. * @param {Shape} shape
  748. * @return {Boolean}
  749. */
  750. removeShape(shape: Shape): boolean;
  751. /**
  752. * 更新属性,结构或者质量改变时会被调用
  753. *
  754. * @method updateMassProperties
  755. *
  756. * @example
  757. * body.mass += 1;
  758. * body.updateMassProperties();
  759. */
  760. updateMassProperties(): void;
  761. /**
  762. * 相对于 world 中的一个点施加力
  763. * @method applyForce
  764. * @param {Array} force 力
  765. * @param {Array} relativePoint 以物体中心点为基准的点
  766. */
  767. applyForce(force: number[], relativePoint: number[]): void;
  768. /**
  769. * Wake the body up. Normally you should not need this, as the body is automatically awoken at events such as collisions.
  770. * Sets the sleepState to {{#crossLink "Body/AWAKE:property"}}Body.AWAKE{{/crossLink}} and emits the wakeUp event if the body wasn't awake before.
  771. * @method wakeUp
  772. */
  773. wakeUp(): void;
  774. /**
  775. * Force body sleep
  776. * @method sleep
  777. */
  778. sleep(): void;
  779. /**
  780. * Called every timestep to update internal sleep timer and change sleep state if needed.
  781. * @method sleepTick
  782. * @param {number} time The world time in seconds
  783. * @param {boolean} dontSleep
  784. * @param {number} dt
  785. */
  786. sleepTick(time: number, dontSleep: boolean, dt: number): void;
  787. /**
  788. * Check if the body is overlapping another body. Note that this method only works if the body was added to a World and if at least one step was taken.
  789. * @method overlaps
  790. * @param {Body} body
  791. * @return {boolean}
  792. */
  793. overlaps(body: Body): boolean;
  794. /**
  795. * Moves the shape offsets so their center of mass becomes the body center of mass.
  796. * @method adjustCenterOfMass
  797. */
  798. adjustCenterOfMass(): void;
  799. /**
  800. * Apply damping.
  801. * @method applyDamping
  802. * @param {number} dt Current time step
  803. */
  804. applyDamping(dt: number): void;
  805. /**
  806. * Reads a polygon shape path, and assembles convex shapes from that and puts them at proper offset points.
  807. * @method fromPolygon
  808. * @param {Array} path An array of 2d vectors, e.g. [[0,0],[0,1],...] that resembles a concave or convex polygon. The shape must be simple and without holes.
  809. * @param {Object} [options]
  810. * @param {Boolean} [options.optimalDecomp=false] Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
  811. * @param {Boolean} [options.skipSimpleCheck=false] Set to true if you already know that the path is not intersecting itself.
  812. * @param {Boolean|Number} [options.removeCollinearPoints=false] Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
  813. * @return {Boolean} True on success, else false.
  814. */
  815. fromPolygon(path:number[][], options?: {
  816. optimalDecomp?: boolean;
  817. skipSimpleCheck?: boolean;
  818. removeCollinearPoints?: boolean; // Boolean | Number
  819. }): boolean;
  820. /**
  821. * Sets the force on the body to zero.
  822. * @method setZeroForce
  823. */
  824. setZeroForce(): void;
  825. /**
  826. * Transform a world point to local body frame.
  827. * @method toLocalFrame
  828. * @param {Array} out The vector to store the result in
  829. * @param {Array} worldPoint The input world point
  830. */
  831. toLocalFrame(out: number[], worldPoint: number[]): void;
  832. /**
  833. * Transform a local point to world frame.
  834. * @method toWorldFrame
  835. * @param {Array} out The vector to store the result in
  836. * @param {Array} localPoint The input local point
  837. */
  838. toWorldFrame(out: number[], localPoint: number[]): void;
  839. /**
  840. * Apply force to a body-local point.
  841. * @method applyForceLocal
  842. * @param {Array} localForce The force vector to add, oriented in local body space.
  843. * @param {Array} localPoint A point relative to the body in world space. If not given, it is set to zero and all of the impulse will be excerted on the center of mass.
  844. */
  845. applyForceLocal(localForce: number[], localPoint: number[]): void;
  846. /**
  847. * Apply impulse to a point relative to the body. This could for example be a point on the Body surface. An impulse is a force added to a body during a short period of time (impulse = force * time). Impulses will be added to Body.velocity and Body.angularVelocity.
  848. * @method applyImpulse
  849. * @param {Array} impulse The impulse vector to add, oriented in world space.
  850. * @param {Array} relativePoint A point relative to the body in world space. If not given, it is set to zero and all of the impulse will be excerted on the center of mass.
  851. */
  852. applyImpulse(impulse: number[], relativePoint: number[]): void;
  853. /**
  854. * Apply impulse to a point relative to the body. This could for example be a point on the Body surface. An impulse is a force added to a body during a short period of time (impulse = force * time). Impulses will be added to Body.velocity and Body.angularVelocity.
  855. * @method applyImpulseLocal
  856. * @param {Array} impulse The impulse vector to add, oriented in world space.
  857. * @param {Array} relativePoint A point relative to the body in world space. If not given, it is set to zero and all of the impulse will be excerted on the center of mass.
  858. */
  859. applyImpulseLocal(impulse: number[], relativePoint: number[]): void;
  860. /**
  861. * Get velocity of a point in the body.
  862. * @method getVelocityAtPoint
  863. * @param {Array} result A vector to store the result in
  864. * @param {Array} relativePoint A world oriented vector, indicating the position of the point to get the velocity from
  865. * @return {Array}
  866. */
  867. getVelocityAtPoint(result: number[], relativePoint: number[]): number[];
  868. /**
  869. * Move the body forward in time given its current velocity.
  870. * @method integrate
  871. * @param {number} dt
  872. */
  873. integrate(dt: number): void;
  874. /**
  875. * Transform a world point to local body frame.
  876. * @method vectorToLocalFrame
  877. * @param {Array} out The vector to store the result in
  878. * @param {Array} worldVector The input world vector
  879. */
  880. vectorToLocalFrame(out: number[], worldVector: number[]): void;
  881. /**
  882. * Transform a local point to world frame.
  883. * @method vectorToWorldFrame
  884. * @param {Array} out The vector to store the result in
  885. * @param {Array} localVector The input local vector
  886. */
  887. vectorToWorldFrame(out: number[], localVector: number[]): void;
  888. }
  889. /**
  890. * Box shape class.
  891. *
  892. * @class Box
  893. * @constructor
  894. * @extends Convex
  895. * @param {Object} [options] (Note that this options object will be passed on to the Shape constructor.)
  896. * @param {Number} [options.width=1] Total width of the box
  897. * @param {Number} [options.height=1] Total height of the box
  898. */
  899. export class Box extends Convex {
  900. constructor(options?: Object);
  901. width: number;
  902. height: number;
  903. }
  904. export class Pool {
  905. objects: any[];
  906. get(): Object;
  907. release(object: Object): Pool;
  908. resize(size: number): Pool;
  909. }
  910. export class OverlapKeeperRecordPool extends Pool {
  911. }
  912. export class IslandPool extends Pool {
  913. }
  914. export class IslandNodePool extends Pool {
  915. }
  916. export class ContactEquationPool extends Pool {
  917. }
  918. export class FrictionEquationPool extends Pool {
  919. }
  920. export class Ray {
  921. static ALL: number;
  922. static ANY: number;
  923. static CLOSEST: number;
  924. constructor(options?: {
  925. from?: number[];
  926. to?: number[];
  927. checkCollisionResponse?: boolean;
  928. skipBackfaces?: boolean;
  929. collisionMask?: number;
  930. collisionGroup?: number;
  931. mode?: number;
  932. callback?: number;
  933. });
  934. callback: Function;
  935. checkCollisionResponse: boolean;
  936. collisionGroup: number;
  937. collisionMask: number;
  938. direction: number[];
  939. from: number[];
  940. length: number;
  941. mode: number;
  942. skipBackfaces: boolean;
  943. to: number[];
  944. getAABB(aabb: AABB): void;
  945. intersectBodies(bodies: Body[]): void;
  946. update(): void;
  947. }
  948. export class RaycastResult {
  949. body: Body;
  950. faceIndex: number;
  951. fraction: number;
  952. isStopped: boolean;
  953. normal: number[];
  954. shape: Shape;
  955. getHitDistance(ray: Ray): void;
  956. getHitPoint(out:number[], ray: Ray): void;
  957. hasHit():boolean;
  958. reset(): void;
  959. stop(): void;
  960. }
  961. export class TopDownVehicle {
  962. constructor(chassisBody: Body, options?: Object);
  963. chassisBody: Body;
  964. wheels: WheelConstraint[];
  965. addToWorld(world: World): void;
  966. addWheel(wheelOptions?: Object): WheelConstraint;
  967. removeFromWorld(world: World): void;
  968. update(): void;
  969. }
  970. export class WheelConstraint extends Constraint {
  971. constructor(vehicle: TopDownVehicle, options?: {
  972. localForwardVector?: number[];
  973. localPosition?: number[];
  974. sideFriction?: number;
  975. });
  976. engineForce: number;
  977. localForwardVector: number[];
  978. localPosition: number[];
  979. steerValue: number;
  980. getSpeed(): number;
  981. update(): void;
  982. }
  983. export class Spring {
  984. constructor(bodyA: Body, bodyB: Body, options?: {
  985. stiffness?: number;
  986. damping?: number;
  987. localAnchorA?: number[];
  988. localAnchorB?: number[];
  989. worldAnchorA?: number[];
  990. worldAnchorB?: number[];
  991. });
  992. bodyA: Body;
  993. bodyB: Body;
  994. damping: number;
  995. stiffness: number;
  996. applyForce(): void;
  997. }
  998. export class LinearSpring extends Spring {
  999. constructor(bodyA: Body, bodyB: Body, options?: {
  1000. restLength?: number;
  1001. stiffness?: number;
  1002. damping?: number;
  1003. worldAnchorA?: number[];
  1004. worldAnchorB?: number[];
  1005. localAnchorA?: number[];
  1006. localAnchorB?: number[];
  1007. });
  1008. localAnchorA: number[];
  1009. localAnchorB: number[];
  1010. restLength: number;
  1011. setWorldAnchorA(worldAnchorA: number[]): void;
  1012. setWorldAnchorB(worldAnchorB: number[]): void;
  1013. getWorldAnchorA(result: number[]): void;
  1014. getWorldAnchorB(result: number[]): void;
  1015. }
  1016. export class RotationalSpring extends Spring {
  1017. constructor(bodyA: Body, bodyB: Body, options?: {
  1018. restAngle?: number;
  1019. stiffness?: number;
  1020. damping?: number;
  1021. });
  1022. restAngle: number;
  1023. }
  1024. /**
  1025. * Capsule shape class.
  1026. *
  1027. * @class Capsule
  1028. * @constructor
  1029. * @extends Shape
  1030. * @param {Object} [options] (Note that this options object will be passed on to the Shape constructor.)
  1031. * @param {Number} [options.length=1] The distance between the end points
  1032. * @param {Number} [options.radius=1] Radius of the capsule
  1033. */
  1034. export class Capsule extends Shape {
  1035. constructor(options?: Object);
  1036. length: number;
  1037. radius: number;
  1038. conputeMomentOfInertia(mass: number): number;
  1039. }
  1040. /**
  1041. * Circle shape class.
  1042. *
  1043. * @class Circle
  1044. * @constructor
  1045. * @extends Shape
  1046. * @param {Object} [options] (Note that this options object will be passed on to the Shape constructor.)
  1047. * @param {Number} [options.radius=1] The radius of this circle
  1048. * @example
  1049. * var circleShape = new Circle({ radius: 1 });
  1050. * body.addShape(circleShape);
  1051. */
  1052. export class Circle extends Shape {
  1053. constructor(options?: Object);
  1054. /**
  1055. * 半径
  1056. * @property radius
  1057. * @type {number}
  1058. */
  1059. radius: number;
  1060. }
  1061. /**
  1062. * Convex shape class.
  1063. *
  1064. * @class Convex
  1065. * @constructor
  1066. * @extends Shape
  1067. * @param {Object} [options] (Note that this options object will be passed on to the Shape constructor.)
  1068. * @param {Array} [options.vertices] An array of vertices that span this shape. Vertices are given in counter-clockwise (CCW) direction.
  1069. * @param {Array} [options.axes] An array of unit length vectors, representing the symmetry axes in the convex.
  1070. * @example
  1071. * var vertices = [[-1,-1], [1,-1], [1,1], [-1,1]];
  1072. * var convexShape = new Convex({ vertices: vertices });
  1073. * body.addShape(convexShape);
  1074. */
  1075. export class Convex extends Shape {
  1076. static projectOntoAxis(offset: number[], localAxis: number[], result: number[]): void;
  1077. static triangleArea(a: number[], b: number[], c: number[]): number;
  1078. constructor(options?: Object);
  1079. vertices: number[][];
  1080. axes: number[];
  1081. centerOfMass: number[];
  1082. triangles: number[];
  1083. updateCenterOfMass(): void;
  1084. updateTriangles(): void;
  1085. }
  1086. export class Heightfield extends Shape {
  1087. constructor(options?: {
  1088. heights?: number[];
  1089. minValue?: number;
  1090. maxValue?: number;
  1091. elementWidth?: number;
  1092. });
  1093. heights: number[];
  1094. maxValue: number;
  1095. minValue: number;
  1096. elementWidth: number;
  1097. getLineSegment(start:number[], end:number[], i:number): void;
  1098. updateMaxMinValues(): void;
  1099. }
  1100. export class Shape {
  1101. static BOX: number;
  1102. static CAPSULE: number;
  1103. static CIRCLE: number;
  1104. static CONVEX: number;
  1105. static HEIGHTFIELD: number;
  1106. static LINE: number;
  1107. static PARTICLE: number;
  1108. static PLANE: number;
  1109. constructor(options?: {
  1110. position?: number[];
  1111. angle?: number;
  1112. collisionGroup?: number;
  1113. collisionMask?: number;
  1114. sensor?: boolean;
  1115. collisionResponse?: boolean;
  1116. type?: number;
  1117. });
  1118. angle: number;
  1119. area: number;
  1120. body: Body;
  1121. boundingRadius: number;
  1122. collisionGroup: number;
  1123. collisionMask: number;
  1124. collisionResponse: boolean;
  1125. id: number;
  1126. material: Material;
  1127. position: number[];
  1128. sensor: boolean;
  1129. type: number;
  1130. computeAABB(out: AABB, position: number[], angle: number): void;
  1131. computeMomentOfInertia(mass: number): number;
  1132. raycast(result: RaycastResult, ray: Ray, position: number[], angle: number): void;
  1133. updateArea(): void;
  1134. updateBoundingRadius(): number;
  1135. }
  1136. export class Line extends Shape {
  1137. constructor(options?: {
  1138. length?: number;
  1139. });
  1140. length: number;
  1141. }
  1142. export class Particle extends Shape {
  1143. }
  1144. export class Plane extends Shape {
  1145. }
  1146. export class Solver extends EventEmitter {
  1147. constructor();
  1148. equations: Equation[];
  1149. equationSortFunction: Function; //Function | boolean
  1150. addEquation(eq: Equation): void;
  1151. addEquations(eqs: Equation[]): void;
  1152. removeAllEquations(): void;
  1153. removeEquation(eq: Equation): void;
  1154. solve(dt: number, world: World): void;
  1155. solveIsland(dt: number, island: Island): void;
  1156. sortEquations(): void;
  1157. }
  1158. export class GSSolver extends Solver {
  1159. constructor(options?: {
  1160. iterations?: number;
  1161. tolerance?: number;
  1162. });
  1163. iterations: number;
  1164. tolerance: number;
  1165. useZeroRHS: boolean;
  1166. frictionIterations: number;
  1167. usedIterations: number;
  1168. solve(h: number, world: World): void;
  1169. }
  1170. export class OverlapKeeper {
  1171. constructor();
  1172. recordPool: OverlapKeeperRecordPool;
  1173. tick(): void;
  1174. setOverlapping(bodyA: Body, shapeA: Body, bodyB: Body, shapeB: Body): void;
  1175. bodiesAreOverlapping(bodyA: Body, bodyB: Body): boolean;
  1176. }
  1177. export class OverlapKeeperRecord {
  1178. constructor(bodyA: Body, shapeA: Shape, bodyB: Body, shapeB: Shape);
  1179. bodyA: Body;
  1180. bodyB: Body;
  1181. shapeA: Shape;
  1182. shapeB: Shape;
  1183. set(bodyA: Body, shapeA: Shape, bodyB: Body, shapeB: Shape): void;
  1184. }
  1185. export class TupleDictionary {
  1186. data: Object;
  1187. keys: number[];
  1188. copy(dict: TupleDictionary): void;
  1189. get(i: number, j: number): number;
  1190. getByKey(key: number): Object;
  1191. getKey(i: number, j: number): string;
  1192. reset(): void;
  1193. set(i: number, j: number, value: number): void;
  1194. }
  1195. export class Utils {
  1196. static appendArray<T>(a: Array<T>, b: Array<T>): Array<T>;
  1197. static splice<T>(array: Array<T>, index: number, howMany: number): void;
  1198. static extend(a: any, b: any): void;
  1199. static defaults(options: any, defaults: any): any;
  1200. }
  1201. export class Island {
  1202. equations: Equation[];
  1203. bodies: Body[];
  1204. reset(): void;
  1205. getBodies(): Body[];
  1206. wantsToSleep(): boolean;
  1207. sleep(): void;
  1208. }
  1209. export class IslandManager extends Solver {
  1210. static getUnvisitedNode(nodes: Node[]): IslandNode; // IslandNode | boolean
  1211. constructor(options?: Object);
  1212. islands: Island[];
  1213. nodes: IslandNode[];
  1214. islandPool: IslandPool;
  1215. nodePool: IslandNodePool;
  1216. visit(node: IslandNode, bds: Body[], eqs: Equation[]): void;
  1217. bfs(root: IslandNode, bds: Body[], eqs: Equation[]): void;
  1218. split(world: World): Island[];
  1219. }
  1220. export class IslandNode {
  1221. constructor(body: Body);
  1222. body: Body;
  1223. neighbors: IslandNode[];
  1224. equations: Equation[];
  1225. visited: boolean;
  1226. reset(): void;
  1227. }
  1228. /**
  1229. * world,包含所有刚体
  1230. *
  1231. * @class World
  1232. * @constructor
  1233. * @param {Object} [options]
  1234. * @param {Solver} [options.solver] 默认值 GSSolver.
  1235. * @param {Array} [options.gravity] 默认值 [0,-9.78]
  1236. * @param {Broadphase} [options.broadphase] 默认值 NaiveBroadphase
  1237. * @param {Boolean} [options.islandSplit=false]
  1238. * @param {Boolean} [options.doProfiling=false]
  1239. * @extends EventEmitter
  1240. *
  1241. * @example
  1242. * var world = new World({
  1243. * gravity: [0, -9.81],
  1244. * broadphase: new SAPBroadphase()
  1245. * });
  1246. */
  1247. export class World extends EventEmitter {
  1248. /**
  1249. * step() 执行之后调用
  1250. * @event postStep
  1251. */
  1252. postStepEvent: {
  1253. type: string;
  1254. };
  1255. /**
  1256. * Body 加入时调用
  1257. * @event addBody
  1258. * @param {Body} body
  1259. */
  1260. addBodyEvent: {
  1261. type: string;
  1262. body: Body;
  1263. };
  1264. /**
  1265. * Body移除时调用
  1266. * @event removeBody
  1267. * @param {Body} body
  1268. */
  1269. removeBodyEvent: {
  1270. type: string;
  1271. body: Body;
  1272. };
  1273. /**
  1274. * Spring 加入时调用
  1275. * @event addSpring
  1276. * @param {Spring} spring
  1277. */
  1278. addSpringEvent: {
  1279. type: string;
  1280. spring: Spring;
  1281. };
  1282. /**
  1283. * 当两个刚体第一次碰撞时调用。调用时碰撞步骤已经完成
  1284. * @event impact
  1285. * @param {Body} bodyA
  1286. * @param {Body} bodyB
  1287. */
  1288. impactEvent: {
  1289. type: string;
  1290. bodyA: Body;
  1291. bodyB: Body;
  1292. shapeA: Shape;
  1293. shapeB: Shape;
  1294. contactEquation: ContactEquation;
  1295. };
  1296. /**
  1297. * 当 Broadphase 手机对碰之后被调用
  1298. * @event postBroadphase
  1299. * @param {Array} 对碰数组
  1300. */
  1301. postBroadphaseEvent: {
  1302. type: string;
  1303. pairs: Body[];
  1304. };
  1305. /**
  1306. * 当两个形状重叠时调用
  1307. * @event beginContact
  1308. * @param {Shape} shapeA
  1309. * @param {Shape} shapeB
  1310. * @param {Body} bodyA
  1311. * @param {Body} bodyB
  1312. * @param {Array} contactEquations
  1313. */
  1314. beginContactEvent: {
  1315. type: string;
  1316. shapeA: Shape;
  1317. shapeB: Shape;
  1318. bodyA: Body;
  1319. bodyB: Body;
  1320. contactEquations: ContactEquation[];
  1321. };
  1322. /**
  1323. * 当两个形状停止重叠时调用
  1324. * @event endContact
  1325. * @param {Shape} shapeA
  1326. * @param {Shape} shapeB
  1327. * @param {Body} bodyA
  1328. * @param {Body} bodyB
  1329. */
  1330. endContactEvent: {
  1331. type: string;
  1332. shapeA: Shape;
  1333. shapeB: Shape;
  1334. bodyA: Body;
  1335. bodyB: Body;
  1336. };
  1337. /**
  1338. * Fired just before equations are added to the solver to be solved. Can be used to control what equations goes into the solver.
  1339. * @event preSolve
  1340. * @param {Array} contactEquations An array of contacts to be solved.
  1341. * @param {Array} frictionEquations An array of friction equations to be solved.
  1342. */
  1343. preSolveEvent: {
  1344. type: string;
  1345. contactEquations: ContactEquation[];
  1346. frictionEquations: FrictionEquation[];
  1347. };
  1348. /**
  1349. * 从不让刚体睡眠
  1350. * @static
  1351. * @property {number} NO_SLEEPING
  1352. */
  1353. static NO_SLEEPING: number;
  1354. /**
  1355. * 刚体睡眠
  1356. * @static
  1357. * @property {number} BODY_SLEEPING
  1358. */
  1359. static BODY_SLEEPING: number;
  1360. /**
  1361. * 取消激活在接触中的刚体,如果所有刚体都接近睡眠。必须设置 World.islandSplit
  1362. * @static
  1363. * @property {number} ISLAND_SLEEPING
  1364. */
  1365. static ISLAND_SLEEPING: number;
  1366. constructor(options?: {
  1367. solver?: Solver;
  1368. gravity?: number[];
  1369. broadphase?: Broadphase;
  1370. islandSplit?: boolean;
  1371. });
  1372. /**
  1373. * For keeping track of what time step size we used last step
  1374. * @property lastTimeStep
  1375. * @type {number}
  1376. */
  1377. lastTimeStep: number;
  1378. overlapKeeper: OverlapKeeper;
  1379. /**
  1380. * If the length of .gravity is zero, and .useWorldGravityAsFrictionGravity=true, then switch to using .frictionGravity for friction instead. This fallback is useful for gravityless games.
  1381. * @property {boolean} useFrictionGravityOnZeroGravity
  1382. * @default true
  1383. */
  1384. useFrictionGravityOnZeroGravity: boolean;
  1385. /**
  1386. * 所有 Spring
  1387. * @property springs
  1388. * @type {Array}
  1389. */
  1390. springs: Spring[];
  1391. /**
  1392. * 所有 Body
  1393. * @property {Array} bodies
  1394. */
  1395. bodies: Body[];
  1396. /**
  1397. * 所使用的求解器,以满足约束条件和接触。 默认值是 GSSolver
  1398. * @property {Solver} solver
  1399. */
  1400. solver: Solver;
  1401. /**
  1402. * @property narrowphase
  1403. * @type {Narrowphase}
  1404. */
  1405. narrowphase: Narrowphase;
  1406. /**
  1407. * The island manager of this world.
  1408. * @property {IslandManager} islandManager
  1409. */
  1410. islandManager: IslandManager;
  1411. /**
  1412. * 重力。在每个 step() 开始对所有刚体生效
  1413. *
  1414. * @property gravity
  1415. * @type {Array}
  1416. */
  1417. gravity: number[];
  1418. /**
  1419. * 重力摩擦
  1420. * @property {Number} frictionGravity
  1421. */
  1422. frictionGravity: number;
  1423. /**
  1424. * 设置为true,frictionGravity 会被自动设置为 gravity 长度.
  1425. * @property {Boolean} useWorldGravityAsFrictionGravity
  1426. */
  1427. useWorldGravityAsFrictionGravity: boolean;
  1428. /**
  1429. * @property broadphase
  1430. * @type {Broadphase}
  1431. */
  1432. broadphase: Broadphase;
  1433. /**
  1434. * 用户添加限制
  1435. *
  1436. * @property constraints
  1437. * @type {Array}
  1438. */
  1439. constraints: Constraint[];
  1440. /**
  1441. * 默认材料,defaultContactMaterial 时使用
  1442. * @property {Material} defaultMaterial
  1443. */
  1444. defaultMaterial: Material;
  1445. /**
  1446. * 使用的默认接触材料,如果没有接触材料被设置为碰撞的材料
  1447. * @property {ContactMaterial} defaultContactMaterial
  1448. */
  1449. defaultContactMaterial: ContactMaterial;
  1450. /**
  1451. * 设置自动使用弹簧力
  1452. * @property applySpringForces
  1453. * @type {Boolean}
  1454. */
  1455. applySpringForces: boolean;
  1456. /**
  1457. * 设置自动使用阻尼
  1458. * @property applyDamping
  1459. * @type {Boolean}
  1460. */
  1461. applyDamping: boolean;
  1462. /**
  1463. * 设置自动使用重力
  1464. * @property applyGravity
  1465. * @type {Boolean}
  1466. */
  1467. applyGravity: boolean;
  1468. /**
  1469. * 使用约束求解
  1470. * @property solveConstraints
  1471. * @type {Boolean}
  1472. */
  1473. solveConstraints: boolean;
  1474. /**
  1475. * 接触材料
  1476. * @property contactMaterials
  1477. * @type {Array}
  1478. */
  1479. contactMaterials: ContactMaterial[];
  1480. /**
  1481. * 世界时间
  1482. * @property time
  1483. * @type {Number}
  1484. */
  1485. time: number;
  1486. /**
  1487. * 是否正在 step 阶段
  1488. * @property {Boolean} stepping
  1489. */
  1490. stepping: boolean;
  1491. /**
  1492. * 是否启用岛内分裂
  1493. * @property {Boolean} islandSplit
  1494. */
  1495. islandSplit: boolean;
  1496. /**
  1497. * 设置为true,world会派发 impact 事件,关闭可以提高性能
  1498. * @property emitImpactEvent
  1499. * @type {Boolean}
  1500. */
  1501. emitImpactEvent: boolean;
  1502. /**
  1503. * 刚体睡眠策略。取值是 World.NO_SLEEPING,World.BODY_SLEEPING,World.ISLAND_SLEEPING 之一
  1504. * @property sleepMode
  1505. * @type {number}
  1506. * @default World.NO_SLEEPING
  1507. */
  1508. sleepMode: number;
  1509. /**
  1510. * 添加约束
  1511. * @method addConstraint
  1512. * @param {Constraint} constraint
  1513. */
  1514. addConstraint(constraint: Constraint): void;
  1515. /**
  1516. * 添加触点材料
  1517. * @method addContactMaterial
  1518. * @param {ContactMaterial} contactMaterial
  1519. */
  1520. addContactMaterial(contactMaterial: ContactMaterial): void;
  1521. /**
  1522. * 移除触点材料
  1523. * @method removeContactMaterial
  1524. * @param {ContactMaterial} cm
  1525. */
  1526. removeContactMaterial(cm: ContactMaterial): void;
  1527. /**
  1528. * 通过2个材料获得触点材料
  1529. * @method getContactMaterial
  1530. * @param {Material} materialA
  1531. * @param {Material} materialB
  1532. * @return {ContactMaterial} 获得的触点材料或者false
  1533. */
  1534. getContactMaterial(materialA: Material, materialB: Material): ContactMaterial;
  1535. /**
  1536. * 移除约束
  1537. * @method removeConstraint
  1538. * @param {Constraint} constraint
  1539. */
  1540. removeConstraint(constraint: Constraint): void;
  1541. /**
  1542. * 使物理系统向前经过一定时间
  1543. *
  1544. * @method step
  1545. * @param {Number} dt 时长
  1546. * @param {Number} [timeSinceLastCalled=0]
  1547. * @param {Number} [maxSubSteps=10]
  1548. *
  1549. * @example
  1550. * var world = new World();
  1551. * world.step(0.01);
  1552. */
  1553. step(dt: number, timeSinceLastCalled?: number, maxSubSteps?: number): void;
  1554. /**
  1555. * 添加一个 Spring
  1556. *
  1557. * @method addSpring
  1558. * @param {Spring} spring
  1559. */
  1560. addSpring(spring: Spring): void;
  1561. /**
  1562. * 移除一个 Spring
  1563. *
  1564. * @method removeSpring
  1565. * @param {Spring} spring
  1566. */
  1567. removeSpring(spring: Spring): void;
  1568. /**
  1569. * 添加一个 Body
  1570. *
  1571. * @method addBody
  1572. * @param {Body} body
  1573. *
  1574. * @example
  1575. * var world = new World(),
  1576. * body = new Body();
  1577. * world.addBody(body);
  1578. */
  1579. addBody(body: Body): void;
  1580. /**
  1581. * 移除一个 Body。如果在 step()阶段调用,将会在阶段之后移除
  1582. *
  1583. * @method removeBody
  1584. * @param {Body} body
  1585. */
  1586. removeBody(body: Body): void;
  1587. /**
  1588. * 通过id获取一个 Body
  1589. * @method getBodyById
  1590. * @return {Body|Boolean} 得到的刚体或者false
  1591. */
  1592. getBodyByID(id: number): Body;
  1593. /**
  1594. * 两个刚体之间禁用碰撞
  1595. * @method disableBodyCollision
  1596. * @param {Body} bodyA
  1597. * @param {Body} bodyB
  1598. */
  1599. disableBodyCollision(bodyA: Body, bodyB: Body): void;
  1600. /**
  1601. * 两个刚体之间启用碰撞
  1602. * @method enableBodyCollision
  1603. * @param {Body} bodyA
  1604. * @param {Body} bodyB
  1605. */
  1606. enableBodyCollision(bodyA: Body, bodyB: Body): void;
  1607. /**
  1608. * 重置 world
  1609. * @method clear
  1610. */
  1611. clear(): void;
  1612. /**
  1613. * Test if a world point overlaps bodies
  1614. * @method hitTest
  1615. * @param {Array} worldPoint Point to use for intersection tests
  1616. * @param {Array} bodies A list of objects to check for intersection
  1617. * @param {number} precision Used for matching against particles and lines. Adds some margin to these infinitesimal objects.
  1618. * @return {Array} Array of bodies that overlap the point
  1619. */
  1620. hitTest(worldPoint: number[], bodies: Body[], precision: number): Body[];
  1621. /**
  1622. * Ray cast against all bodies in the world.
  1623. * @method raycast
  1624. * @param {RaycastResult} result
  1625. * @param {Ray} ray
  1626. * @return {boolean} True if any body was hit.
  1627. */
  1628. raycast(result: RaycastResult, ray: Ray): boolean;
  1629. /**
  1630. * Runs narrowphase for the shape pair i and j.
  1631. * @method runNarrowphase
  1632. * @param {Narrowphase} np
  1633. * @param {Body} bi
  1634. * @param {Shape} si
  1635. * @param {Array} xi
  1636. * @param {number} ai
  1637. * @param {Body} bj
  1638. * @param {Shape} sj
  1639. * @param {Array} xj
  1640. * @param {number} aj
  1641. * @param {number} mu
  1642. */
  1643. runNarrowphase(np:Narrowphase, bi:Body, si:Shape, xi:number[], ai:number, bj:Body, sj:Shape, xj:number[], aj:number, mu:number): void;
  1644. /**
  1645. * Set the relaxation for all equations and contact materials.
  1646. * @method setGlobalRelaxation
  1647. * @param {number} relaxation
  1648. */
  1649. setGlobalRelaxation(relaxation: number): void;
  1650. /**
  1651. * Set the stiffness for all equations and contact materials.
  1652. * @method setGlobalStiffness
  1653. * @param {Number} stiffness
  1654. */
  1655. setGlobalStiffness(stiffness: number): void;
  1656. }
  1657. }