Before looking at the source, let’s take a look at some of the fundamental mathematics behind the software.
rotations – Rotations in this software simple geometric transformations based around an unmoving center axis. Below are the three rotation matrices for each axis, X, Y, and Z, respectively. Every operation can be found in Transform.java below.
Rotate Around X,Y, or Z Axis
It is also important to know that every point in our world is defined by:
This can be seen in Point3D.java below. (Which is synonymous to a Vector in this application.)
Rotate Around Arbitrary Axis
The above rotations are all rotations about either the X,Y, or Z axises. But another common and more complicated rotation is to rotate Vector/Point A around Vector/Point B.
Here are the Steps:
1. the Vector that is being rotated around must be NORMALIZED.
this can be done very easily
2. next, using quaternions, perform the rotation.
( is in radians)
The rotation matrix evaluates to the following:
Example: Suppose we have point
and we would like to rotate by 30° around the X axis.
And finally, to project the 3D points onto a 2D canvas after performing a rotation, a simple way is to simply ignore the Z coordinate and draw the point based on it’s X and Y coordinates. However this is assuming that you’re projecting it on to the screen as if you are looking straight at it.
The source code can be found below as well as being bundled into the Jar file.
Cube 3D – A simple 3D Cube engine that demonstrates the usage of ALL the above mentioned equations.
Transform3D.java – This is a simple version using a 3×3 matrix. Using a 4×4 matrix you can also store the translation information. This is useful when trying to program a skeleton represented by vectors.
(Also, check out Graph/Rotate4D here
Other included source: