A.2 Turtle and 3D

>From version 0.9.92, our turtle can leave its plane and move into 3D space. To switch to this mode, we use the primitive perspective. Welcome to a 3D world!

A.2.1 The perspective projection

To represent a 3D space on a 2D plane, XLOGO uses a projection perspective. A camera looks at the 3D scene, where the image from the projection screen is displaying. Here is a little scheme to explain this:


Some primitives allow us to set the camera position. The screen projection is half the distance from the camera.

A.2.2 Understanding orientation in a 3D World

In a 2D plane, the turtle’s orientation was only defined by its heading. In a 3D world, the turtle’s orientation is given by 3 angles:

In fact, to move itself in the 3D World, the turtle is very similar to an aircraft. Here is a little illustration which represents these 3 values:

PIC Roll

PIC Pitch

PIC Heading

It seems quite complex at first, but you will see that a lot of things stay very similar to moving in a 2D plane. Here are the basic primitives for moving in the 3D world:

forward, fd, back, bk n

Same behaviour as in 2D plane.

right, rt, left, lt n

Same behaviour as in 2D plane.

rr, rightroll n

The turtle turns n degrees to the right around its longitudinal axis.

lr, leftroll n

The turtle turns n degrees to the left around its longitudinal axis.

up, uppitch n

The turtle goes n degrees up around its transversal axis.

down, downpitch n

The turtle goes n degrees down around its transversal axis.

In the 2D plane, when we want to draw a square of side 200 steps, we write:
repeat 4[fd 200 rt 90]

These instructions are still available in the 3D world, where the square is drawn in perspective mode. If the turtle goes down 90 degrees, we can draw another square and we obtain:

repeat 4[fd 200 rt 90]  
down 90  
repeat 4[fd 200 rt 90]


You just have to try some examples to understand these orientations and become an expert!
You must understand that the 3 rotation primitives are linked together, for example try this:

leftroll 90 up 90 rightroll 90


The turtles movement is equivalent to left 90 (You can try with your hand simulating the turtle if you don’t understand)

A.2.3 Primitives available in 2D mode and 3D mode

The following primitives are available in 2D plane or in 3D world. The only difference is the arguments received by the primitives. For example, the primitive setpos or setposition is still waiting for a list as an argument but now, the list must contain three numbers (x;y;z) which represent the three point coordinates. Here are all those primitives:

circle arc home towards

distance setpos, setposition setx sety

setheading label labellength dot

pos, position heading

Primitives only available in 3D mode

setxyz x y z

This primitive moves the turtle to the chosen point. This primitive is waiting for three arguments representing the point’s coordinates. setxyz is very similar to setpos but the coordinates are not written into a list.
Example, setxyz -100 200 50: move the turtle to the point x = -100;y = 200;z = 50

setz z

This primitive moves the turtle to the point with the valid value z. setz is waiting for one number as an argument. This primitive is comparable to setx or sety.

setorientation list

Set the turtle’s orientation. This primitive waits for a list which contains 3 numbers, the roll, the pitch and the heading.
Example, setorientation [100 0 58]: the turtle has roll: 100 degrees, pitch: 0 degree and heading: 58 degrees.


Returns the turtle’s orientation in a list which contains: [ roll pitch heading ]. Note the number order, if for example, the orientation value is [100 20 90], this means that if you want the same orientation starting from the origin position (after a clearscreen instruction), you’ll have to write the following sequence:

rightroll 100 up 20 right 90

If you inverse the instruction’s order, you don’t obtain the valid orientation!

setroll n

The turtle turns around its longitudinal axis to the chosen roll angle.


Returns the current roll value.

setpitch n

The turtle turns around its transversal axis to the chosen pitch angle.


Returns the current pitch value.

A.2.4 3D Viewer

A 3D Viewer is included in XLogo, it allows you to visualize your drawing in 3D. This module uses the JAVA3D library, so it’s necessary to have java3D fully installed.

Here are the rules to use the 3D Viewer:
When we create a geometric figure on the drawing area, we have to indicate to the 3D Viewer which shapes we want to record for future visualization. It’s possible to record polygons (surfaces), lines, points or text. To use this feature, here are the primitives:


The following turtle’s moves are saved to create a polygon.


Since the last polystart call, the turtle has gone through several vertices. This new polygon is recorded, its color is defined by all vertices color. This primitive finalizes the polygon.


The following turtle’s moves are saved to create a strip line.


Since the last linestart call, the turtle has gone through several vertices. This new line is recorded, its color is defined by all vertices color. This primitive finalizes the strip line.


The following turtle’s moves are saved to create a point set.


This primitive finalizes the point set.


Each time the user displays text on the drawing area with the primitive label, it will be recorded and then displayed by the 3D Viewer.


End of text recording.

view3d polyview

Launch the 3D viewer, all recorded objects are drawn on this new window. You have control of the camera scene:

A.2.5 Drawing a cube

All faces are 400 steps square. Here is the program:

to square  
# we record the vertice square  
polystart repeat 4[forward 400 right 90] polyend  
to simpleCube  
# yellow cube  
clearscreen perspective setpencolor yellow  
# lateral faces  
repeat 4[square penup right 90 forward 400 left 90 rightroll 90 pendown]  
# bottom face  
downpitch 90 square uppitch 90  
# upper face  
forward 400 downpitch 90 square  
# visualization  

We launch with the command: simpleCube:


When we replace in the procedure square, polystart with linestart and polyend with lineend


If we had used pointstart and pointend instead of linestart and lineend, we would see on screen only the eight cube vertices. These primitives are very useful to display the point set in 3D Space.

A.2.6 Lighting the scene

You can specify four lights in your 3D scene. By default, the main 3D scene has only two ponctual lights enabled. Click on one of the 4 button lights in the 3D modeler, and this dialog box appears:


Several light type are available:

The best thing is to play with those lights to understand how they work!

A.2.7 Fog effect

You can add a fog effect on the main 3d scene. Click on the cloud button in the 3D scene and this dialog box appears.


Two fogs are available:

Example with a progressive fog: