How do I get facing/up vectors for a transform?


(Nolan) #1

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.


(Kae) #2

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


(Gray Olson) #3

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.


(Nolan) #4

Thanks for the responses. Haven’t had a chance to test them out yet but am hoping to soon.


(Nolan) #5

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.