#### Archived

This topic is now archived and is closed to further replies.

# Having Serious Problems With UVN Camera System

## Recommended Posts

Crazy_Vasey    100
I''ve called my vectors right, up, and direction instead of UVN because UVN didn''t really help me with remembering which was which when I was programming. Anyway, I''ve been trying to get this camera code working for the last few days, on and off, now, and it''s just not working for some reason. It''s probably going to be really obvious to someone who knows what they''re doing, but this is the first time I''ve coded a UVN camera system and I haven''t done a huge amount of 3D graphics before, so it''s kicking my ass a bit. The problem is the turning/looking up and down. I have this idea that to turn you rotate the right and direction vectors around the up vector, and that to look up and down you rotate the up and direction vectors around the right vector? Does that sound right? Anyway, that''s what I''ve done and it almost works. Almost. I thought you had to normalise the three vectors as well, but when I do that... well, it''s not pretty at all. It just goes apeshit on me. You''ll see what I mean if you look at the executable files I''ve uploaded. The code for the camera class is:
#include "camera.h"
#include "misc.h"

using namespace maths;

Camera::Camera(Vector initialPosition, Vector direction)
{
pos = initialPosition;
this->direction = direction;
up = Vector(0,1,0);
right = CrossProduct(direction,up);
up = CrossProduct(right,direction);
this->direction = GetNormalVector(this->direction);
up = GetNormalVector(up);
right = GetNormalVector(right);
}

Matrix Camera::toMatrix()
{
float m[16];
m[0] = right.x; m[4] = right.y; m[8] = right.z; m[12] = -pos.x;
m[1] = up.x; m[5] = up.y; m[9] = up.z; m[13] = -pos.y;
m[2] = direction.x; m[6] = direction.y; m[10] = direction.z; m[14] = -pos.z;
m[3] = 0; m[7] = 0; m[11] = 0; m[15] = 1;
return Matrix(m);
}

void Camera::translate(float x, float y, float z)
{
pos = pos + Vector(x, y, z);

}

void Camera::turn(float a)
{
direction = Quaternion(up,a).toMatrix() * direction;
right = Quaternion(up,a).toMatrix() * right;
}

void Camera::look(float a)
{
direction = Quaternion(right, a).toMatrix() * direction;
up = Quaternion(right, a).toMatrix() * up;
}

The executables are uploaded at: here UVN.exe is without the vectors being normalised and UVNnormalised.exe is when the vectors are normalised on each rotation. I would really appreciate help here. It''s beginning to frustrate me bigtime.

##### Share on other sites
DJSnow    100
go to gametutorials.com;
digiben has a cool tutorial/demoprogram on implementing such camera; you can easily adopt it; or try gamedev.net''s article
(but it''s not quaternion based)

DJSnow
---
this post is manually created and therefore legally valid without a signature

##### Share on other sites
Crazy_Vasey    100
Mine''s not quaternion based, it just uses them to generate an axis/angle rotation matrix.

Anyway, I''ll look at that tutorial. Thanks.

##### Share on other sites
Crazy_Vasey    100
I still haven''t got this working. If anyone could help me, I would be forever grateful.

##### Share on other sites
Yes, you have to normalize the vector you''re rotating around because the axis-angle and quaternion rotations both need an axis represented as a unit length vector. Here''s a link Math World. There are links on that page to quaternion and the matrix form of that formula. When I was getting my vector/matrix library to work, i ran it in debug mode w/ breakpoints so i could see the values of my variables. I did one [for you a rotation] that was easy to calculate by hand on paper and i knew, and i made sure everything went well as shown by the value window. I think the normalization you''re talking about, and have probably seen in tuts is to have all vectors normalized on every operation on them, so if you make the camera "look at" something with a few cross products, you''d normalize the resultant UVN vecs so that you wouldn''t have to when it comes to rotate them or do other ops on them that require a unit vec. I guess one could choose to do this as a prereq within the func that needs it or you could do it after every op that alters the length, it''s up to you. I hope i helped. Funny, i have to make MY own UVN camera class too, and you came across the same questions i had and yet have. Personally, when i''m testing something, i make a seperate app, a console app, so testing something is simple, and i test one or two classes at a time or even member functions at a time. Most important: sanity checks on paper!!

##### Share on other sites
Crazy_Vasey    100
I''ve already tested my Matrix and Quaternion classes with normal X/Y/Z axis rotations to check that they''re not producing bullshit results. So I''m fairly certain that the problem''s in the way I''m running my camera class (can''t be 100% sure as I''m not that experienced in 3D graphics yet).

Thing''s kicking my ass.

##### Share on other sites
sirSolarius    138

Vector maths::GetNormalVector(Vector v){    float mag = v.getMagnitude();    if(mag == 0) return v;    return Vector(v.x / mag, v.y / mag, v.z / mag);}