简体   繁体   中英

In Three.js, how to translate a Vector3?

I have created a Vector3() called ori , and I have populated its coordinates x, y and z. How, now, do I translate this vector, say along axis z, of the indicated value?

I tried this:

 ori.translateZ( - 100);

This gets me an error (TypeError: Cannot read property 'translateZ' of undefined)

Matey gave the answer you need, but didn't tell you why your method didn't work. ori is a Vector3 and not a Object3D. translateZ() is a method of the Object3D class, but not a method of the Vector3 class. If the position member of an Object3D class had been set to equal ori (position is a Vector3) then translateZ on that Object3D instance would have worked.

Your understanding is correct. The answer juagicre gave would only change the z value to-100

If you want to translate by a single axis, it's as simple as adding the translation value:

ori.z += -100;

If you want to translate by a vector, it's again very simple:

var trans = new THREE.Vector3(-100,-200,-300);
ori.add(trans);
ori.setZ(-100);

Vector3文档

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM