physics.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Physics</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <meta name="twitter:card" content="summary_large_image">
  8. <meta name="twitter:site" content="@threejs">
  9. <meta name="twitter:title" content="Three.js – Physics">
  10. <meta property="og:image" content="https://threejs.org/files/share.png">
  11. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  12. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  13. <link rel="stylesheet" href="../resources/lesson.css">
  14. <link rel="stylesheet" href="../resources/lang.css">
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../../build/three.module.js"
  19. }
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <div class="container">
  25. <div class="lesson-title">
  26. <h1>Physics</h1>
  27. </div>
  28. <div class="lesson">
  29. <div class="lesson-main">
  30. <p>
  31. Physics engines allow you to simulate physical phenomena like gravity, collisions, and forces within
  32. your 3D capabilities. In a typical three.js scene, objects are moved by directly modifying their
  33. position or rotation. When using a physics engine, however, you create a parallel physics world
  34. where bodies react to forces and collisions. You then synchronize the three.js meshes with these
  35. physics bodies on every frame, creating the illusion of a physically simulated environment.
  36. </p>
  37. <p>
  38. It should be noted that the physics engine does not necessarily have to be updated every frame.
  39. Usually, to keep experiences consistent, physics are updated at fixed time steps. For instance, it could
  40. be that we are running the game loop at 60fps but the physics engine at 30fps (that is 1/30=3.33ms)
  41. while updating the three.js meshes' transforms (e.g., positions and rotations) with the most recent
  42. state from the physics engine.
  43. </p>
  44. <p>
  45. Physics simulations are particularly useful for games, interactive visualizations, and any
  46. application requiring realistic object behavior, such as objects falling, bouncing, or sliding.
  47. </p>
  48. <h2>Integration Approaches</h2>
  49. <p>
  50. There are three main ways to integrate a physics engine into a three.js project:
  51. </p>
  52. <h3>1. Using Three.js Physics Addons</h3>
  53. <p>
  54. Three.js provides wrapper classes for several popular physics engines in the
  55. <i>examples/jsm/physics</i> directory. These addons simplify the setup process by handling the
  56. initialization of the physics world and the synchronization of meshes.
  57. </p>
  58. <p>
  59. Available addons include:
  60. </p>
  61. <ul>
  62. <li><b>AmmoPhysics:</b> A wrapper for Ammo.js (Bullet Physics).</li>
  63. <li><b>JoltPhysics:</b> A wrapper for Jolt Physics.</li>
  64. <li><b>RapierPhysics:</b> A wrapper for Rapier.</li>
  65. </ul>
  66. <p>
  67. These addons effectively hide much of the complexity of the underlying engines. For standard use
  68. cases, they offer a very quick way to get started.
  69. </p>
  70. <h4>
  71. Examples
  72. </h4>
  73. <ul>
  74. <li><a href="https://threejs.org/examples/physics_ammo_instancing.html" target="_blank">physics / ammo / instancing</a></li>
  75. <li><a href="https://threejs.org/examples/physics_jolt_instancing.html" target="_blank">physics / jolt / instancing</a></li>
  76. <li><a href="https://threejs.org/examples/physics_rapier_instancing.html" target="_blank">physics / rapier / instancing</a></li>
  77. </ul>
  78. <h3>2. Using 3rd-Party Physics JS/TS Libraries</h3>
  79. <p>
  80. Many physics engines are written directly in JavaScript or TypeScript and are designed to work
  81. easily with the web ecosystem. Libraries like <b>cannon-es</b> are popular choices because they are
  82. lightweight and easy to integrate specifically with three.js.
  83. </p>
  84. <p>
  85. When using these libraries, you instantiate the physics world and bodies yourself, then manually
  86. copy the position and quaternion from the physics body to the three.js mesh in your animation loop.
  87. </p>
  88. <h4>
  89. Projects
  90. </h4>
  91. <ul>
  92. <li><b><a href="https://github.com/pmndrs/cannon-es" target="_blank">cannon-es</a></b>: A lightweight 3D physics engine purely written in JS/TS. Under an MIT license. Apparently no longer maintained (latest commit more than a couple years ago).</li>
  93. <li><b><a href="https://github.com/schteppe/cannon.js" target="_blank">cannon.js</a></b>: A lightweight 3D physics engine purely written in JavaScript. Under an MIT license. No longer maintained (latest commit more than a couple years ago). Consider using its more recent fork cannon-es.</li>
  94. <li><b><a href="https://github.com/lo-th/phy" target="_blank">phy</a></b>: Physics engine for three.js purely written in JavaScript. Under an MIT license. Latest commit. Currently maintained.</li>
  95. <li><b><a href="https://github.com/lo-th/Oimo.js" target="_blank">Oimo.js</a></b>: A no-longer maintained lightweight 3D physics engine written purely in JavaScript. Under an MIT license. Consider using phy instead (as per the author's advise).</li>
  96. </ul>
  97. <p>
  98. It should also be noted that there are a couple of 3D physics engines that are seemingly written in JS/TS but are in reality calling other standalone 3D physics engines. For example:
  99. </p>
  100. <ul>
  101. <li><b><a href="https://github.com/chandlerprall/Physijs" target="_blank">Physijs</a></b>: Calls ammo.js under the hood to do physics work in a separate thread (using web workers). Under MIT license. Apparently no longer maintained (latest commit more than a couple of years ago).</li>
  102. <li><b><a href="https://github.com/enable3d/enable3d" target="_blank">enable3d</a></b>: 3D physics framework for three.js built on top of ammo.js. Under LGPL-3.0 license. Apparently maintained.</li>
  103. </ul>
  104. <h3>3. Importing WASM-based Engines</h3>
  105. <p>
  106. For maximum performance, stability, and precision, especially with complex simulations, you can use physics engines written in
  107. C++ or Rust (or any other language that supports WASM) that have been compiled to WebAssembly (WASM). Engines like <b>Ammo.js</b> (a port of
  108. Bullet Physics) and <b>Rapier</b> fall into this category.
  109. </p>
  110. <p>
  111. While this approach offers the most features and best performance, it often requires more setup code
  112. to handle the WASM memory management and interaction with the physics API directly.
  113. </p>
  114. <h4>
  115. Examples
  116. </h4>
  117. <ul>
  118. <li><a href="https://threejs.org/examples/physics_ammo_break.html" target="_blank">physics / ammo / break</a></li>
  119. <li><a href="https://threejs.org/examples/physics_ammo_cloth.html" target="_blank">physics / ammo / cloth</a></li>
  120. <li><a href="https://threejs.org/examples/physics_ammo_rope.html" target="_blank">physics / ammo / rope</a></li>
  121. <li><a href="https://threejs.org/examples/physics_ammo_terrain.html" target="_blank">physics / ammo / terrain</a></li>
  122. <li><a href="https://threejs.org/examples/physics_ammo_volume.html" target="_blank">physics / ammo / volume</a></li>
  123. </ul>
  124. <h4>
  125. Projects
  126. </h4>
  127. <ul>
  128. <li><b><a href="https://github.com/jrouwe/JoltPhysics" target="_blank">JoltPhysics</a></b>: A multi core friendly rigid body physics and collision detection library. Written in C++. Under MIT license. Actively maintained. Proven with its usage in world-renowned titles and game engines including: Horizon Forbidden West, Death Stranding 2, and official supported by the Godot game engine.</li>
  129. <li><b><a href="https://github.com/NVIDIA-Omniverse/PhysX" target="_blank">PhysX</a></b>: Industry-standard realtime 3D physics engine provided by NVIDIA. Under BSD-3-Clause license. Actively maintained and very stable.</li>
  130. <li><b><a href="https://github.com/dimforge/rapier" target="_blank">Rapier</a></b>: 2D and 3D physics engines focused on performance. Written in Rust. Under an MIT license. Actively maintained.</li>
  131. <li><b><a href="https://github.com/bulletphysics/bullet3" target="_blank">Bullet</a></b>:
  132. Real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc. Written in C++. Under a ZLIB license. Potentially no longer maintained.</li>
  133. </ul>
  134. <p>
  135. Some of these multi-platform 3D physics engines have a ready-to-use WASM port, including:
  136. </p>
  137. <ul>
  138. <li><b><a href="https://github.com/jrouwe/JoltPhysics.js" target="_blank">JoltPhysics.js</a></b>: Port of JoltPhysics to JavaScript using Emscripten. Under MIT license. Currently maintained.</li>
  139. <li><b><a href="https://github.com/fabmax/physx-js-webidl" target="_blank">physx-js-webidl</a></b>: Javascript WASM bindings for Nvidia PhysX. Under MIT license. Currently maintained.</li>
  140. <li><b><a href="https://github.com/dimforge/rapier.js" target="_blank">Rapier.js</a></b>: Official JavaScript bindings for the Rapier physics engine. Under Apache-2.0 license. Actively maintained.</li>
  141. <li><b><a href="https://github.com/kripken/ammo.js" target="_blank">Ammo.js</a></b>: Direct port of the Bullet physics engine to JavaScript using Emscripten. No longer maintained (latest commit a couple of years ago). Under an MIT-like custom permissive license.</li>
  142. </ul>
  143. </div>
  144. </div>
  145. </div>
  146. <script src="../resources/prettify.js"></script>
  147. <script src="../resources/lesson.js"></script>
  148. </body>
  149. </html>
粤ICP备19079148号