Why in THREE.js the arithmetic expression in Object3D.translateX isn't evaluated as expected or Vector.copy doesn't work correctly in animate loop?

Viewed 47

I'm trying to move an object called "car" via the dat.gui. If the user changes the x value using the dat.gui slider, the car should move along the x-axis of its local coordinate system. here I have copied the part of the code that is causing me problems:

var m = new THREE.Vector3;
m.copy(car.position);
if (changed.key=='X') car.translateX(changed.value-car.worldToLocal(m).x);

My problem is that the expression in car.translateX always evaluates to the value that is in changed.value. The part after the minus has no effect at all or maybe is permanently 0. I have printed the values ​with console.log and the values ​​of car.position.x and m change in each step, but the subtraction still delivers in every step only the result that is already in changed.value anyway. Can someone help me and tell me why this happens? Unfortunately, I am absolutely stuck.

1 Answers

car.worldToLocal(m)

I'm afraid this piece of code makes no sense since car.position (and thus m) already represents the car's position in local space.

Instead of using translateX() you achieve the same result by modifying car.position directly:

car.position.x = changed.value;
Related