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);
}
}
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.
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;
}
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:
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.
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.