-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add a rotate_toward()
method to Vector2, Vector3, Quaternion and Basis
#7965
Comments
I am not able to get it past the finishing but here's a quick port godotengine/godot#82699 |
Please suggest better use cases for why this is not a few lines of script or is very common. |
As suggested by @kitbdev in chat.godotengine.org: Using okno, thinking more. I am wrong. |
In theory and However dividing by 10eps or even 100eps, could cause an outbound of floating points, I won't be sure if it is sufficient to avoid infinite results when both |
I would recommend making use of the following formula: Let I don't have a source for this formula, but it's easy enough to prove it, you just need to show that is perpendicular to There are probably cancelations you can apply to make the expression for |
Can you check this formula translated by machine? # Assuming v1 and v2 are Vector2 or Vector3 objects in Godot, and theta is the angle in radians
func rotate_vector(v1, v2, theta):
var v1_dot_v2 = v1.dot(v2)
var v1_mag_sq = v1.length_squared()
var v2_mag_sq = v2.length_squared()
var v3 = (v1_mag_sq * v2 - v1_dot_v2 * v1) / sqrt(v1_mag_sq * v2_mag_sq - v1_dot_v2 * v1_dot_v2)
var v = cos(theta) * v1 + sin(theta) * v3
return v |
It looks right enough to me. I'd recommend writing out a few unit tests. Not necessarily you fire, but perhaps @ettiSurreal would be up to the task. By the way, if you value numerical stability more than having an intuitive interpretation of the parameter that controls the magnitude of the rotation, you can also use the following formula. In this version of the formula, where As you can see, I don't really have any preference for which one gets implemented. It depends on whether this function will more likely be used in a context where you actually want to rotate by a specific number of radians or you just want to rotate some amount in the right direction. |
So like turn rotations? |
This is the formula for
|
Not really sure what you mean by that. |
It's not quite the formula for The formula I gave above is a bit black-boxy, so maybe I can explain a bit better where it's coming from. The idea is to express the rotated vector as a combination of the initial vector Really, the only tricky part is in how That said, I have no strong opinions about whether this formula goes in the engine or not. I need it for any project that I'm working on, so I don't really know how much more convenient a function like this compared to just |
Quaternion and Basis |
Quick idea from the quick port PR: |
I agree with the idea but the name can be a little misleading. Perhaps something like |
Yes i forgot to specify I was looking for a better name. |
define As one approach is changes by percentages and the other by deltas... may be better to merge both names as similar, like: LERP -> percentual change SLERP -> percentual change Or using a single one with a flag, |
We discussed in the animation meeting. We think is a good quality of life enhancement but looking for additional math reviewers like @TokageItLab or @aaronfranke Also needs rebasd |
rotate_toward
to Vector2, Vector3, Quaternion and Basis.rotate_toward()
method to Vector2, Vector3, Quaternion and Basis
Describe the project you are working on
Small platformer game.
Describe the problem or limitation you are having in your project
There is currently no built-in way to rotate Vectors, Quaternions and Basis by an increment.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Like the newly added GlobalScope
rotate_toward
is the equivalent oflerp_angle
, this would be the equivalent ofslerp
.The Vector implementation should also let you move toward the lengths/magnitudes of a vector, which the proposed implementation has implemented.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
With some help in the contributors chat we came to this implementation, just need to get someone to translate this to C++, because I cannot do that myself.
Quaternion
Basis
Vector2 and Vector3 (Same code should work for both, just need to swap the types)
If this enhancement will not be used often, can it be worked around with a few lines of script?
The functions can be recreated (or copied from here) as static functions. But some implementations of it I saw can cause a division by zero near the end.
Is there a reason why this should be core and not an add-on in the asset library?
To me the main use case of this would be looking at a target while rotating at a constant value, which would be useful for something like a turret or homing missile off the top of my head. I think these would share the use cases with
slerp
, with this being mostly preferential.The text was updated successfully, but these errors were encountered: