by: cprogramming.com
Rotations in Three Dimensions Part Three: Rotation About an Arbitrary Axis Written by: Confuted, with a cameo bySilvercord (Charles Thibualt) The previous method of doing the rotationsis called usingEuler angles. It's probably the simplest way of doing rotations,but ithas some problems. The biggest problem is called gimballock. Youmay or may not have already encountered this if you wrote codeaccording to thelast tutorial. If you encountered it and noticed it, withoutknowing whatit was, you may have spent hours trying to figure out where you wentwrong inyour code, carefully comparing every line of your code to the tutorial,tryingto find the difference. If that happened, I'm sorry. Thereisnothing wrong with your code; there is something wrong with themath. Ifyou'll recall, I told you two very important things, which you probablydidn'tconnect in the last tutorial. 1) Matrix multiplication is notcommutative. A*B != B*A. 2) We generated matRotationTotalby doing matRotationX * matRotationY* matRotationZ. If therewas nothing wrong with the math, you should have been able to do matRotationY*matRotationZ*matRotationX,or any other order, and gotten exactly the same results. But youwouldn't. This problem is the root cause of gimbal lock. Trying tovisualize this might blow your mind, so if you don't understand thenextparagraph, don't worry too much. Just remember that Gimbal Lockhappenswhen one axis gets rotated before another axis, and the axes are nolongermutually perpendicular. It can be a large problem, or it can gounnoticed, depending on the application.
We multiplied our matrices in the ordermatRotationX * matRotationY *matRotationZ. It seemed to work, and for the most part, itdid. But if you think about it carefully, you'll realize that, as you movean objectin 3d space, all three axes change at once. They remain mutuallyperpendicular to one another. In the program, however, we'rerotating theobject over the X-axis first. That rotates the Y andZ-axes. Thenwe rotate over the Y axis, but since we've already rotated over theX-axis, therotation on the Y-axis only changes the location of the Z-axis. Therotation of the Z-axis does not change the location of either of theother twoaxes. Huge problem if you need to rotate on all three axes,because oneaxis can literally end up on top of another axis! (Just in themath. It can't do that in real life, meaning our representationis notaccurate)
Luckily for you, many math geniuses have dealt with this problem. Therewas a famous man named Euler, whom you'll hear mentioned inCalculus. Hedetermined that any series of rotations in three dimensional space canberepresented as a single rotation over an arbitrary axis. For thisrepresentation, called angle/axis representation, you'll need to storethearbitrary axis about which you are rotating, and the amount by whichyou arerotating.
Now for the cameo by Charles, who was kind enough to write thefollowingsection for me: Arbitraryaxis rotation by Charles Thibault
I am going to describe the calculations I perform in order to performrotationsabout an arbitrary axis. These calculations are NOT the matrixform ofthe rotations. Up to this point you know you can combine matricesinto asingle transformation. The single transformation matrix involvesabout 29multiplication operations and 9 addition operations, whereas completelyrotating a vector using my transformations (meaning calling myRotateVectorfunction TWICE, once over the Y axis then once over the Strafe vector)entailsabout ten percent more multiplications and about twice as many additionoperations (32 multiplications for two RotateVector calls, and 18additionoperations for two RotateVector calls).
How do you actually perform a rotation about an arbitrary axis? Wellfirstly you must understand rotations in two dimensions, because theconceptstays the same on an arbitrary plane. I'm going to make this asshort andsweet as possible. Instead of rotating the X and Y components ofavector, the X is really the component of the vector you are trying torotatePerpendicular to the vector that is the normal to the plane. Likewise theY is really the cross product between the vector you are trying torotate aboutand the actual vector being rotated.
Steps to rotate a vector: -Calculate the Perpendicular component, multiply it by the cosine ofthe angleyou are trying to rotate through -Calculate the cross product between the vector you are trying torotate aboutand the vector you are rotating, multiply it by the sine of the angle -Add the results together -Add the component of the vector you are trying to rotate that isparallel tothe vector you are rotating about
Note it is not totally necessary to calculate the parallel component ifthevector you are rotating and the vector you are rotating about arealreadyorthogonal. I do it in all cases anyway to avoid any mishaps andmakesure it is mathematically correct, but it seems to work bothways. Plus,by leaving it in the code you can rotate vector A about vector P evenif A andP are not orthogonal. (orthogonal means mutually perpendicular tooneanother) ContactSilvercord onCProgramming.com if you have questions about that section. Therest ofthis will, once again, be written by me (Confuted).
There's still the problem of performing the actual rotation about yourarbitrary axis. Luckily, this can also be done with amatrix. Again, I'm not going to derive it, I'm going to spoon feed it toyou. You can thank me later. | LeftHanded * | RightHanded * | | tX2 + c | tXY - sZ | tXZ + sY | 0 | | tXY+SZ | tY2 + c | tYZ - sX | 0 | | tXZ - sY | tYZ + sX | tZ2 + c | 0 | | 0 | 0 | 0 | 1 | | | X2 + c | tXY + sZ | tXZ - sY | 0 | | tXY-sZ | tY2 + c | tYZ + sX | 0 | | tXY + sY | tYZ - sX | tZ2 + c | 0 | | 0 | 0 | 0 | 1 | | Wherec = cos (theta), s = sin (theta), t = 1-cos (theta), and <X,Y,Z>is theunit vector representing the arbitary axis Now, you can replace matRotationTotal with thismatrix, and completely eliminate matRotationX, matRotationY, and matRotationZ. Of course, there is extra math involved elsewhere. But by usingtheaxis/angle representation for your rotations, it is possible to avoidgimballock. However, it's also possible to still suffer from it, if youdosomething incorrectly. In the next tutorial, I'll talk about someof theuses for the things I've been saying, and after that, brace yourselffor theexciting and strange world of quaternions. If you don't have aheadachefrom thinking too much yet, you probably will after the quaternions.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Web Development Articles More By Developer Shed developerWorks - FREE Tools! | <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
| | | | Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
| | | | Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs). FREE! Go There Now!
| | | | Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. FREE! Go There Now!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
| | | | WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |