Vector3f truncate method

Would it be possible + desirable to add the following method in the Vector3f class?



    /**
     * Truncates the vector's magnitude so that it does not exceed specified <code>max</code>.
     *
     * @param max the maximum magnitude of the vector.
     *
     * @return the truncated vector.
     */
    public static Vector3f truncate(float max) {
        if (length() > max) {
            normalizeLocal().multLocal(max);
        }
        return this;
    }

If you want to do that, as you have already called length() once, you should perhaps rather choose:

multLocal(max/length), which should be a lot less expensive than normalizeLocal().

So:


    public static Vector3f truncate(float max) {
        float length = length();
        if (length > max) {
            multLocal(max/length);
        }
        return this;
    }


Just a thought on performance. I have no idea if/why/why not this should be added to source, however.

Thanks for the tip!  8)



As for usage, I currently use it in order to limit a steering force vector(calculated by combining

several steering behaviours) by a preset maximum force amount.



I just thought it could be of more general use, which at the moment I can't think of… :expressionless: