| WWH - Calculating a Rotation Matrix Based on Location/Target | |
|
WWH5: Calculating a Rotation Matrix Based on Location/Target
WhatThe purpose here is to describe how to obtain a proper matrix given a location and a target (or a vector), and an amount of roll. This document will also emulate the specific nature of 3DS cameras, when it comes to the degenerate case (input vector points directly up). The accompanying source code is in C++. WhyThis is useful when dealing with cameras in a 3D world, or when you need to orient an object based on a vector, rather than roll/pitch/yaw components. HowDefinecVector - dx, dy, dz cMatrix - 3x3 matrix class (consisting of 3 cVectors) The input Vector must be a directional vector. So for location->target, calculate like this: vector = target - camera; There is a problem with creating rotation matricies out of direction vectors. There is a degenerate case when the [delta y] of the direction vector is anything but zero. 3D Studio handles this in a special way, and here's a solution to it. TestingMany people claim to have perfectly working code, and I have found that in over 50% of them, this was not the case. To test this, simply view an object from all 6 directions (above, below, left, right, front, back). Make sure the view vector contains two 0 components and a 1 component (i.e. [0,0,1] or [0,-1,0]). The degenerate cases are [0,1,0] and [0,-1,0]. Pay special attention to the degenerate cases. This code based on the descriptions in _Computer Graphics Principles and Practice_ (page 222) by Foley, van Dam, Feiner and Hughes. Source
Discuss this article in the forums
See Also: © 1999-2009 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
|