I'm undertaking a small project whereby I'm trying to recreate the educational programming language Logo.
For those who don't know it, Logo consists of a 'turtle' which starts at the (x, y) location (0, 0) and can then move forwards and backwards to draw lines. In addition, the turtle can turn to the right or left infinitely (I.E. the angle 'wraps' at 360 back to zero when turning clockwise, and has the same property in the counter-clockwise direction.)
Students can then use commands like FD to move the turtle forward, or RT to turn to the right. So, for example, the program FD 10 RT 90 FD 10 RT 90 FD 10 RT 90 FD 10 RT 90 would draw a square with sides of 10-units in length.
My turtle has x, y and angle properties, and when I supply d to specify the distance to travel, I can easily calculate the new values of x and y with the following:
old_x = x
old_y = y
r = angle * (pi / 180) # convert angle (in degrees) to radians
new_x = old_x + (d * cos(r))
new_y = old_y + (d * sin(r))
I then draw a line from (old_x, old_y) to (new_x, new_y). So far, so good. I'm able to move the turtle perfectly in two dimensions.
But, what I'd like to do now, is add UT and DT commands to my program, so that it is possible to move the turtle's nose up and down, so that we can now draw 3D shapes. For example, the program FD 10 UT 90 FD 10 UT 90 FD 10 UT 90 FD 10 UT 90 would still draw a square, only now it would standing vertically.
I know my graphics library (OpenGL) supports drawing lines in 3D, infact that's what I'm already doing, only I'm keeping the z dimension zero the whole time, and obviously I'll need to keep track of a second angle variable for up and down, but I've absolutely no idea of how to go about calculating the new x, y and z values given two angles.
Can anyone help? Many TIA.



You have to keep track of the orientation of the turtle nose in $3D$ using the two angles $\phi$ (horizontal) and $\theta$ (elevation angle from the horizontal)
The unit forward direction of the turtle is
$ N = \begin{bmatrix} \cos \theta \cos \phi \\ \cos \theta \sin \phi \\ \sin \theta \end{bmatrix} $
Let's assume that you start with the turtle in the $xy$ plane with its nose pointing in the positive $x$ axis direction, then the starting $\phi = 0$ and the starting $\theta = 0$
Using $RT \phi_1$ decrements $\phi$ by $\phi_1$ and $LT \phi_1$ increments $\phi$ by $\phi_1$. Similarly, $UT \theta_1$ increments $\theta$ by $\theta_1$ and $DT \theta_1$ decrements $\theta$ by $\theta_1$.
Then we moving forward, the new position is
$P_2 = P_1 + d N $