2

I am using AFRAME for my metaverse application. I am trying to get Third party perspective(TPP) for my avatars in it. I am using rotate-with-camera component to my model so that the avatar rotates when the came is rotated. And I have positioned my camera a little backside of my avatar to give TPP effect. Gave wasd controls for both the camera and avatar.

I am able to get the movement sync perfectly, but the issue is with the camera rotation. The camera is rotating around the origin i.e. taking position="0 0 0" as its center rather than taking the avatar position as center.

This is the register component which I am using.

AFRAME.registerComponent("rotate-with-camera", {
tick: (function () {
// create once
const tmpq = new THREE.Quaternion();
const tmpe = new THREE.Euler();

return function (t, dt) {
  if (!this.el.sceneEl.camera) return; // ignore when there is no camera
  const cam = this.el.sceneEl.camera.el; // get the camera entity
  cam.object3D.getWorldQuaternion(tmpq); // get the world rotation
  tmpe.setFromQuaternion(tmpq, "YXZ");
  // set attribute is necesarry for wasd-controls
  this.el.setAttribute("rotation", {
    x: 0,
    y: (tmpe.y * 180) / Math.PI,
    z: 0,
  });
};
})(),
});

This is the came and avatar which I am using in the code.

<!--Camera -->
  <a-entity look-at=".avatar"  look-controls="pointerLockEnabled: false" wasd-controls="acceleration:12;">
    <a-entity camera="near:0.01;" position="4 1.72 177.5"></a-entity>
  </a-entity>

  <!-- Avatar -->
  <a-entity class="avatar" >
    <a-gltf-model
    id="player"
    src="#boyBlue"
    position="4 0.12 175"
    animation-mixer="clip:Idle"
    wasd-controls="acceleration: 12"
    rotate-with-camera
    >
    </a-gltf-model>
  </a-entity>

I want my camera to rotate by taking my avatar position as its axis. How can I proceed with this?

1 Answers1

1

It is happening because Your camera has an x offset in regard to the parent entity, hence:

enter image description here


An easy solution would be to move the offset to the parent entity:

<a-entity look-controls wasd-controls="acceleration:12;" position="4 0 0">
<a-entity camera="near:0.01;" position="0 1.72 177.5"></a-entity>
</a-entity>

glitch here


But it would be better to follow the answer where you found the component, and have a javascript controller to move the camera behind the box instead of syncing the positions, both of them having wasd controls.

The controller is simple:

AFRAME.registerComponent("follow-target", {
  schema: {
    target: {type: "selector"}
  },
  tick: (function() {
    // create once
    const tmpv = new THREE.Vector3();

    return function(t, dt) {
      if (!this.data.target) return; // ignore when there is no target
      const target = this.data.target.object3D; // get the mesh
      // track the position
      const position = target.getWorldPosition(tmpv); // get the world position
      this.el.object3D.position.lerp(tmpv, 0.5) // linear interpolation towards the world position
    }
  })()
})

glitch here

Piotr Adam Milewski
  • 14,150
  • 3
  • 21
  • 42
  • Thankyou for the wonderful explanation Adam. I have tried the follow-box register component. But the problem is that most of the times I am getting this error. *Uncaught TypeError: Cannot read properties of undefined (reading 'getWorldPosition')*. The line in follow-box component const target = this.data.target.getObject3D("mesh"); is not taking any value inside the target variable and giving undefined when trying to console. This issue is not being faced when I am using any basic AFrame object like a cube. This is faced when I am using my personalised avatar. Any view on this this? – Raghavendra Reddy Jun 23 '22 at 03:39
  • 1
    @RaghavendraReddy I've made an edit, you can just track the `object3D`, with models you may need to wait until it's loaded before accessing the `mesh`, abstract objects like rigs won't have a `mesh`. – Piotr Adam Milewski Jun 23 '22 at 13:20
  • @RaghavendraReddy do you have more questions regarding this question? – Piotr Adam Milewski Jun 27 '22 at 18:11
  • No Adam, You explanation was good enough. But, I am having a question regarding the navmesh for the avatar. Which I have given here. https://stackoverflow.com/questions/72768021/navmesh-in-tpp-for-avatar-in-a-frame – Raghavendra Reddy Jun 28 '22 at 06:32