Finding magnitude of vector in given direction

1.7k views Asked by At

I am writing a tool in Unity3D which allows dragging points to resize shapes:

enter image description here

The drag points can only be moved along the surface normal (i.e. the example cube could not become deformed, only elongated).

Naturally the mouse can be dragged in any direction, so I am trying to calculate the distance dragged along the surface normal.

Currently I am doing the following:

public static float NormalMoveHandle(Vector3 position, Vector3 normal)
{
    Vector3 newPosition = FreeMoveHandle(position);
    Vector3 dragVector = newPosition - position;

    return Vector3.Dot(dragVector, normal) * dragVector.magnitude;
}

However I'm getting unpredictable results:

http://i.gyazo.com/35f3fc4b4e0471f3c5d70097ddc6f79f.mp4

When dragging more than a short distance the value will suddenly become NaN:

enter image description here

If I adjust my calculation I get more stable behaviour, but the handle becomes sluggish. I believe this demonstrates that the handles are functional; the problem lies with my maths.

return Vector3.Dot(dragVector, normal);

Is there an error in my magnitude calculation?

0

There are 0 answers