Skip to content
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

Closed

Conversation

pouleyKetchoupp
Copy link
Contributor

This change adds a few missing functions to Vector2 and Vector3 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.

@pouleyKetchoupp pouleyKetchoupp requested a review from a team as a code owner October 30, 2019 10:33
@akien-mga akien-mga added this to the 4.0 milestone Oct 30, 2019
@Zireael07
Copy link
Contributor

Yay, Vector3 finally gets clamped()! I had to basically copy same code over and over...

@Chaosus
Copy link
Member

Chaosus commented Oct 30, 2019

exposed to gdscript/gdnative

Also needs to be added to C#


return v;
}
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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! 😄

Copy link
Member

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.

@pouleyKetchoupp
Copy link
Contributor Author

Just added the new methods to C#, and the changes to optimize clamped() in both C++ & C# for Vector2 & Vector3.

@pouleyKetchoupp
Copy link
Contributor Author

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.

@aaronfranke
Copy link
Member

aaronfranke commented Nov 6, 2019

New method in Vector3 (from Vector2):
Vector3 Vector3::clamped(float length)

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.

@aaronfranke aaronfranke marked this pull request as draft August 26, 2021 00:54
@akien-mga
Copy link
Member

Seems like this was implemented already in 4.0.

The only missing method from the ones listed in OP is Vector2.inverse, could maybe be added in a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants