<< Chapter < Page Chapter >> Page >

It is very important to understand that the order of the rotations is critical. You cannot change the order of rotations and expect to end up with thesame results. This method is designed to allow you to rotate an object around all three axes with a single method call in the order given above. If you needto rotate your object in some different order, you should call the method up to three times in succession, rotating around only one axis with each call to themethod.

Incoming parameters

The anchor point is passed in as a reference to an object of the GM01.Point3D class.

The rotation angles are passed in as double values in degrees (based on the right-hand rule ) in the order given above, packaged in an object of the class GM01.ColMatrix3D .

(Note that in this case, the ColMatrix3D object is simply a convenient container for the three double angle values and it has no significance from a matrix arithmetic viewpoint. Also payattention to the order of the three values and the rotation axes associated with those values. See Listing 20 . It is z, x, y, and not x, y, z as you might assume.)

The right-hand rule

The right-hand rule states that if you point the thumb of your right hand in the positive direction of an axis and curl your fingers to make a fist, thedirection of positive rotation around that axis is given by the direction that your fingers will be pointing.

I will refer back to this rule later when describing rotations around various axes.

Original object is not modified

This method does not modify the contents of the Point3D object on which the method is called. Rather, it uses the contents of that object toinstantiate, rotate, and return a new Point3D object.

Rotation around the anchor point

For simplicity, this method translates the anchor point to the origin, rotates around the origin, and then translates the object back to the anchorpoint.

Very familiar code

The code in Listing 19 is very similar to the code that I explained earlier beginning in Listing 14 . Therefore, this code should not require an explanation beyond the embedded comments.

Get the rotation angle values

Listing 20 extracts the rotation angle values from the GM01.ColMatrix3D object in which they are contained.

Listing 20 . Get the rotation angle values.
double zAngle = angles.getData(0); double xAngle = angles.getData(1);double yAngle = angles.getData(2);

Rotate the Point3D object around the z-axis

By this point in the execution of the method, the object has been translated to the origin using the negative of the anchor-point displacement vector. Theobject will be rotated around the origin and will then be translated back to the anchor point.

Listing 21 implements the first two equations from Figure 12 to rotate the Point3D object around the z-axis. By this, I mean that the object is rotated in a plane that is perpendicular to the z-axis modifying only x and y coordinatevalues from the object being rotated.

Listing 21 . Rotate the Point3D object around the z-axis.
//Rotate around z-axis tempX = newPoint.getData(0);tempY = newPoint.getData(1); newPoint.setData(//new x coordinate0, tempX*Math.cos(zAngle*Math.PI/180) -tempY*Math.sin(zAngle*Math.PI/180)); newPoint.setData(//new y coordinate1, tempX*Math.sin(zAngle*Math.PI/180) +tempY*Math.cos(zAngle*Math.PI/180));

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