What you are doing here is moving the world around a static camera. The better way is to move the camera then draw your objects.
The trick here is to use the
camera() function at the beginning of your draw method. This method has 9 parameters which might seem a bit daunting but are easy to understand.
camera(
eyeX,
eyeY,
eyeZ,
centerX,
centerY,
centerZ,
upX,
upY,
upZ)
eyeX,
eyeY,
eyeZ represents the position of the camera and
centerX,
centerY,
centerZ represents the location the camera is looking at and
upX,
upY,
upZ are the camera's up direction and are normally 0,1,0
To rotate the camera you calculate new values for
centerX,
centerY,
centerZ
To move the camera you calculate new values for
eyeX,
eyeY, eyeZ
To move the camera without introducing unwanted rotations due to the movement then changes in the position of the camera should also be applied to
eye and
center X/Y/Z values. If you only change eye then movement will introduce some unwanted rotation.
In draw()
- calculate the new values for the eye and center values based on key presses and mouse position
- use the camera() method to position the camera
- pushMatrix
- then translate to the box position say bx, by, bz
- use fill stroke etc to colour the box
- call box(size_of_box)
- popMatrix
- repeat setps 3-7 for each box you want
It may take a while to get this right but the effort is well worth it if you want to program 3D