Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

cast problem

Started by derek7 Aug 7, 2006 at 2:29 AM 2 replies 1k views
Original Post
derek7
derek7
see the snippet Decal::Decal(const Vector3D& center, const Vector3D& normal, const Vector3D& tangent, float width, float height, float depth) { decalCenter = center; decalNormal = normal; Vector3D binormal = CrossProduct(normal, tangent); // Calculate boundary planes float d = DotProduct(center, tangent); ..................


inline float DotProduct(const Vector4D& p, const Vector3D& q)
{
	return .0f;
	//return (p.x * q.x + p.y * q.y + p.z * q.z + p.w);
}



#pragma once

#include "Vector3.h"


class Vector4D
{
public:

	float   x;
	float   y;
	float   z;
	float   w;

	Vector4D() {}

	Vector4D(const Vector3D& v, float u)
	{
		x = v.x;
		y = v.y;
		z = v.z;
		w = u;
	}

	Vector4D(float r, float s, float t, float u)
	{
		x = r;
		y = s;
		z = t;
		w = u;
	}

	Vector4D(const Vector3D& v)
	{
		x = v.x;
		y = v.y;
		z = v.z;
		w = 0.0F;
	}

	Vector4D(const Point3D& p)
	{
		x = p.x;
		y = p.y;
		z = p.z;
		w = 1.0F;
	}

	Vector4D(const Vector2D& v)
	{
		x = v.x;
		y = v.y;
		z = 0.0F;
		w = 0.0F;
	}

	Vector4D(const Point2D& p)
	{
		x = p.x;
		y = p.y;
		z = 0.0F;
		w = 1.0F;
	}

	Vector4D& Set(const Vector3D &v, float u)
	{
		x = v.x;
		y = v.y;
		z = v.z;
		w = u;
		return (*this);
	}

	Vector4D& Set(float r, float s, float t, float u)
	{
		x = r;
		y = s;
		z = t;
		w = u;
		return (*this);
	}

	float& operator [](long k)
	{
		return ((&x)[k]);
	}

	const float& operator [](long k) const
	{
		return ((&x)[k]);
	}

	Vector3D& GetVector3D(void)
	{
		return (*reinterpret_cast<Vector3D *>(this));
	}

	const Vector3D& GetVector3D(void) const
	{
		return (*reinterpret_cast<const Vector3D *>(this));
	}

	Point3D& GetPoint3D(void)
	{
		return (*reinterpret_cast<Point3D *>(this));
	}

	const Point3D& GetPoint3D(void) const
	{
		return (*reinterpret_cast<const Point3D *>(this));
	}

	Vector4D& operator =(const Vector3D& v)
	{
		x = v.x;
		y = v.y;
		z = v.z;
		w = 0.0F;
		return (*this);
	}

	Vector4D& operator =(const Point3D& p)
	{
		x = p.x;
		y = p.y;
		z = p.z;
		w = 1.0F;
		return (*this);
	}

	Vector4D& operator =(const Vector2D& v)
	{
		x = v.x;
		y = v.y;
		z = 0.0F;
		w = 0.0F;
		return (*this);
	}

	Vector4D& operator =(const Point2D& p)
	{
		x = p.x;
		y = p.y;
		z = 0.0F;
		w = 1.0F;
		return (*this);
	}

	Vector4D& operator +=(const Vector4D& v)
	{
		x += v.x;
		y += v.y;
		z += v.z;
		w += v.w;
		return (*this);
	}

	Vector4D& operator +=(const Vector3D& v)
	{
		x += v.x;
		y += v.y;
		z += v.z;
		return (*this);
	}

	Vector4D& operator +=(const Vector2D& v)
	{
		x += v.x;
		y += v.y;
		return (*this);
	}

	Vector4D& operator -=(const Vector4D& v)
	{
		x -= v.x;
		y -= v.y;
		z -= v.z;
		w -= v.w;
		return (*this);
	}

	Vector4D& operator -=(const Vector3D& v)
	{
		x -= v.x;
		y -= v.y;
		z -= v.z;
		return (*this);
	}

	Vector4D& operator -=(const Vector2D& v)
	{
		x -= v.x;
		y -= v.y;
		return (*this);
	}

	Vector4D& operator *=(float t)
	{
		x *= t;
		y *= t;
		z *= t;
		w *= t;
		return (*this);
	}

	Vector4D& operator /=(float t)
	{
		float f = 1.0F / t;
		x *= f;
		y *= f;
		z *= f;
		w *= f;
		return (*this);
	}

	Vector4D& operator &=(const Vector4D& v)
	{
		x *= v.x;
		y *= v.y;
		z *= v.z;
		w *= v.w;
		return (*this);
	}

	Vector4D operator -(void) const
	{
		return (Vector4D(-x, -y, -z, -w));
	}

	Vector4D operator +(const Vector4D& v) const
	{
		return (Vector4D(x + v.x, y + v.y, z + v.z, w + v.w));
	}

	Vector4D operator +(const Vector3D& v) const
	{
		return (Vector4D(x + v.x, y + v.y, z + v.z, w));
	}

	Vector4D operator +(const Vector2D& v) const
	{
		return (Vector4D(x + v.x, y + v.y, z, w));
	}

	Vector4D operator -(const Vector4D& v) const
	{
		return (Vector4D(x - v.x, y - v.y, z - v.z, w - v.w));
	}

	Vector4D operator -(const Vector3D& v) const
	{
		return (Vector4D(x - v.x, y - v.y, z - v.z, w));
	}

	Vector4D operator -(const Vector2D& v) const
	{
		return (Vector4D(x - v.x, y - v.y, z, w));
	}

	Vector4D operator *(float t) const
	{
		return (Vector4D(x * t, y * t, z * t, w * t));
	}

	Vector4D operator /(float t) const
	{
		float f = 1.0F / t;
		return (Vector4D(x * f, y * f, z * f, w * f));
	}

	float operator *(const Vector4D& v) const
	{
		return (x * v.x + y * v.y + z * v.z + w * v.w);
	}

	float operator *(const Vector3D& v) const
	{
		return (x * v.x + y * v.y + z * v.z);
	}

	float operator *(const Point3D& p) const
	{
		return (x * p.x + y * p.y + z * p.z + w);
	}

	float operator *(const Vector2D& v) const
	{
		return (x * v.x + y * v.y);
	}

	float operator *(const Point2D& p) const
	{
		return (x * p.x + y * p.y + w);
	}

	Vector3D operator %(const Vector3D& v) const
	{
		return (Vector3D(y * v.z - z * v.y, z * v.x - x * v.z,
			x * v.y - y * v.x));
	}

	Vector4D operator &(const Vector4D& v) const
	{
		return (Vector4D(x * v.x, y * v.y, z * v.z, w * v.w));
	}

	bool operator ==(const Vector4D& v) const
	{
		return ((x == v.x) && (y == v.y) && (z == v.z) && (w == v.w));
	}

	bool operator !=(const Vector4D& v) const
	{
		return ((x != v.x) || (y != v.y) || (z != v.z) || (w != v.w));
	}

	Vector4D& Normalize(void)
	{
		return (*this /= sqrtf(x * x + y * y + z * z + w * w));
	}

	Vector4D& RotateAboutX(float angle);
	Vector4D& RotateAboutY(float angle);
	Vector4D& RotateAboutZ(float angle);
	Vector4D& RotateAboutAxis(float angle, const Vector3D& axis);
};


inline Vector4D operator +(const Vector3D& v1, const Vector4D& v2)
{
	return (Vector4D(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v2.w));
}

inline Vector4D operator +(const Vector2D& v1, const Vector4D& v2)
{
	return (Vector4D(v1.x + v2.x, v1.y + v2.y,  v2.z, v2.w));
}

inline Vector4D operator -(const Vector3D& v1, const Vector4D& v2)
{
	return (Vector4D(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, -v2.w));
}

inline Vector4D operator -(const Vector2D& v1, const Vector4D& v2)
{
	return (Vector4D(v1.x - v2.x, v1.y - v2.y, -v2.z, -v2.w));
}

inline Vector4D operator *(float t, const Vector4D& v)
{
	return (Vector4D(t * v.x, t * v.y, t * v.z, t * v.w));
}

inline float operator *(const Vector3D& v1, const Vector4D& v2)
{
	return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
}

inline float operator *(const Point3D& v1, const Vector4D& v2)
{
	return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v2.w);
}

inline float operator *(const Vector2D& v1, const Vector4D& v2)
{
	return (v1.x * v2.x + v1.y * v2.y);
}

inline float operator *(const Point2D& v1, const Vector4D& v2)
{
	return (v1.x * v2.x + v1.y * v2.y + v2.w);
}

inline float Dot(const Vector4D& v1, const Vector4D& v2)
{
	return (v1 * v2);
}

inline float Magnitude(const Vector4D& v)
{
	return (sqrtf(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w));
}

inline float InverseMag(const Vector4D& v)
{
	return (1.0F / sqrtf(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w));
}

inline float SquaredMag(const Vector4D& v)
{
	return (v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
}









#pragma once
#include "Vector2.h"


class Vector3D
{
public:

	float   x;
	float   y;
	float   z;

	Vector3D() {}

	Vector3D(float r, float s, float t)
	{
		x = r;
		y = s;
		z = t;
	}

	Vector3D(const Vector2D& v, float u)
	{
		x = v.x;
		y = v.y;
		z = u;
	}

	Vector3D& Set(float r, float s, float t)
	{
		x = r;
		y = s;
		z = t;
		return (*this);
	}

	Vector3D& Set(const Vector2D& v, float u)
	{
		x = v.x;
		y = v.y;
		z = u;
		return (*this);
	}

	float& operator [](long k)
	{
		return ((&x)[k]);
	}

	const float& operator [](long k) const
	{
		return ((&x)[k]);
	}

	Vector3D& operator =(const Vector2D& v)
	{
		x = v.x;
		y = v.y;
		z = 0.0F;
		return (*this);
	}

	Vector3D& operator +=(const Vector3D& v)
	{
		x += v.x;
		y += v.y;
		z += v.z;
		return (*this);
	}

	Vector3D& operator +=(const Vector2D& v)
	{
		x += v.x;
		y += v.y;
		return (*this);
	}

	Vector3D& operator -=(const Vector3D& v)
	{
		x -= v.x;
		y -= v.y;
		z -= v.z;
		return (*this);
	}

	Vector3D& operator -=(const Vector2D& v)
	{
		x -= v.x;
		y -= v.y;
		return (*this);
	}

	Vector3D& operator *=(float t)
	{
		x *= t;
		y *= t;
		z *= t;
		return (*this);
	}

	Vector3D& operator /=(float t)
	{
		float f = 1.0F / t;
		x *= f;
		y *= f;
		z *= f;
		return (*this);
	}

	Vector3D& operator %=(const Vector3D& v)
	{
		float       r, s;

		r = y * v.z - z * v.y;
		s = z * v.x - x * v.z;
		z = x * v.y - y * v.x;
		x = r;
		y = s;

		return (*this);
	}

	Vector3D& operator &=(const Vector3D& v)
	{
		x *= v.x;
		y *= v.y;
		z *= v.z;
		return (*this);
	}

	Vector3D operator -(void) const
	{
		return (Vector3D(-x, -y, -z));
	}

	Vector3D operator +(const Vector3D& v) const
	{
		return (Vector3D(x + v.x, y + v.y, z + v.z));
	}

	Vector3D operator +(const Vector2D& v) const
	{
		return (Vector3D(x + v.x, y + v.y, z));
	}

	Vector3D operator -(const Vector3D& v) const
	{
		return (Vector3D(x - v.x, y - v.y, z - v.z));
	}

	Vector3D operator -(const Vector2D& v) const
	{
		return (Vector3D(x - v.x, y - v.y, z));
	}

	Vector3D operator *(float t) const
	{
		return (Vector3D(x * t, y * t, z * t));
	}

	Vector3D operator /(float t) const
	{
		float f = 1.0F / t;
		return (Vector3D(x * f, y * f, z * f));
	}

	float operator *(const Vector3D& v) const
	{
		return (x * v.x + y * v.y + z * v.z);
	}

	float operator *(const Vector2D& v) const
	{
		return (x * v.x + y * v.y);
	}

	Vector3D operator %(const Vector3D& v) const
	{
		return (Vector3D(y * v.z - z * v.y, z * v.x - x * v.z,
			x * v.y - y * v.x));
	}

	Vector3D operator &(const Vector3D& v) const
	{
		return (Vector3D(x * v.x, y * v.y, z * v.z));
	}

	bool operator ==(const Vector3D& v) const
	{
		return ((x == v.x) && (y == v.y) && (z == v.z));
	}

	bool operator !=(const Vector3D& v) const
	{
		return ((x != v.x) || (y != v.y) || (z != v.z));
	}

	Vector3D& Normalize(void)
	{
		return (*this /= sqrtf(x * x + y * y + z * z));
	}

	Vector3D& RotateAboutX(float angle);
	Vector3D& RotateAboutY(float angle);
	Vector3D& RotateAboutZ(float angle);
	Vector3D& RotateAboutAxis(float angle, const Vector3D& axis);
	
float length() const // float length(Vector3D * this)      n.length() == length(&n)
	{
		return (sqrtf(x * x + y * y + z * z));
	}
};


class Point3D : public Vector3D
{
public:

	Point3D() {}

	Point3D(float r, float s, float t) : Vector3D(r, s, t) {}

	Point3D& operator =(const Vector3D& v)
	{
		x = v.x;
		y = v.y;
		z = v.z;
		return (*this);
	}

	Point3D& operator *=(float t)
	{
		x *= t;
		y *= t;
		z *= t;
		return (*this);
	}

	Point3D& operator /=(float t)
	{
		float f = 1.0F / t;
		x *= f;
		y *= f;
		z *= f;
		return (*this);
	}

	Point3D& operator &=(const Vector3D& v)
	{
		x *= v.x;
		y *= v.y;
		z *= v.z;
		return (*this);
	}

	Point3D operator -(void) const
	{
		return (Point3D(-x, -y, -z));
	}

	Point3D operator +(const Vector3D& v) const
	{
		return (Point3D(x + v.x, y + v.y, z + v.z));
	}

	Point3D operator -(const Vector3D& v) const
	{
		return (Point3D(x - v.x, y - v.y, z - v.z));
	}

	Vector3D operator -(const Point3D& p) const
	{
		return (Vector3D(x - p.x, y - p.y, z - p.z));
	}

	Point3D operator *(float t) const
	{
		return (Point3D(x * t, y * t, z * t));
	}

	Point3D operator /(float t) const
	{
		float f = 1.0F / t;
		return (Point3D(x * f, y * f, z * f));
	}

	Point3D operator &(const Vector3D& v) const
	{
		return (Point3D(x * v.x, y * v.y, z * v.z));
	}
};


inline Vector3D operator *(float t, const Vector3D& v)
{
	return (Vector3D(t * v.x, t * v.y, t * v.z));
}

inline Point3D operator *(float t, const Point3D& p)
{
	return (Point3D(t * p.x, t * p.y, t * p.z));
}

inline float Dot(const Vector3D& v1, const Vector3D& v2)
{
	return (v1 * v2);
}

inline Vector3D CrossProduct(const Vector3D& v1, const Vector3D& v2)
{
	return (v1 % v2);
}

inline float length(const Vector3D& v)
{
	return (sqrtf(v.x * v.x + v.y * v.y + v.z * v.z));
}

inline float InverseMag(const Vector3D& v)
{
	return (1.0F / sqrtf(v.x * v.x + v.y * v.y + v.z * v.z));
}

inline float SquaredMag(const Vector3D& v)
{
	return (v.x * v.x + v.y * v.y + v.z * v.z);
}


struct Origin3D_
{
	const Point3D& operator +(const Vector3D& v)
	{
		return (static_cast<const Point3D&>(v));
	}

	Point3D operator -(const Vector3D& v)
	{
		return (Point3D(-v.x, -v.y, -v.z));
	}
};


extern Origin3D_ Origin3D;


derek7
derek7
see the snippet

Decal::Decal(const Vector3D& center, const Vector3D& normal, const Vector3D& tangent, float width, float height, float depth)
{
decalCenter = center;
decalNormal = normal;



// Calculate boundary planes
float d = DotProduct(center, tangent);

question : center and normal is Vector3D and DotProduct()take a Vector4D for parameter. can Vector4D automaticly convert to Vector3D? compile say no error.
blarn
blarn
What's happening in the call

DotProduct( center, tanget )

is that 'center' is getting automatically converted into a Vector4D. This is possible because one of the constructors for Vector4D is

Vector4D( const Vector3D& v )
{
...
}

So the compiler knows how to make a Vector4D from a Vector3D. If you don't want this behavior, you have to declare the constructor with the 'explicit' keyword.

Topic Locked

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

Sign in to reply to this topic.