I’m using a 3D affine transform (a 4x4 matrix of floats) to represent a combination of translations and rotations in 3D space doing a sort of 3D turtle graphics kind of thing. From this state I know the heading that I’m going, and what I’d like to do is roll around this particular axis. The problem that I’m trying to solve is determining the angle (let’s call it θ) to roll that results in one of the heading vectors being as close to vertical (+Y) as possible while still on the plane of rotation that’s constrained to the “heading” axis.
My starting points for the “forward” heading is +1 on the Y axis, and a local “up” heading as +1 on the Z axis.
The way I was trying to tackle this problem was applying the built-up set of translations and rotations using the 3D Affine transform and then figuring out if there was trigonometry I could use to solve for the angle. Since I know the axis around which I want to roll (or can compute it by applying the transform to the unit vector that represents it - the (0,0,1) vector), I was looking at the Rodrigues rotational formula, but my linear algebra (matrix math) skills and understanding are fairly weak - and I couldn’t see a path to solving that equation for θ given known vectors for the heading, or vectors on the plane that can be used to define a cross-product that is the heading, as well as knowing the world vector.
I’m heading towards trying to solve this by iteratively applying various values of θ and homing in on the solution based on the the resulting value that has the Y component value. I can apply the roll as an affine transform that I multiply onto the current transform, and then test the result of a unit “up” vector - rinse and repeat to find the one that gives me the best “Y” component value.
I’d like to know If there’s a way to solve this directly - to compute the value of θ that I can use to directly do the rotation, rather than numerically iterate/solve into the solution.
If the background of "why" makes any difference, I'm implementing a 3D rendering of a Lindenmayer System similar to what's in The Algorithmic Beauty of Plants, and there's a specific roll rendering command that I'm trying to sort out: '$', which rolls about the axis of the current heading, so that the "up" vector is vertical.