Reputation: 2705
Suppose I create an arrow helper, e.g. something like:
const arrowPosition = new THREE.Vector3(0, 0, 0);
const arrowPosition = new THREE.Vector3(2, -2, 0);
arrowDirection.subVectors( scene.position, new THREE.Vector3(1, 1, 1)).normalize();
arrow = new THREE.ArrowHelper( arrowDirection, arrowPosition, arrowLength, 0xffff00, arrowHeadLength, arrowHeadWidth);
After creating it, how do I access its direction?
Upvotes: 1
Views: 779
Reputation: 5527
Just for educational purposes (please don't do this in production code)...
The setDirection
source code (shown in @RobertSilver 's answer) sets arrow.quaternion
to be the quaternion that rotates the y axis (0,1,0)
to the arrow direction dir
. So, if you know arrow.quaternion
and you want to deduce dir
, just rotate (0,1,0)
by arrow.quaternion
, and that's your answer:
const dir = new THREE.Vector3(0,1,0).applyQuaternion(arrow.quaternion);
It would be a bad idea to actually do this in production code, though, since it relies on the undocumented internal implementation of THREE.ArrowHelper
which may change in the future (probably not, but it's best to simply refrain from making this kind of gamble so that you don't get occasionally bitten). If your code is the code that sets the arrow direction in the first place, then it would be better to just save that value when you set it (in the arrow helper object's .userData
, if you like, as suggested by @prisoner849 ).
Upvotes: 0
Reputation: 72
In short, I'm not sure there is an easy way to access the direction, at least in a vector3 format. The value dir
used in the ArrowHelper
constructor (as described in the docs) is never assigned directly to a vector property of ArrowHelper
or the Object3D
parent class, but is instead used to set the quaternion property of the ArrowHelper
, inherited from the parent class.
See the code snippet below from the helper source code:
setDirection(dir) {
// dir is assumed to be normalized
if (dir.y > 0.99999) {
this.quaternion.set(0, 0, 0, 1);
} else if (dir.y < - 0.99999) {
this.quaternion.set(1, 0, 0, 0);
} else {
_axis.set(dir.z, 0, - dir.x).normalize();
const radians = Math.acos(dir.y);
this.quaternion.setFromAxisAngle(_axis, radians);
}
}
As listed in the Object3D docs, the quaternion
describes the helper's rotation (quaternion docs). A better discussion on the math might be found on this unity forum, but to get the vector direction you'll need to use the quaternion
field
Hope this helps and if someone can improve this explanation please do, my experience with Eulers and quaternions is limited
Upvotes: 1