How to prevent XR camera moving through the wall by its backward movement?

For preventing teleport through the walls, WebXRDefaultExperience’s teleportation provided “addBlockMesh” method. But it doesn’t block the backward movements, is that a bug or something?

cc @RaananW our XR wizard

There is currently no support for collision detection when teleporting or moving backwards with the xr teleportation module. You can use the movement feature you move freely using your controllers, but this is a different UX, so it all depends on the solution you are implementing. In case you are using the movement feature instead of teleportation you can use babylon’s native collision detection to avoid moving through walls

1 Like

Finally I made something like this, at least it works:

scene.createDefaultXRExperienceAsync({
  floorMeshes: [ground]
}).then(experience => {
  const cameraXR = experience.baseExperience.camera
  blockerMeshes.map(mesh => experience.teleportation.addBlockerMesh(mesh))
  
  scene.onBeforeRenderObservable.add(() => {
    const ray = new Ray(Vector3.Zero(), Vector3.Zero(), 1.5)
    const rayHelper = new RayHelper(ray)
    const node = new AbstractMesh('null', scene)
    node.position.set(
      cameraXR.position.x,
      ground.getHeightAtCoordinates(
        cameraXR.position.x,
        cameraXR.position.z
      ) + 0.2,  // Just slightly higher than the ground
      cameraXR.position.z
    )
    node.rotationQuaternion = Quaternion.RotationAxis(
      Axis.Y, 
      cameraXR.rotationQuaternion.toEulerAngles().y
    )
    rayHelper.attachToMesh(node, new Vector3(0, 0, -1))
    let backRayIntersected = 0
    ray.intersectsMeshes(blockerMeshes).map(pickingInfo => if (pickingInfo.hit) backRayIntersected += 1)
    rayHelper.dispose()
    experience.teleportation.backwardsMovementEnabled = backRayIntersected === 0
  })
})
2 Likes