-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added some functions to harmonize Vector2/Vector3 #33177
Conversation
Yay, Vector3 finally gets clamped()! I had to basically copy same code over and over... |
Also needs to be added to C# |
|
||
return v; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably be done more efficiently, e.g. (untested) :
Vector3 v = *this;
real_t sl = length_squared();
if (sl > 0)
{
real_t p_len_squared = p_len * p_len;
if (sl < p_len_squared)
{
real_t l = Math::sqrt(sl);
v *= (p_len / l);
}
}
return v;
You don't have to do the test against p_len_squared
, you can do the sqrt before that, depending on the expected data.
Noteworthy, that you can speed up the case where length is zero (by avoiding the sqrt), and you can combine the vector division and multiply.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! I'll update the one in Vector2 as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I better profile it first just in case hehe! 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it is an improvement, is 3x faster for me on some random test data.
0b4dcc3
to
d9cf888
Compare
Just added the new methods to C#, and the changes to optimize clamped() in both C++ & C# for Vector2 & Vector3. |
Just adding a note that api changes are probably going to break gdnative. The new functions have to be moved to the proper extension for 4.0 before merging. |
See the discussion in #13926 (comment). Don't add this until we decide what to do with this method, since we may be renaming it. Also see #30058 EDIT: Also, #34005 is somewhat of a subset of this PR, so I suppose it should be merged first. |
Seems like this was implemented already in 4.0. The only missing method from the ones listed in OP is |
This change adds a few missing functions to
Vector2
andVector3
to make their functionalities similar. The code has been also cleaned to follow the new coding rules.New methods in Vector2 (from Vector3):
Vector2 Vector2::inverse()
int Vector2::min_axis()
int Vector2::max_axis()
New method in Vector3 (from Vector2):
Vector3 Vector3::clamped(float length)
These new methods are exposed to gdscript/gdnative and the doc is updated.