Learn creative coding writing simple programs

63. Time for our first 3D animation

After many episodes of 2D graphics we find out how to draw things on 3D. It's actually very simple. We call the box() function to draw a 3D cube. We send just one parameter to box() to indicate the size of the box. To place the cube somewhere in the screen we use translate(). It's not the first time we use translate, but it is the first time we use it with three parameters. The third parameter indicates the depth, or how close to you or far away from you is the cube. This third parameter is called Z. If Z is positive, the object is closer to you. Making that value smaller, even negative, will bring the cube away from you.
Using the box() function will not work if you don't change the current renderer. By default, Processing wants to draw only 2D. To switch to 3D mode, you can add a third parameter when you call size, for example size(400, 400, P3D); The P3D part is telling Processing you want to do 3D graphics.
Once you get used to using translate() to move the cube around, try using rotateX(), rotateY() and rotateZ() to make the cube rotate around different axes.

Tags: 3D, software tutorial

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```