-
Notifications
You must be signed in to change notification settings - Fork 161
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 method for quats to set their direction #293
Comments
What would the method signature look like? Is there a method on |
Probably something like And you would get the direction vector by subtracting the 2 points, you would also need the current quaternion's direction vector and you can get that by just multiplying the quaternion by something like I also found this neat implementation which creates a quaternion to convert some vector direction into another: And after creating the quaternion, you would multiply it by the current one. |
Does https://docs.rs/glam/latest/glam/f32/struct.Quat.html#method.from_rotation_arc do what you want? |
That does work if I'm using vectors for directions, however I am using quaternions. Though there's a way to convert a vector into a quaternion, I think it would be better to just offer a method on quaternions to set their direction |
I think you will always need two vectors to determine what the rotation should be. |
I would also be interested in a look_at function. Something like this maybe? pub fn from_look_at(forward: Vec3, up: Vec3) -> Self {
let right = up.cross(forward).normalize();
let up = forward.cross(right);
Quat::from_mat3(&Mat3::from_cols(right, up, forward))
} |
Resurrecting this because I'd love to see something along these lines as well, and I'm happy to put up a PR if this is something that'll get accepted. Bevy has |
|
Ah, I hadn't thought of that, but that's an excellent reason to omit this function. Would you consider something like |
Yep I'd be OK with |
Alright, if nobody beats me to a pr, I'll have it up in the next couple days |
Popping in here to link this bevy pull request where I implemented a general version of this which looks like this: pub fn align(
&mut self,
handle: Vec3,
direction: Vec3,
weak_handle: Vec3,
weak_direction: Vec3,
) {
let handle = handle.try_normalize().unwrap_or(Vec3::X);
let direction = direction.try_normalize().unwrap_or(Vec3::X);
let weak_handle = weak_handle.try_normalize().unwrap_or(Vec3::Y);
let weak_direction = weak_direction.try_normalize().unwrap_or(Vec3::Y);
// The solution quaternion will be constructed in two steps.
// First, we start with a rotation that takes `handle` to `direction`.
let first_rotation = Quat::from_rotation_arc(handle, direction);
// Let's follow by rotating about the `direction` axis so that the image of `weak_handle`
// is taken to something that lies in the plane of `direction` and `weak_direction`. Since
// `direction` is fixed by this rotation, the first criterion is still satisfied.
let weak_image = first_rotation * weak_handle;
let weak_image_ortho = weak_image.reject_from_normalized(direction).try_normalize();
let weak_direction_ortho = weak_direction
.reject_from_normalized(direction)
.try_normalize();
// If one of the two weak vectors was parallel to `direction`, then we just do the first part
self.rotation = match (weak_image_ortho, weak_direction_ortho) {
(Some(weak_img_ortho), Some(weak_dir_ortho)) => {
let second_rotation = Quat::from_rotation_arc(weak_img_ortho, weak_dir_ortho);
second_rotation * first_rotation
}
_ => first_rotation,
};
} I would be a bit surprised if there wasn't a more expedient way to implement something equivalent, but I think the construction is at least pretty conceptually clear from this: If you want to basically take this (and perhaps improve it!) I would be happy to see it or something similar upstreamed into glam. |
I think it would be nice if quats had a method to make them look in a certain direction, for example if you have some direction vector and you want to make the quat look in that vector's direction, instead of making the user having to convert the vector into a rotation matrix and then into a matrix.
The text was updated successfully, but these errors were encountered: