How to determine in which "direction" object was dragged/moved?

Hey there folks, I’m pretty new to Three.js and wanted to understand am I doing something wrong here.

So basically what I need is to understand in which axis the object was moved.
What am I doing currently is:

const diff = new Vector3();
diff.subVectors(objec.position, objectPositionOnMouseDown).normalize();

from here I can understand if x is not 0 then it moved in x axis etc.
This works when dragging in a single axis.

But when dragging freely, the object changes its two axis, and I am a bit stuck here.
Am I doing it wrong?

For 3 axis maybe need take object position before move and after move.
No “objectPositionOnMouseDown”

Hey! thanks for the answer.

Isn’t “objectPositionOnMouseDown” is before move? That’s called when we click the mouse to start dragging.

Can you upload example to codepen?

It’s a huge codebase, but I will try explaining in detail.

I have a onMouseDown listener, when it’s fired I store the objects position at that point.
Then I start dragging and getting the object position on every drag.
When dragging objects position is changing properly.
Now I want to understand the axis in which dominantly the object was dragged.

At the begin of all add code for remembering the position.
var last_position=new Vector3();
Into “onMouseDown” listener add code at the begin:
last_position=objec.position;
Into “onMouseUp” listener add code at the end:
last_position=diff.subVectors(last_position,objec.position).normalize();

Mistake here objec.position need rename to object.position

I’m not sure I understand this. What you compute with diff.subVectors(objec.position, objectPositionOnMouseDown).normalize(); is a direction vector in 3D space. So this is the answer to your question.

In other words, this vector indicates in which direction the object was moved/dragged.

2 Likes

I think @Mugen87 already gave the proper answer.

You find the “dominant” of the three cardinal directions (x, y, z-axis) by looking for the maximum (absolute) value of the direction vector’s x, y and z-components.