Enhancement: new truncate / limit method for the Vector3f class

Hello everybody,
to improve the functionality of the engine, what do you think about adding in the next release, the truncate or limit method to the Vector3f class?

	/**
	 * truncate the length of the vector to the given limit
	 */
    public static Vector3f limit(Vector3f source, float limit) {
		if (source.lengthSquared() <= limit * limit) {
			return source;
		} else {
			return source.normalize().scaleAdd(limit, Vector3f.ZERO);
		}
	}
1 Like

In all of the cases I ever needed such a thing, I already had the length for other reasons. And it was extremely rare to need it. I can’t even imagine your use-case.

Also, super weird that it’s a static method instead of something that just operates on an existing vector.

1 Like

use-case:

https://p5js.org/examples/simulate-flocking.html

Edit 1:
the method name could be truncate or limit

	/**
	 * truncate the length of the vector to the given limit
	 */
    public Vector3f truncate(float limit) {
		if (lengthSquared() > limit * limit) {
			normalizeLocal().scaleAdd(limit, Vector3f.ZERO);
		}
        return this;
	}

Edit 2:
or this:

	/**
	 * truncate the length of the vector to the given limit
	 */
    public Vector3f limit(float limit) {
		if (lengthSquared() > limit * limit) {
			normalizeLocal().multLocal(limit);
		}
        return this;
	}
1 Like

I don’t know how often this would be useful to justify adding such a method to the engine, but, should it follow jME’s standards, we would end up with three methods:

  • public Vector3f limit(float lim)
  • public Vector3f limit(float lim, Vector3f result)
  • public Vector3f limitLocal(float lim)
1 Like

Here’s another use case:

I noticed that this function is recurrent in the steering algorithms and more generally in all the algorithms that involve the use of velocity and accelerations (but there could be many other situations).

I made the proposal to hear your ideas about it. If the community thinks never to use this functionality or prefers to do it in a custom class containing its own functions, (for this reason, initially my method was static) no problem, we can discard it.

1 Like

I think this is only true for certain idioms and it only comes up when “speed” is involved… else velocity and acceleration as vectors usually stand on their own. I’m not sure why I get away without this in my own steering but it is likely because I already keep direction and speed separate and reconstruct them as needed. In physics based steering (where you want things to be able to bump into you and push you around against your own forces), everything tends to be force based. So the desired velocity and actual velocity are separate and usually the magnitude of the desired velocity is already known.

I guess a physics engine could use it to limit max velocity of any object but it’s going to be case specific, I guess.

BTW, you can make it more efficient than what you’ve done so far.

public static Vector3f truncate( Vector3f v, float limit ) {
    float lengthSq = v.lengthSquared();
    if( lengthSq < limit * limit ) {
        return v;
    }
    return v.mult(limit/FastMath.sqrt(lengthSq));
} 

Thank you, I’ll do as you suggested. Thank you all for sharing your ideas. That’s fine for me, if you want we can consider the topic SOLVED.