**0**

## Bug fixes... and a Screenshot Part Duex

Posted by

**ObsidianBlk**, in Tutorials and Explanations, Archetype Engine, Python 21 January 2011 · 420 viewsOpenGL LookAt Matrix Archetype Engine Python

**[BLURB]:**GOT YA! My camera works, finally! I fixed the rendering of the camera too, so it LOOKS like a camera. Nailed an interesting bug in my vector classes, and all is right with the world... oh yeah, another boring screen shot!

**[DEVELOPMENT: Archetype Engine]**

The blurb basically sums up the massive part of my work. Basically, the camera was screwing up on me. You have ANY idea how much of a PAIN it is to get the horizontal and vertical angles between two objects in 3D space?! Suuure... the arc tangent of the cross/dot products for each of the two planes the objects are sitting on... except one of my objects was sitting at the origin, and, the dot and cross products in that case BOTH come out to zero, so BOTH angles were coming out to zero.

Before anyone points it out, yes, I know that method is actually for two VECTORS not 3D coordinates (well, I know that now, anyway... sue me, my math is a little rusty), but even treating the vectors as two 3D points, I get one accurate angle but the other (the vertical angle), which should be ZERO, flip flops between zero and 180 depending on the quadrant the non-origin object is sitting. Technically, that flip-flopping is mathematically legit, but NOT useful at all!

I wanted to use these angles to generate rotations that I would then enter into a quaternion to generate a rotation matrix... lol, laughable, I know! Then, it hit me... how does gluLookAt generate it's matrix. Soooo... all hail the Google, I looked it up and found a simple formula to generate a rotational transform matrix given an eye, a target, and an up vector (HEY! That sounds like what gluLookAt wants!! Why yes, good sir or madam, it is, that's not the point, moving on please). I will post the method at the end of this entry just for those programmers green enough (as I am) to not know the method by heart, because I know the pain of looking for the answer and you seem sooo close and then the jack-a-mole website you find doesn't tell you!

Anyway, here's a new screenshot of the camera demo...

The demo actually works too! The redish objects are the cameras. Both cameras are set to look at the white cube in the middle. Pressing "W" or "S" will orbit camera A around the cube, while "A" and "D" will orbit camera B around the cube. Very fun... very exciting... will add more depth to this demo over time.

As always, anyone who's interested in downloading and working with the code can get their hands on it here at gitorious... I'm actively working on the development branch, so, if you're looking in the master branch and wondering where everything is, it's still all in the development branch

**[TUTORIAL:**

*"LookAt" 4x4 Rotation Matrix Method*]Assuming you have 3 3D vectors, eye, target, and up, where eye is the position of your camera, target is where you want your camera to look, and up to the upward vector of your world, then...

NOTE: up vector should be normalized

vec3d zaxis = normalize(target - eye)

vec3d xaxis = cross(up, zaxis)

vec3d yaxis = cross(zaxis, xaxis)

matrix = xaxis.x, yaxis.x, zaxis.x, 0, # Row1

xaxis.y, yaxis.y, zaxis.y, 0, # Row2

xaxis.z, yaxis.z, zaxis.z, 0, # Row3

0, 0, 0, 1 # Row4

NOTE: For the matrix above to work in OpenGL, the array containing the data is in column order...

Array Index: 1, 5, 9, 13, # Col1

2, 6, 10, 14, # Col2

3, 7, 11, 15, # Col3

4, 8, 12, 16 # Col4

Hope that's useful to someone. Forgive me if it's a little confusing, I'm tired and not thinking with all synapses.