Given a Transform
which appears to include rotation, is it possible to get a vector representing the direction that transform is facing, along with its relative up vector?
Thanks.
Given a Transform
which appears to include rotation, is it possible to get a vector representing the direction that transform is facing, along with its relative up vector?
Thanks.
I think you can use .isometry().inverse().rotation * direction
to transform from object space to its parentâ€™s space, which should get you what you want. For example, transform.isometry().inverse().rotation * Vector3::z()
for the forward-facing direction. Replace z with y for up. If you use parents and want to get the world space direction, you need to use GlobalTransform
which has some ordering issues. The GlobalTransform
is being removed in a future release: https://github.com/amethyst/amethyst/pull/1334
Given that the transformation is an isometry (i.e. a â€śtrueâ€ť linear rotation + a translation), which is enforced by the way that Transform is structured in Amethyst, the rotation matrix contained in the isometry is literally a transformation from the old X, Y, and Z basis vectors to the new new X, Y, and Z basis vectors. i.e. you can do transform.isometry().rotation.matrix().row(2)
to get the new Z (forward) basis vector and transform.isometry().rotation.matrix().row(1)
to get the new Y (up) basis vector. These vectors are relative to the vector space of the parent, as @kabergstrom said. To get the global, youâ€™d use GlobalTransform
which is just a single Matrix4
so itâ€™s slightly more complex since you just want the first 3 values in the row, i.e. global_transform.0.row(2)[0..3]
for the Z basis vector and global_transform.0.row(1)[0..3]
for the Y.
Thanks for the responses. Havenâ€™t had a chance to test them out yet but am hoping to soon.
Thanks, this seems to be working for me. I was just about to reuse this solution to calculate left/right/behind vectors and assume I can do something similar? I.e. the direction of local +X would be right, -X left, and -Z would be behind?
If so, since Iâ€™m using this more heavily, Iâ€™d like to use traits that arenâ€™t going away. Should I stay away from GlobalTransform for this, and instead use the first solution? I donâ€™t have any Parent
components, so I shouldnâ€™t need to convert local isometries to global ones I donâ€™t think.
Thanks for all the support Iâ€™ve gotten here thus far.
OK, sorry to keep bringing this back upâ€“I just want to make sure Iâ€™m not doing something wrong or writing bad code. After lots of study of these examples, and after lots of logging of my positions relative to the directions Iâ€™m traveling, I have this:
let mut direction = transform.isometry().rotation * Vector::z();
direction *= -1.;
info!("Direction: {:?}", direction.data);
Initially when I instantiate an object, I get:
[INFO][onslaught] Direction: [-0.0, -0.0, -1.0]
And this is correct. If I fly in the direction of travel, my Z coordinate does indeed decrease, and rotation seems to set these correctly. So, things Iâ€™m confused about:
let mut direction = transform.isometry().rotation.inverse() * Vector::z();
But the presence or absence of .inverse()
makes absolutely no difference here. Itâ€™s only by multiplying the rotation by -1 that I get a rotation in the correct direction. Iâ€™d have thought multiplying by -1 was an inverse. Am I misunderstanding why thatâ€™s there?
transform.isometry().rotation.matrix().row(2)
looks a bit cleaner, and avoids the vanishing GlobalTransform
. Unfortunately, thereâ€™s no .matrix()
method on the rotation. I got this to compile: let rotation = transform.isometry().rotation;
let mut direction = rotation * Vector::z();
direction *= -1.;
info!("Direction: {:?}", direction.data);
let direction2 = &rotation.coords.row(2);
info!("Direction2: {}, {}, {}", direction2[0], direction2[1], direction2[2]);
And I wanted to see if the values equalled, but I get a matrix index out of bounds error.
So in short, my code seems to work, but Iâ€™m not sure why. Calling .inverse()
on the rotation has no effect, and it seems like inverting a rotation should do something to the direction vector. The second example looks a bit cleaner, and Iâ€™d like to compare their results, but I canâ€™t make it work. Anyhow, I donâ€™t like code that I donâ€™t understand, and that a method named .inverse()
has no effect on, so here I am again.
Thanks.