physicsRaycastResult.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { Vector3 } from "../Maths/math.vector";
  2. /**
  3. * Holds the data for the raycast result
  4. * @see https://doc.babylonjs.com/how_to/using_the_physics_engine
  5. */
  6. export class PhysicsRaycastResult {
  7. private _hasHit: boolean = false;
  8. private _hitDistance: number = 0;
  9. private _hitNormalWorld: Vector3 = Vector3.Zero();
  10. private _hitPointWorld: Vector3 = Vector3.Zero();
  11. private _rayFromWorld: Vector3 = Vector3.Zero();
  12. private _rayToWorld: Vector3 = Vector3.Zero();
  13. /**
  14. * Gets if there was a hit
  15. */
  16. get hasHit(): boolean {
  17. return this._hasHit;
  18. }
  19. /**
  20. * Gets the distance from the hit
  21. */
  22. get hitDistance(): number {
  23. return this._hitDistance;
  24. }
  25. /**
  26. * Gets the hit normal/direction in the world
  27. */
  28. get hitNormalWorld(): Vector3 {
  29. return this._hitNormalWorld;
  30. }
  31. /**
  32. * Gets the hit point in the world
  33. */
  34. get hitPointWorld(): Vector3 {
  35. return this._hitPointWorld;
  36. }
  37. /**
  38. * Gets the ray "start point" of the ray in the world
  39. */
  40. get rayFromWorld(): Vector3 {
  41. return this._rayFromWorld;
  42. }
  43. /**
  44. * Gets the ray "end point" of the ray in the world
  45. */
  46. get rayToWorld(): Vector3 {
  47. return this._rayToWorld;
  48. }
  49. /**
  50. * Sets the hit data (normal & point in world space)
  51. * @param hitNormalWorld defines the normal in world space
  52. * @param hitPointWorld defines the point in world space
  53. */
  54. public setHitData(hitNormalWorld: IXYZ, hitPointWorld: IXYZ) {
  55. this._hasHit = true;
  56. this._hitNormalWorld = new Vector3(hitNormalWorld.x, hitNormalWorld.y, hitNormalWorld.z);
  57. this._hitPointWorld = new Vector3(hitPointWorld.x, hitPointWorld.y, hitPointWorld.z);
  58. }
  59. /**
  60. * Sets the distance from the start point to the hit point
  61. * @param distance
  62. */
  63. public setHitDistance(distance: number) {
  64. this._hitDistance = distance;
  65. }
  66. /**
  67. * Calculates the distance manually
  68. */
  69. public calculateHitDistance() {
  70. this._hitDistance = Vector3.Distance(this._rayFromWorld, this._hitPointWorld);
  71. }
  72. /**
  73. * Resets all the values to default
  74. * @param from The from point on world space
  75. * @param to The to point on world space
  76. */
  77. public reset(from: Vector3 = Vector3.Zero(), to: Vector3 = Vector3.Zero()) {
  78. this._rayFromWorld = from;
  79. this._rayToWorld = to;
  80. this._hasHit = false;
  81. this._hitDistance = 0;
  82. this._hitNormalWorld = Vector3.Zero();
  83. this._hitPointWorld = Vector3.Zero();
  84. }
  85. }
  86. /**
  87. * Interface for the size containing width and height
  88. */
  89. interface IXYZ {
  90. /**
  91. * X
  92. */
  93. x: number;
  94. /**
  95. * Y
  96. */
  97. y: number;
  98. /**
  99. * Z
  100. */
  101. z: number;
  102. }