3D Rotation Matrix – Graph 3D

This was a simple program I wrote that Displays 3D points. It does so by simply projecting a 3D (vector) into 2D.
The Jar file can be downloaded here: Graph3D.jar
The C/C++ version can be found here

3D rotation matrix
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

rotX = delim{[}{matrix{3}{3}{1 0 0 0 {cos(theta)} {-sin(theta)} 0 {sin(theta)} {cos(theta)}}}{]} rotY = delim{[}{matrix{3}{3}{{cos(theta)} 0 {sin(theta)} 0 1 0 {-sin(theta)} 0 {cos(theta)}}}{]}
rotZ = delim{[}{matrix{3}{3}{{cos(theta)} {-sin(theta)} 0 {sin(theta)} {cos(theta)} 0 0 1 0}}{]}

It is also important to know that every point in our world is defined by:
vec{p } = delim{[}{matrix{3}{1}{x y z}}{ ]}
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

1a. d = sqrt(x^2 + y^2 + z^2) 1b. hat{p} = delim{[}{matrix{3}{1}{x/d y/d z/d}}{]}

2. next, using quaternions, perform the rotation.
2a. hat{p} = ai + bj + ck
q0 = cos(theta/2),  q1 = sin(theta/2) a,  q2 = sin(theta/2) b,  q3 = sin(theta/2) c (theta is in radians)
The rotation matrix evaluates to the following:
2b. rotAB = delim{[}{matrix{3}{3}{ (q0^2 + q1^2 - q2^2 - q3^2) {2(q1q2 - q0q3)} {2(q1q3 + q0q2)} {2(q2q1 + q0q3)} (q0^2 - q1^2 + q2^2 - q3^2)  {2(q2q3 - q0q1)}  {2(q3q1 - q0q2)} {2(q3q2 + q0q1)} {(q0^2 - q1^2 - q2^2 + q3^2)} }}{]}

Example: Suppose we have point vec{p} = delim{[}{matrix{3}{1}{1 2 3}}{]}
and we would like to rotate vec{p} by 30° around the X axis.
 vec{p}_{new} = (rotX)vec{p} = delim{[}{matrix{3}{3}{1 0 0 0 {cos(theta)} {-sin(theta)} 0 {sin(theta)} {cos(theta)}}}{]}.delim{[}{matrix{3}{1}{x y z}}{]} = delim{[}{matrix{3}{3}{1 0 0 0 {cos(30)} {-sin(30)} 0 {sin(30)} {cos(30)}}}{]}.delim{[}{matrix{3}{1}{1 2 3}}{]}

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:


You can leave a response, or trackback from your own site.

9 Responses to “3D Rotation Matrix – Graph 3D”

  1. Juromenha says:

    Thank you for sharing. I came to this site to read how things really are

  2. [...] 3D Rotation Matrix – Graph 3D [...]

  3. [...] 3D Rotation Matrix – Graph 3D [...]

  4. iceblink says:

    Thanks for this very helpful page.
    Please note that formula 2a is erroneous. The numbers on the matrix diagonal should all be squared, i.e. q0^2+q1^2-q2^2-q3^2 etc.
    I haven’t used the programming code so I have no idea if the code is okay.

  5. Kenny says:

    The code was correct, just my comments were wrong, and I wrote this blog based off my code comments, It has been updated. Thanks again.

  6. History Of Animation…

    [...]3D Rotation Matrix – Graph 3D | Ken-Soft[...]…

  7. Jack says:

    ttagg.com uses an invalid security certificate.

    The certificate is only valid for the following names:
    datarank.com , http://www.datarank.com

    (Error code: ssl_error_bad_cert_domain)

Leave a Reply

Powered by WordPress | Designed by: WordPress Themes | Thanks to best wordpress themes, Find WordPress Themes and Themes Directory