<< Chapter < Page Chapter >> Page >

A new ColMatrix3D object

Listing 5 creates a new ColMatrix3D object with the x and y-axes coordinate values matching the corresponding values for the user specifiedvector. It executes the expression shown in Listing 5 to compute the value of the z-axis coordinate that will cause the new vector to be perpendicular to theuser-specified vector. (The expression in Listing 5 matches the last equation in Figure 6 .)

A new Vector3D object

Then it uses the ColMatrix3D object described above to create, normalize, scale, and draw the first perpendicular vector. Following that, thecode negates the perpendicular vector to create another perpendicular vector that points in the opposite direction.

Along the way, some information is displayed on the command-line screen.

The most important code

The most important code in Listing 5 , insofar as the objective of the program is concerned, is the expression that computes the z-axis coordinate value thatwill cause the new vector to be perpendicular to the user-specified vector.

Remainder of the actionPerformed method

Listing 6 does essentially the same thing two more times to implement the other two equations shown in Figure 6 , creating and drawing four more perpendicular vectors in the process.

Listing 6 . Remainder of the actionPerformed method.
if(Math.abs(yCoor)>0.001){ tempMatrix = new GM02.ColMatrix3D(xCoor,-(xCoor*xCoor + zCoor*zCoor)/yCoor,zCoor); tempVec = new GM02.Vector3D(tempMatrix);System.out.println(tempVec); //Normalize and scale the perpendicular vector.tempVec = tempVec.normalize().scale(50.0); tempVec.draw(g2D,new GM02.Point3D(new GM02.ColMatrix3D(0,0,0))); tempVec.negate().draw(g2D,new GM02.Point3D(new GM02.ColMatrix3D(0,0,0))); System.out.println(vecA.angle(tempVec));}//end ifif(Math.abs(xCoor)>0.001){ tempMatrix = new GM02.ColMatrix3D(-(yCoor*yCoor + zCoor*zCoor)/xCoor, yCoor,zCoor); tempVec = new GM02.Vector3D(tempMatrix);System.out.println(tempVec); //Normalize and scale the perpendicular vector.tempVec = tempVec.normalize().scale(50.0); tempVec.draw(g2D,new GM02.Point3D(new GM02.ColMatrix3D(0,0,0))); tempVec.negate().draw(g2D,new GM02.Point3D(new GM02.ColMatrix3D(0,0,0))); System.out.println(vecA.angle(tempVec));}//end if myCanvas.repaint();//Copy off-screen image to canvas.System.out.println();//blank line }//end actionPerformed//----------------------------------------------------//

Listing 6 also causes the off-screen image to be displayed on the canvas.

That concludes the explanation of the program named DotProd3D06 .

The program named DotProd3D04

The purpose of this program is to serve as a counterpoint to the program named Prob3D03 , which demonstrates backface culling. (I will explain the program named Prob3D03 shortly.)

This program draws the 3D object shown in Figure 1 , while the program named DotProd3D03 draws the 3D object shown in Figure 2 . The difference between the two is that the object drawn by this program ( Figure 1 ) does not incorporate backface culling to hide the lines on the back of the object. The object in Figure 2 does incorporate backface culling. The difference is easy to see.

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