24 C
New York
Friday, August 8, 2025

physics – Finest technique to sync PhysicalObj to participant headset place?


I’m within the technique of designing a threejs-based webxr engine for easy VR video games. Proper now, I’m engaged on implementing physics with cannon-es, however I’m struggling to determine how you can sync the participant’s physics physique with their headset place.

In my engine, every Entity is made up of two features: VisualObj and (generally) PhysicalObj. For many entities, VisualObj is synced up with its PhysicalObj place every body.

Presently, every body the next code is run for the Participant entity:

    const pos = Digicam.getWorldPosition(new THREE.Vector3()).toArray();
    const head = pos[1] + 0.1;
    if (pos[1] !== 0) {
      if (!Participant.PhysicalObj) {
        Participant.PhysicalObj = new Objects.PPill({
          radius: 0.25,
          mass: 60,
          peak: head,
          offsetPos: [0, head / 2, 0],
          fixedRotation: true
        });
        World.addBody(Participant.PhysicalObj);
      }

      Participant.PhysicalObj.place.set(pos[0], Participant.PhysicalObj.place.y, pos[2]);
    }

First, it creates the participant’s PhysicalObj (a capsule form) from a premade operate. After that, it merely strikes the PhysicalObj’s x and z positions to the digital camera’s world place. I believe this works for when the participant is simply strolling round in actual life, however will trigger main points down the road with participant motion.

That is the operate that every Entity has to sync its VisualObj with its PhysicalObj (if relevant):

    Sync(cameraPos) {
     if (!this.PhysicalObj) return;
     this.VisualObj.place.copy(this.PhysicalObj.place);
     this.VisualObj.quaternion.copy(this.PhysicalObj.quaternion);

      if (this.isPlayer) {
       const vec = new THREE.Vector3(cameraPos.x, 0, cameraPos.z);
       this.VisualObj.place.sub(vec);
     }
   }

If the Entity in query is Participant, then it offsets itself by the digital camera’s native place, to forestall a suggestions loop (as a result of Digicam is a toddler of Participant.VisualObj).

My query is that this: Is there a greater technique to transfer the participant’s PhysicalObj to the digital camera’s place, reasonably than teleporting? Would making use of the drive / impulse essential to get the physique to the digital camera’s world place, and even altering its velocity to get it there, work higher? I believe it’s necessary that the PhysicalObj comply with the digital camera’s worldposition, so the participant can’t clip by partitions by strolling round in actual life.

Any assist could be a lot appreciated, I’ve been caught on this for fairly some time.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles