c# - Extract forward vector from camera matrix -


scenario

i have scene contains several cameras, may facing direction. have no control on creation of scene or cameras, need work out direction cameras facing in order align them correctly.

the code below sample based on simple numbers, , think shows i'm missing fundamental.

i extracting forward vector camera matrix using

vector3 forward = new vector3(-worldtocamera.m13, -worldtocamera.m23, -worldtocamera.m33);

as specified @ http://roy-t.nl/2010/03/04/getting-the-left-forward-and-back-vectors-from-a-view-matrix-directly.html

however, given rotated input, returns (0, 0, -1). question is:

how forward direction in world space camera matrix?

code

using system; using system.numerics;  public class program {     private const double oneeightyoverpi = 180d / math.pi;     private const double twopi = math.pi * 2d;     private const double piovertwo = math.pi / 2d;     private const double threepiovertwo = 3d * math.pi / 2d;      public static void main()     {                vector3 cameraposition = new vector3(5, 5, 5);         matrix4x4 translate = matrix4x4.createtranslation(-cameraposition);          matrix4x4 cameramatrix = translate;          test(cameramatrix, cameraposition, 0);          // z-axis vertical, rotate around change pan angle of camera         cameramatrix = matrix4x4.createrotationz((float)piovertwo) * translate;               test(cameramatrix, cameraposition, 90);          cameramatrix = matrix4x4.createrotationz((float)math.pi) * translate;                test(cameramatrix, cameraposition, 180);          cameramatrix = matrix4x4.createrotationz((float)threepiovertwo) * translate;                 test(cameramatrix, cameraposition, 270);     }      private static void test(matrix4x4 worldtocamera, vector3 camerapositionworld, int expected) {           // http://roy-t.nl/2010/03/04/getting-the-left-forward-and-back-vectors-from-a-view-matrix-directly.html         vector3 forward = new vector3(-worldtocamera.m13, -worldtocamera.m23, -worldtocamera.m33);          // input aligned such that:         vector3 north = vector3.unity;          double angle = math.atan2(north.y, north.x) - math.atan2(forward.y, forward.x);           if (angle < 0) {             angle += twopi;         }          int deg = (int)(angle * oneeightyoverpi);          console.writeline("expected: " + expected + ", actual: " + deg + ", diff: " + (expected - deg));         } 


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -