I need the Matrix4x4 to rotate around a given axis that does not go through (0,0,0). Is there some built in straightforward way to do this in the Unity libraries, or do I need to build and maintain my own?
Below is the formula I'd use to build my own in case somebody needs it:
We will define an arbitrary line by a point the line goes through and a direction vector. If the axis of rotation is given by two points P1 = (a,b,c) and P2 = (d,e,f), then a direction vector can be obtained by ⟨u,v,w⟩ = ⟨d-a,e-b,f -c⟩. [...] Assuming that ⟨u,v,w⟩ is a unit vector so that L = 1, we obtain [...]
I do need the rotation Matrix, not just the rotation. If I didn't need the Matrix I could use (not tested, may contain flaws, but you get the idea):
public Vector3 RotatePointAroundAxis(Vector3 point, Ray axis, float rotation) { return Quaternion.AngleAxis(rotation,axis.direction) * (point - axis.origin) + axis.origin; } So far the best I've got is the equivalent of the above (not tested, may contain flaws, but you get the idea), but it looks quite wasteful:
public Matrix4x4 RotationMatrixAroundAxis(Ray axis, float rotation) { return Matrix4x4.TRS(-Axis.origin, Quaternion.AngleAxis(rotation, Axis.direction), Vector3.one) * Matrix4x4.TRS(Axis.origin, Quaternion.identity, Vector3.one); } 