<< Chapter < Page | Chapter >> Page > |
Within the method named drawOffScreen , the only really interesting code is the statement that calls the rotate method of the game-math library on each Point2D object inside a for loop. Knowing what you do about the rotate method, you should have no problem understanding the code in Listing 18 .
End of the discussion
That concludes the discussion of the program named StringArt02 . You will find a complete listing of this program in Listing 31 .
I saved the best for last, or at least I saved the most difficult program until last. In this program, I will teach you how to use the new GM01.Point3D.rotate method to rotate objects in 3D space.
Not only is the code for doing rotations in 3D space much more complicated than the rotation code in 2D, it is also more difficult to examine the graphicoutput produced by rotating an object in 3D space and be certain that the program is working as it should. Therefore, we need to start this discussionwith an explanation of the game-math library method named GM01.Point3D.rotate . Before we can get to that method, however, we must deal with the rotationequations for rotation of a point in 3D space.
The six 3D rotation equations
Unlike with 2D rotation where things were less complicated, we now have to deal with three coordinate values, three rotation angles, and six equations.Using the Spatial Transformations webpage and other online material as well, we can conclude that our 3D rotation method must implement the six equations shown in Figure 12 .
Figure 12 . The six 3D rotation equations. |
---|
Let rotation angle around z-axis be zAngle
Let rotation angle around z-axis be xAngleLet rotation angle around z-axis be yAngle
Rotation around the z-axisx2 = x1*cos(zAngle) - y1*sin(zAngle)
y2 = x1*sin(zAngle) + y1*cos(zAngle)Rotation around the x-axis
y2 = y1*cos(xAngle) - z1*sin(xAngle)z2 = y1*sin(xAngle) + z1* cos(xAngle)
Rotation around the y-axisx2 = x1*cos(yAngle) + z1*sin(yAngle)
z2 = -x1*sin(yAngle) + z1*cos(yAngle)Where:
x1, y1, and z1 are coordinates of original pointx2, y2, and z2 are coordinates of rotated point |
Also, as before, these six equations are only good for rotation around the origin, but our objective is to be able to rotate a point about anyarbitrary anchor point in 3D space. Once again, we will use the trick of translating the anchor point to the origin, rotating the object around theorigin, and then translating the object back to the anchor point.
Beginning of the method named GM01.Point3D.rotate
The method named GM01.Point3D.rotate begins in Listing 19 .
Listing 19 . Beginning of the method named GM01.Point3D.rotate. |
---|
public GM01.Point3D |
The purpose of this method is to rotate a point around a specified anchor point in 3D space in the following order:
A useful upgrade to the game-math library might be to write three separate rotation methods, each designed to rotate a Point3D object around only one of the three axes.
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?