# Integrating Physx WASM into BabylonJS project

**URL:** <https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964>\
**Category:** Questions\
**Created:** [November 2, 2021, 3:31pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964 "2021-11-02T15:31:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![bozworth](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bozworth/32/6484_2.png) [@bozworth](https://forum.babylonjs.com/u/bozworth)\
**Post date:** [November 2, 2021, 3:31pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/1 "2021-11-02T15:31:35Z")

</div>

> **[GitHub - umutkarakoc/physx-wasm](https://github.com/umutkarakoc/physx-wasm)**
>
> Contribute to umutkarakoc/physx-wasm development by creating an account on GitHub.

Im trying to integrate this into a multiplayer WebRTC Project but don’t know where to begin… Do I have to create a new Plugin for this or Can I utilise what already exists?

I found the other physics libs lacking!

---

<div class="post-metadata">

**Author:** ![PirateJC](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/piratejc/32/1003_2.png) [@PirateJC](https://forum.babylonjs.com/u/PirateJC)\
**Post date:** [November 2, 2021, 3:50pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/2 "2021-11-02T15:50:25Z")

</div>

Pinging @syntheticmagus to see if he has a little guidance to offer on this.

---

<div class="post-metadata">

**Author:** ![syntheticmagus](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/syntheticmagus/32/395_2.png) [@syntheticmagus](https://forum.babylonjs.com/u/syntheticmagus)\
**Post date:** [November 2, 2021, 4:25pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/3 "2021-11-02T16:25:17Z")

</div>

Hi bozworth,

The direct answer is that I think you would need to [add your own physics engine](https://doc.babylonjs.com/divingDeeper/physics/addPhysicsEngine) to use this, though that might end up being even trickier than anticipated for this particular use case. (If WebRTC implies P2P, it might be hard to get multiplayer physics collisions working as expected without some sort of [authoritative server](https://www.npmjs.com/package/nengi).)

The other thing to note about this particular repo is that it doesn’t appear to have any sort of license whatsoever, so it’s not clear under what parameters that code is shared or what kind of usage is okay. Do you have a way to reach out to the repo owners for clarification about that?

---

<div class="post-metadata">

**Author:** ![jeremy-coleman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeremy-coleman/32/15493_2.png) [@jeremy-coleman](https://forum.babylonjs.com/u/jeremy-coleman)\
**Post date:** [November 2, 2021, 10:39pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/4 "2021-11-02T22:39:48Z")

</div>

Isnt phsyx a hardware api for nvidia gpus ? Not sure that makes sense to do.

@syntheticmagus  
Webrtc is just how you use udp in the browser. Its painful to use, but data channel gives u unordered encrypted udp. Quic3/web transport is doable now but getting the server deployment setup isnt so easy

---

<div class="post-metadata">

**Author:** ![syntheticmagus](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/syntheticmagus/32/395_2.png) [@syntheticmagus](https://forum.babylonjs.com/u/syntheticmagus)\
**Post date:** [November 3, 2021, 12:50am UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/7 "2021-11-03T00:50:25Z")

</div>

> [@jeremy-coleman](#):
>
> Webrtc is just how you use udp in the browser.

Right, I was referring to the network architecture. If you’re doing physics on a networked app (in my limited understanding), there are challenges associated with the fact that not every client believes everything to be in the same place at the same time. Because of this, if multiple clients are running the simulation, they may get different answers because their states don’t match, which can be tricky to reconcile. I think there are a lot of ways to solve this, but the one I’ve heard most about is the “authoritative server” model where the server runs a physics simulation that can override the results from the client (if they even have local simulations at all). A P2P architecture based on (my again limited understanding of) WebRTC wouldn’t have an active server, so it might need to use some other approach to figuring out whose physics simulation was “right.”

---

<div class="post-metadata">

**Author:** ![jeremy-coleman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeremy-coleman/32/15493_2.png) [@jeremy-coleman](https://forum.babylonjs.com/u/jeremy-coleman)\
**Post date:** [November 3, 2021, 11:25am UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/8 "2021-11-03T11:25:06Z")

</div>

webrtc doesnt have to be p2p clients in the sense of mesh topology, you can still be server authoritative in the sense of single source of truth for game state. its not ideal but its the only option really. chromium had a web channel api or something along those lines, but they removed it. next thing is now web transport which is pretty cool, because you can use it in a worker. it requires http3 connection though and not very many examples out there. i’ve always thought an alternative to webrtc could be to encode game state into a video or audio stream but i havnt seen anything doing that. hoping web transport gets some adoption so i can copy paste things

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [December 3, 2021, 12:14am UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/9 "2021-12-03T00:14:02Z")

</div>

@bozworth just curious, did you ever figure out the integration? We’d love to know or if you have any other further questions!

---

<div class="post-metadata">

**Author:** ![jeremy-coleman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeremy-coleman/32/15493_2.png) [@jeremy-coleman](https://forum.babylonjs.com/u/jeremy-coleman)\
**Post date:** [December 4, 2021, 1:16pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/10 "2021-12-04T13:16:56Z")

</div>

I saw this recently and thought about this thread. It seems people are indeed actively developing with it. This is a link to where the wasm is, if u back out into the main repo there is a link to web demos too.

> **[kool/kool-demo/src/jsMain/resources at main · fabmax/kool](https://github.com/fabmax/kool/tree/main/kool-demo/src/jsMain/resources)**
>
> main/kool-demo/src/jsMain/resources

I skimmed over the physx docs, which says the default is to run on the cpu with optional extension to run on gpu depending on complexity of scene. I am not a cg expert by any means, but from my understanding , running stuff on the gpu when many objects are the same shape (thin instances?) is very performant, but that is also not a very realistic expectation, so when shapes of collision objects vary a lot, it’ll be a lot faster on the cpu. This is why some physics demos of like, how many blocks can this falling tower hold, are not really very good, because it will favor gpu physics over cpu physics, which isnt representative of game perf. However, if there was some physics engine that could split static objects and dynamic ones, putting the static on gpu and dynamic on cpu, that seems like it’d be good if reconciliation time is acceptably low. idk if its feasible or makes even sense in practice though.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [December 4, 2021, 1:32pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/11 "2021-12-04T13:32:39Z")

</div>

> [@jeremy-coleman](#):
>
> However, if there was some physics engine that could split static objects and dynamic ones, putting the static on gpu and dynamic on cpu, that seems like it’d be good if reconciliation time is acceptably low. idk if its feasible or makes even sense in practice though.

One can assign static/dynamic properties to objects as some metadata to pass to the physics engine and split processing between CPU and GPU. Would be interesting to see an implementation; if there are GPU particles there should be some kind of GPU physics, why not 🙂

---

<div class="post-metadata">

**Author:** ![jeremy-coleman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeremy-coleman/32/15493_2.png) [@jeremy-coleman](https://forum.babylonjs.com/u/jeremy-coleman)\
**Post date:** [December 4, 2021, 1:54pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/12 "2021-12-04T13:54:22Z")

</div>

Here is a glsl impl from the cannon author [gpu-physics.js/src/shaders at master · schteppe/gpu-physics.js · GitHub](https://github.com/schteppe/gpu-physics.js/tree/master/src/shaders) . At the time of its authoring, i think the browser api was too restricting, but now its a different story. Maybe shader god x aka Mr. Popov aka my compute hero has insight on feasibility of a hybrid system using compute shaders. Not gonna at him on a Saturday morning though;)

---

<div class="post-metadata">

**Author:** ![bozworth](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bozworth/32/6484_2.png) [@bozworth](https://forum.babylonjs.com/u/bozworth)\
**Post date:** [December 5, 2021, 2:40pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/13 "2021-12-05T14:40:43Z")

</div>

[https://physx-js-example.deminetix.vercel.app/](https://physx-js-example.deminetix.vercel.app/)

Yeah essentially there is already an implementation in THREE, from reading it,  
its simply about passing the objects through into the simulator and getting back values to then update in babylon / js

In the example it shows you can install physxjs an npm package and in the node\_modules folder you get a distributed wasm build :  
import physxModule from ‘physx-js/dist/physx.release.wasm’

Can initialise it :

```auto
physics = PhysX.PxCreatePhysics(
    version,
    foundation,
    new PhysX.PxTolerancesScale(),
    false,
    null
  )
  PhysX.PxInitExtensions(physics, null)
  const sceneDesc = PhysX.getDefaultSceneDesc(
    physics.getTolerancesScale(),
    0,
    physxSimulationCallbackInstance
  )
  scene = physics.createScene(sceneDesc)

```

Then to create a body :

```auto
  geometry = new PhysX.PxBoxGeometry(
        // PhysX uses half-extents
        entity.body.size[0] / 2,
        entity.body.size[1] / 2,
        entity.body.size[2] / 2
      )

```

Then its just a case of calling the update  
physics update

```auto
export const update = entities => {
  scene.simulate(1 / 60, true)
  scene.fetchResults(true)
  entities.forEach(entity => {
    const body = bodies[entity.id]
    const transform = body.getGlobalPose()
    entity.transform.position[0] = transform.translation.x

```

main app loop

```auto
const update = () => {
  tick++
  physics.update(entities)
  renderer.update(entities)
  requestAnimationFrame(update)
}

```

I will post a babylonjs example

---

<div class="post-metadata">

**Author:** ![jeremy-coleman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeremy-coleman/32/15493_2.png) [@jeremy-coleman](https://forum.babylonjs.com/u/jeremy-coleman)\
**Post date:** [December 5, 2021, 4:12pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/14 "2021-12-05T16:12:19Z")

</div>

Cool. Looking forward to trying it out.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [January 17, 2022, 12:58am UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/15 "2022-01-17T00:58:49Z")

</div>

Here are the license terms, it is open source - [PhysX License — NVIDIA PhysX SDK 4.0 Documentation](https://gameworksdocs.nvidia.com/PhysX/4.0/documentation/PhysXGuide/Manual/License.html)

---

<div class="post-metadata">

**Author:** ![han\_Gao](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/han_gao/32/28345_2.png) [@han\_Gao](https://forum.babylonjs.com/u/han_Gao)\
**Post date:** [January 5, 2023, 7:00am UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/16 "2023-01-05T07:00:27Z")

</div>

Is the problem solved? longing for your babylon demo 😀

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [February 3, 2023, 4:07pm UTC](https://forum.babylonjs.com/t/integrating-physx-wasm-into-babylonjs-project/24964/17 "2023-02-03T16:07:41Z")

</div>

It’s not PhysX but Babylon 6.0 will have some very cool physics updates 👁
