phys3D.d.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. declare namespace phys3D {
  2. // pvd调试配置
  3. export interface physicsDebugConfig {
  4. isNetwork: boolean; // 采用网络的方式
  5. ip?: string; // 如果isNetwork为true,调试信息会通过tcp转发的方式转发到打开了pvd调试软件的电脑,需要注意的是,防火墙要对pvd打开
  6. port?: 5425; // pvd默认接口
  7. timeout?: 1000; // 默认耗时
  8. path?: string; // 如果isNetwork为false,调试信息会通过写本地文件的方式落地,文件名建议为xxx.pxd2,导入pvd调试即可
  9. }
  10. export enum QueryTriggerInteraction {
  11. UseGlobal = 0,
  12. Ignore = 1,
  13. Collide = 2
  14. }
  15. export class PhysSystem {
  16. constructor(config?: physicsDebugConfig);
  17. gravity: RawVec3f;
  18. bounceThreshold: number;
  19. defaultMaxAngularSpeed: number;
  20. defaultSolverIterations: number;
  21. defaultSolverVelocityIterations: number;
  22. sleepThreshold: number;
  23. defaultContactOffset: number;
  24. destroyScene: () => void;
  25. createScene: () => number;
  26. Simulate: (step: number) => void;
  27. SyncFromTransforms: (() => void) | undefined; // added in 2021.06
  28. SetCollisionMask: (mask: ArrayBuffer) => void;
  29. Raycast: (origin: RawVec3f, unitDir: RawVec3f, distance: number, hit: RaycastHit, layerMask?: number, queryTriggerInteraction?: QueryTriggerInteraction) => boolean;
  30. RaycastAll: (origin: RawVec3f, unitDir: RawVec3f, distance: number, layerMask?: number, queryTriggerInteraction?: QueryTriggerInteraction) => RaycastHit[];
  31. CapsuleCast(p1: RawVec3f, p2: RawVec3f, radius: number, direction: RawVec3f, hit: RaycastHit, maxDistance: number, layerMask?: number, queryTriggerInteraction?: QueryTriggerInteraction): void;
  32. CapsuleCastAll: (p1: RawVec3f, p2: RawVec3f, radius: number, direction: RawVec3f, maxDistance: number, layerMask?: number, queryTriggerInteraction?: QueryTriggerInteraction) => RaycastHit[];
  33. BoxCast(center: RawVec3f, halfExt: RawVec3f, direction: RawVec3f, hit: RaycastHit, orientation: RawQuaternion, maxDistance: number, layerMask?: number, queryTriggerInteraction?: QueryTriggerInteraction): void;
  34. BoxCastAll: (center: RawVec3f, halfExt: RawVec3f, direction: RawVec3f, orientation: RawQuaternion, maxDistance: number, layerMask?: number, queryTriggerInteraction?: QueryTriggerInteraction) => RaycastHit[];
  35. OverlapBox: (center: RawVec3f, halfExt: RawVec3f, orientation: RawQuaternion, layermask?: number, queryTriggerInteraction?: QueryTriggerInteraction) => Collider[];
  36. OverlapCapsule: (p1: RawVec3f, p2: RawVec3f, radius: number, layermask?: number, queryTriggerInteraction?: QueryTriggerInteraction) => Collider[];
  37. }
  38. export class Rigidbody {
  39. constructor(system: PhysSystem);
  40. enabled?: boolean; // since 2021.06
  41. position: RawVec3f;
  42. rotation: RawQuaternion;
  43. AttachToEntity: (pollObj: any, id: number) => void;
  44. Remove(): void;
  45. Detach(): void;
  46. IsAttached(): boolean;
  47. }
  48. export enum CollisionDetectionMode {
  49. Discrete = 0,
  50. Continuous = 1,
  51. ContinuousDynamic = 2,
  52. ContinuousSpeculative = 3
  53. }
  54. export enum RigidbodyConstraints {
  55. None = 0,
  56. FreezePositionX = (1 << 0),
  57. FreezePositionY = (1 << 1),
  58. FreezePositionZ = (1 << 2),
  59. FreezeRotationX = (1 << 3),
  60. FreezeRotationY = (1 << 4),
  61. FreezeRotationZ = (1 << 5),
  62. FreezePosition = (FreezePositionX | FreezePositionY | FreezePositionZ),
  63. FreezeRotation = (FreezeRotationX | FreezeRotationY | FreezeRotationZ),
  64. FreezeAll = (FreezePosition | FreezeRotation)
  65. }
  66. export enum ForceMode {
  67. kForceModeForce = 0,
  68. kForceModeImpulse = (1 << 0),
  69. kForceModeVelocityChange = (1 << 1),
  70. kForceModeAcceleration = (1 << 2)
  71. }
  72. export enum CombineMode {
  73. eAverage = 0,
  74. eMin,
  75. eMultiply,
  76. eMax
  77. }
  78. export enum CookingFlag {
  79. None = 0,
  80. CookForFasterSimulation = 1 << 0,
  81. EnableMeshCleaning = 1 << 1,
  82. WeldColocatedVertices = 1 << 2
  83. }
  84. export enum CollisionFlags {
  85. None = 0,
  86. Sides = 1 << 0,
  87. Above = 1 << 1,
  88. Below = 1 << 2
  89. }
  90. export class RawVec3f {
  91. constructor();
  92. constructor(x: number, y: number, z: number);
  93. x: number;
  94. y: number;
  95. z: number;
  96. }
  97. export class RawQuaternion {
  98. constructor();
  99. constructor(x: number, y: number, z: number, w: number);
  100. x: number;
  101. y: number;
  102. z: number;
  103. w: number;
  104. }
  105. export class Collider {
  106. attachedRigidbody: Rigidbody;
  107. bounds: Bounds;
  108. name: string;
  109. contactOffset: number;
  110. enabled: boolean;
  111. isTrigger: boolean;
  112. scale : RawVec3f;
  113. material?: Material;
  114. sharedMateiral?: Material;
  115. ClosestPoint: (raw: RawVec3f) => RawVec3f;
  116. ClosestPointOnBounds: (raw: RawVec3f) => RawVec3f;
  117. onCollisionEnter?: (collision: Collision) => void;
  118. onCollisionExit?: (collision: Collision) => void;
  119. onCollisionStay?: (collision: Collision) => void;
  120. onTriggerEnter?: (collision: Collision) => void;
  121. onTriggerExit?: (collision: Collision) => void;
  122. onTriggerStay?: (collision: Collision) => void;
  123. dettachRigidbody?: () => void;
  124. userData?: Object | null;
  125. layer: number;
  126. }
  127. export class BoxCollider extends Collider {
  128. constructor(system: PhysSystem, center: RawVec3f, size: RawVec3f);
  129. center: RawVec3f;
  130. size: RawVec3f;
  131. }
  132. export class SphereCollider extends Collider {
  133. constructor(system: PhysSystem, center: RawVec3f, radius: number);
  134. center: RawVec3f;
  135. radius: number;
  136. }
  137. export class CapsuleCollider extends Collider {
  138. constructor(system: PhysSystem, center: RawVec3f, height: number, radius: number);
  139. center: RawVec3f;
  140. height: number;
  141. radius: number;
  142. direction: number;
  143. }
  144. export class MeshCollider extends Collider {
  145. constructor(system: PhysSystem, convex: boolean, cookingOptions: number, sharedMesh: PhysMesh);
  146. cookingOptions: number;
  147. sharedMesh: PhysMesh | null;
  148. convex: boolean;
  149. }
  150. export class CharacterController extends Collider {
  151. constructor(system: PhysSystem);
  152. position: RawVec3f;
  153. center: RawVec3f;
  154. collisionFlags: CollisionFlags;
  155. detectCollisions: boolean;
  156. enableOverlapRecovery: boolean;
  157. height: number;
  158. isGrounded: boolean;
  159. minMoveDistance: number;
  160. radius: number;
  161. skinWidth: number;
  162. slopeLimit: number;
  163. stepOffset: number;
  164. velocity: RawVec3f;
  165. Move: (movement: RawVec3f) => CollisionFlags;
  166. SimpleMove: (speed: RawVec3f) => boolean;
  167. AttachToEntity: (pollObj: any, id: number) => void;
  168. OnControllerColliderHit?: (hit: ControllerColliderHit) => void;
  169. }
  170. export interface ContactPoint {
  171. normal: RawVec3f;
  172. this_collider: Collider;
  173. other_collider: Collider;
  174. point: RawVec3f;
  175. separation: number;
  176. }
  177. export interface Collision {
  178. collider: Collider;
  179. contacts: Array<ContactPoint>;
  180. impulse: RawVec3f;
  181. relative_velocity: RawVec3f;
  182. }
  183. export interface ControllerColliderHit {
  184. collider: Collider;
  185. controller: CharacterController;
  186. moveDirection: RawVec3f;
  187. normal: RawVec3f;
  188. moveLength: number;
  189. point: RawVec3f;
  190. }
  191. export class Bounds {
  192. constructor(center: RawVec3f, size: RawVec3f);
  193. center: RawVec3f;
  194. extents: RawVec3f;
  195. max: RawVec3f;
  196. min: RawVec3f;
  197. size: RawVec3f;
  198. ClosestPoint: (point: RawVec3f) => RawVec3f;
  199. Contains: (point: RawVec3f) => boolean;
  200. Expand: (amount: number) => void;
  201. Intersects: (bounds: Bounds) => boolean;
  202. SetMinMax: (min: RawVec3f, max: RawVec3f) => void;
  203. SqrDistance: (point: RawVec3f) => number;
  204. }
  205. export class Material {
  206. constructor(system: PhysSystem);
  207. dynamicFriction: number;
  208. staticFriction: number;
  209. bounciness: number;
  210. frictionCombine: CombineMode;
  211. bounceCombine: CombineMode;
  212. id: Number;
  213. }
  214. export class DynamicRigidbody extends Rigidbody {
  215. mass: number;
  216. angularDamping: number;
  217. angularVelocity: RawVec3f;
  218. centerOfMass: RawVec3f;
  219. collisionDetectionMode: CollisionDetectionMode;
  220. constraints: number;
  221. detectCollisions: boolean;
  222. linearDamping: number;
  223. freezeRotation: boolean;
  224. inertiaTensor: number;
  225. // inertiaTensorRotation
  226. // interpolation
  227. isKinematic: boolean;
  228. maxAngularVelocity: number;
  229. maxDepenetrationVelocity: number;
  230. sleepThreshold: number;
  231. solverIterations: number;
  232. solverVelocityIterations: number;
  233. useGravity: boolean;
  234. velocity: RawVec3f;
  235. userData?: Object | null;
  236. GetWorldCenterOfMass: () => RawVec3f;
  237. AddForce: (force: RawVec3f, mode: ForceMode) => void;
  238. AddTorque: (torque: RawVec3f, mode: ForceMode) => void;
  239. IsSleeping: () => boolean;
  240. Sleep: () => void;
  241. WakeUp: () => void;
  242. AddExplosionForce: (explosionForce: number, explosionPosition: RawVec3f,
  243. explosionRadius: number, upwardsModifier: number, mode: ForceMode) => void;
  244. AddForceAtPosition: (force: RawVec3f, position: RawVec3f, mode: ForceMode) => void;
  245. AddRelativeForce: (force: RawVec3f, mode: ForceMode) => void;
  246. AddRelativeTorque: (torque: RawVec3f, mode: ForceMode) => void;
  247. ClosestPointOnBounds: (position: RawVec3f) => RawVec3f;
  248. GetPointVelocity: (worldPoint: RawVec3f) => RawVec3f;
  249. GetRelativePointVelocity: (relativePoint: RawVec3f) => RawVec3f;
  250. MovePosition: (position: RawVec3f) => void;
  251. MoveRotation: (rotation: RawQuaternion) => void;
  252. ResetCenterOfMass: () => void;
  253. ResetInertiaTensor: () => void;
  254. SetDensity: (density: number) => void;
  255. // SweepTest: () => void;
  256. // SweepTestAll: () => void;
  257. }
  258. export class PhysMesh {
  259. constructor(system: PhysSystem);
  260. // set vertices
  261. SetVertices: (buffer: Float32Array, count: number) => void;
  262. // set indices
  263. SetTriangles: (buffer: Uint16Array | Uint32Array, count: number, useUint16: boolean) => void;
  264. }
  265. export class RaycastHit {
  266. constructor();
  267. collider: Collider;
  268. distance: number;
  269. normal: RawVec3f;
  270. point: RawVec3f;
  271. rigidbody: Rigidbody;
  272. }
  273. }