2

I want to find the "real" position of an a-frame element. Not the one shown in the position attribute. But the one that's the total of all the parents and relative to the world/scene position?

alex
  • 7,111
  • 15
  • 50
  • 77

2 Answers2

3

You can look up world coordinates in three.js: https://threejs.org/docs/#api/en/core/Object3D.getWorldPosition

It looks something like:

var worldPos = new THREE.Vector3();
worldPos.setFromMatrixPosition(el.object3D.matrixWorld);
Blake Frederick
  • 1,510
  • 20
  • 31
ngokevin
  • 12,980
  • 2
  • 38
  • 84
0

To give a more complete / updated answer from 2021 / an A-Frame 1.1.0 perspective,

As stated here,

var parent = new THREE.Object3D();
parent.position.set(100, 100, 100);

var child = new THREE.Object3D();
child.position.set(100, 100, 100);
parent.add(child);

scene.add(parent);

var target = new THREE.Vector3(); // create once an reuse it

child.getWorldPosition( target );

target will contain the result.

in A-Frame speak, that should be,

AFRAME.registerComponent('some-component', {
  init() {
    const realWorldPosition = this.el.object3D.getWorldPosition(new THREE.Vector3())
  },
};
Kyle Baker
  • 3,424
  • 2
  • 23
  • 33