<< Chapter < Page Chapter >> Page >

This is the only rotation possibility in 2D rotation and the code in Listing 21 is essentially the same as the code in Listing 16 for 2D rotation.

Rotate the Point 3D object around the x-axis

Before translating the partially rotated object back to the anchor point, it must still be rotated around the x and y-axes. Listing 22 implements the middle two equations in Figure 12 to rotate the Point3D object in a plane that is perpendicular to the x-axis, modifying only the y and z coordinate values.

Listing 22 . Rotate the Point3D object around the x-axis.
//Rotate around x-axis tempY = newPoint.getData(1);tempZ = newPoint.getData(2); newPoint.setData(//new y coordinate1, tempY*Math.cos(xAngle*Math.PI/180) -tempZ*Math.sin(xAngle*Math.PI/180)); newPoint.setData(//new z coordinate2, tempY*Math.sin(xAngle*Math.PI/180) +tempZ*Math.cos(xAngle*Math.PI/180));

Rotate the Point3D object around the y-axis

Listing 23 implements the last two equations in Figure 12 to rotate the Point3D object in a plane that is perpendicular to the y-axis, modifying only the x and z coordinate values.

Listing 23 . Rotate the Point3D object around the y-axis.
//Rotate around y-axis tempX = newPoint.getData(0);tempZ = newPoint.getData(2); newPoint.setData(//new x coordinate0, tempX*Math.cos(yAngle*Math.PI/180) +tempZ*Math.sin(yAngle*Math.PI/180)); newPoint.setData(//new z coordinate2, -tempX*Math.sin(yAngle*Math.PI/180) +tempZ*Math.cos(yAngle*Math.PI/180));

Translate the object back to the anchor point

Listing 24 translates the rotated object back to the anchor point, thus completing the 3D rotation of a single GM01.Point3D object.

Listing 24 . Translate the object back to the anchor point.
//Translate back to anchorPoint newPoint = newPoint.addVectorToPoint(tempVec);return newPoint;}//end rotate

In order to rotate an entire 3D geometric object, such as the hexagon in Figure 13 , every point that comprises the geometric object must be rotated using the same set of rotation angles and the same anchor point.

Now back to the program named StringArt03

This is a 3D version of a string art program that demonstrates rotation in three dimensions. This program produces a 3D string-art image by connectingvarious points that are equally spaced on the circumference of a circle. Initially, the circle is on the x-y plane centered on the origin as shown in Figure 13 .

Figure 13 Graphic output from the program named StringArt03 at startup.

Missing image.

At startup, there are six points (vertices) on the circle connected by lines forming a hexagon. The lines that connect the points are different colors.The radius of the circle is 50 units. The points at the vertices of the hexagonare not drawn, but the lines that connect the vertices are drawn.

You may have noticed that the startup graphic output in Figure 13 looks a lot like the startup graphic output of the 2D program in Figure 7 . There is a significant difference however. Figure 7 shows only two orthogonal axes whereas Figure 13 shows three orthogonal axes using oblique parallel projection to transform the 3D image to a 2D display plane.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask