# How do I ADD gravitation to a rotation matrix?

I have ported the Matlab script from https://instruct1.cit.cornell.edu/courses/bionb441/LSystem/ to POV-Ray and everything works fine (although I'm not quite sure why). To navigate a "turtle" through space, it utilizes a 3D rotation matrix (roll/pitch/yaw).

Now I would like to add a "tropism" command to simulate gravity pulling on the elements of an L-System, similar to how Laurens Laprés LParser does, and this is where I'm stuck.

This sounds trivial maybe, but I've been hitting walls for the past week, google and dissecting Lparser source code didn't help a bit:

What operation must be performed to "add" a gravity vector to the rotation matrix, iow: how do I easily rotate x degrees towards (0, -1, 0)?

---If it is of any help, this is a simplified example of how my script performs a YAW of dA radians. The columns of the matrix are stored in three vectors hT, lT, uT. For each, macro Rup(Vector, Angle) is called and returns the new vector (column of the matrix).

Corresponding macros Rlp & Rhp for pitch and roll exist, of course. vdot(A, B) is a POV Ray function and returns the dot product of A and B.

#if (cmdT = "+")
#declare hT=Rup(hT, dA);
#declare lT=Rup(lT, dA);
#declare uT=Rup(uT, dA);


#macro Rup(RotVec, Angle)
#local RotX=vdot(RotVec, < cos(Angle),  sin(Angle),      0>);
#local RotY=vdot(RotVec, <-sin(Angle),  cos(Angle),      0>);
#local RotZ=vdot(RotVec, < 0,           0,               1>);
<RotX, RotY, RotZ>
#end


It works perfectly, but I don't know why so I don't know what to do to add gravitation. Thanks in advance!

• You could try altering the angle based on the orientation of the current segment. If it's currently tilted to the right of vertical, tilt the next segment a bit farther right as if gravity were pulling on it, and vice versa. Sep 25, 2013 at 18:52
• Thank you! I have considered that, but only as a last resort as it involves a bunch of conditionals - as you say, IF tilted to the right, tilt a little more to the right, also pitch a little down if heading forward, BUT more up if belly-up and so on. I imagined there would be an easier, straightforward way to simulate a gravitational "pull" on the turtle's trajectory. Sep 26, 2013 at 8:03
• Well, I imagined coming up with a formula rather than a bunch of conditionals. The amount of extra tilt should vary continously with the current orientation. Sep 26, 2013 at 18:19
• Maybe I was being unclear. Here: math.stackexchange.com/questions/40808/… I learned how the gravity vector inside the object is computed, with the world gravity vector and the rotation matrix given; therefor I was under the impression that some similarly trivial approach would suffice to transform a rotation matrix "towards" a world vector, so to speak. Sep 27, 2013 at 8:56
• @Akareyon I would love to see this work as I do have an L-System, maybe you could create a separate matrix, and just use matrix multiplication? Other than that, I guess you must resort to some other iterative method. Jun 26, 2015 at 22:02