Skip to main content
gamedev.net
🔒 Locked

Camera class problem

Started by colinisinhere 0 replies 200+ views
Original Post
colinisinhere
colinisinhere
I writing a camera class based on the tutorial on NeHe... also found at http://www.gamedev.net/reference/articles/article1927.asp Here is my code for my vector class and for my set target function: Vector class:

template <class type> class NARCOTIK_API vector3d {
	public:
		//! Constructors
		vector3d() {
			x = 0, y = 0, z = 0;
		}

		vector3d(type X, type Y, type Z) {
			x = X, y = Y, z = Z;
		}

		vector3d(const vector3d<type>& other) {
			x = other.x, y = other.x, z = other.x;
		}

		//! Overload Operators
		vector3d<type>& operator=(const vector3d<type>& other) { x = other.x; y = other.y; z = other.z; return *this; }

		vector3d<type> operator+(const vector3d<type>& other) const { return vector3d<type>(x + other.x, y + other.y, z + other.z);	}
		vector3d<type>& operator+=(const vector3d<type>& other) { x += other.x; y += other.y; z += other.z; return *this; }

		vector3d<type> operator-(const vector3d<type>& other) const { return vector3d<type>(x - other.x, y - other.y, z - other.z);	}
		vector3d<type> operator-() const { return vector3d<type>(-x, -y, -z);	}
		vector3d<type>& operator-=(const vector3d<type>& other)	{ x -= other.x; y -= other.y; z -= other.z; return *this; }
		
		vector3d<type> operator*(const vector3d<type>& other) const { return vector3d<type>(x * other.x, y * other.y, z * other.z);	}
		vector3d<type>& operator*=(const vector3d<type>& other)	{ x *= other.x; y *= other.y; z *= other.z; return *this; }
		vector3d<type> operator*(const type v) const { return vector3d<type>(x * v, y * v, z * v);	}
		vector3d<type>& operator*=(const type v) { x *= v; y *= v; z *= v; return *this; }

		vector3d<type> operator/(const vector3d<type>& other) const { return vector3d<type>(x / other.x, y / other.y, z / other.z);	}
		vector3d<type>& operator/=(const vector3d<type>& other)	{ x /= other.x; y /= other.y; z /= other.z; return *this; }
		vector3d<type> operator/(const type v) const { type i=(type)1.0/v; return vector3d<type>(x * i, y * i, z * i);	}
		vector3d<type>& operator/=(const type v) { type i=(type)1.0/v; x *= i; y *= i; z *= i; return *this; }

		bool operator<=(const vector3d<type>&other) const { return x <= other.x && y <= other.y && z <= other.z;};
		bool operator>=(const vector3d<type>&other) const { return x >= other.x && y >= other.y && z >= other.z;};

		bool operator==(const vector3d<type>& other) const { return other.x == x && other.y == y && other.z == z; }
		bool operator!=(const vector3d<type>& other) const { return other.x != x || other.y != y || other.z != z; }

		//! Public Functions
		//! Returns the length
		float getLength() const { return sqrt(x*x + y*y + z*z); }

		//! Returns the dot product
		type dotProduct(const vector3d<type>& other) const { return x*other.x + y*other.y + z*other.z; }

		//! Returns the distance from a given vector
		float getDistanceFrom(const vector3d<type>& other) const {
			float vx = x - other.x; float vy = y - other.y; float vz = z - other.z;
			return sqrt(vx*vx + vy*vy + vz*vz);
		}

		//! Returns the cross product of 2 vectors
		vector3d<type> crossProduct(const vector3d<type>& p) const {
			return vector3d<type>(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x);
		}

		//! Normalizes the vector
		void normalize() {
			type inv = (type)1.0f / (type)getLength();
			x *= inv;
			y *= inv;
			z *= inv;
		}

		//! Public Members
		type x, y, z;
	};

	//! Type definitions to make it easier to use
	typedef vector3d<int> vector3di;
	typedef vector3d<float> vector3df;

setTarget function:

void CCamera::setTarget(vector3df newTarget) {
		vector3df projectedTarget;
	
		newTarget = newTarget - m_pos;
		projectedTarget = newTarget;
	        
		if(fabs(newTarget.x) < 0.00001f && fabs(newTarget.z) < 0.00001f) { // YZ plane
			projectedTarget.x = 0.0f;
			projectedTarget.normalize();

			m_right = vector3df(1.0f, 0.0f, 0.0f);
			m_up = projectedTarget.crossProduct(m_right);
	            
			m_target = newTarget;
			m_right = -m_target.crossProduct(m_up);
		}
	        
		else { // XZ plane
	            
			projectedTarget.y = 0.0f;
			projectedTarget.normalize();
	        
			m_up = vector3df(0.0f, 1.0f, 0.0f);
			m_right = -projectedTarget.crossProduct(m_up);
		
			m_target = newTarget;
			m_up = m_target.crossProduct(m_right);
		}
		
		m_target.normalize();
		m_right.normalize();
		m_up.normalize();
	}

So mater what I set the target too, it always ends up as 0.0f, 0.0f, 0.0f.... any idea why? thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.