Maths problem

Started by
9 comments, last by Lord_Evil 15 years, 10 months ago
I'm sure this is really simple but I just can't seem to find what the equations are for the 3d versions...

inline double PointDirection(double x, double y)
{
	double dir = RadToDeg(atan2(y, x));
	//ranges from -180 to 180 so we make it 0 to 360 (-180 == 180)
	if (dir < 0) return dir + 360;
	else return dir;
}
inline double LengthDirX(double Length, double Dir)//get X component
{
	return  cos(DegToRad(Dir))*Length;
}
inline double LengthDirY(double Length, double Dir)//y
{
	return -sin(DegToRad(Dir))*Length;
}

Now for 3d Ive got 3 axis that things can be roated on, However only 2 apply for the direction between points (pitch & yaw). Problem i I can't find the maths for them even though I'm pretty certain there as simple as the 2d ones :(

inline double PointDirectionX(double x, double y, double z);
inline double PointDirectionY(double x, double y, double z);
inline double LengthDirX(double Length, double DirX, double DirY);
inline double LengthDirY(double Length, double DirX, double DirY);
inline double LengthDirZ(double Length, double DirX, double DirY);

Advertisement
You're probably looking for 'spherical coordinates' (using spherical coordinates you can map a pair of angles to a point on the unit sphere, much like a single angle can be mapped to a point on the unit circle in 2-d).

As an aside, I'd recommend organizing the code you posted differently. For example, you might wrap it all up in a 2-d vector class and a 3-d vector class (note that it's really vectors you're working with here, not points).
Actauly I already have, but there the functions/maths behind some of my Vector3 class, but Ive only found detailed infomation on the maths for 2d vectors, not these operations for a 3d vector :(

class Vector2{...};class Vector3{	public:	double x,y,z;	Vector3();	Vector3(double Right[3]);	Vector3(double x, double y, double z);	Vector3(const Vector3 &Right);	Vector3& operator + (const Vector3 &Right) const;	Vector3& operator - (const Vector3 &Right) const;	Vector3& operator * (const double Right) const;	Vector3& operator =  (const double Right[2]);	Vector3& operator =  (const Vector3 &Right);	Vector3& operator += (const Vector3 &Right);	Vector3& operator -= (const Vector3 &Right);	Vector3& operator *= (const double Right);	bool     operator == (const Vector3 &Right) const;	bool     operator != (const Vector3 &Right) const;//need the maths for the functions from here:	void   SetMagDir(double Mag, double DirX, double DirY);//Pitch, Yaw	double GetMagnitude() const;	void   SetMagnitude(double Mag);	void   AddMagnitude(double Mag);	void  TakeMagnitude(double Mag);	//Vectors can not have a DirZ(roll) value	double GetDirX() const;	double GetDirY() const;	void   SetDirection(double DirX, double DirY);//to here....	void   Normalise();	double Dot(const Vector3 &Other) const;	Vector3 Cross(const Vector3 &Other) const;};
If you're actually after spherical co-ordinates, look e.g. at mathworld or wikipedia if you like.

BTW: What is TakeMagnitude(double Mag) good for? Is there perhaps a & being missed?
Because It's shorter to write than:

v->SetMagnitude(v->GetMagnitude() - 50);

might have some use although I'm generaly using vector additions right now...
There's no need for both an AddMagnitude and TakeMagnitude function. Adding a negative number is the same as subtracting a positive number.

That said, is directly altering the magnitude really what you want to do? Surely you just want to scale the vector, as provided for by your overload of operator* to take a double. I'm not sure I 'get' any of those (...)Magnitude functions, actually.
[TheUnbeliever]
Quote:look e.g. at mathworld or wikipedia if you like.

I really can't make much sense of either of those :(

 Y        Z |       / |      / |     / |    / |   / |  / | / |/__________X

So I can have roation around the 3 axis. But for a line only 2 of them have any meaning (X and Y). But I don't know how to get between them and an xyz vector format :(

- Given the angles and a speed, I need to know, starting from (0,0,0) where that "line" ends (The LengthDir* functions)
- Given an x,y,z cordinate I need to know the angles to it from (0,0,0) (the PointDirection* functions)
Well, look again, e.g. into the section "Cartesian coordinate system" at wikipedia. There are the correspondences between cartesian co-ordinates (x, y, z) on the one side and 2 angles (theta, phi) plus a magnitude (r) on the other. The same appears on mathworld in the equations (1) to (3) and (4) to (6), resp. That is AFAIK exactly what you want.

BTW: Are you sure you need spherical co-ordinates for your vector class? Most often, when such problem was requested here at GDnet, after some discussion the OPs drop spherical co-ordinate and work totally in right-angled co-ordinates.
How else am I supposed to represent angles for movement and just how an object is orientated in the world other than as rotation on the 3 axis?
Quote:How else am I supposed to represent angles for movement and just how an object is orientated in the world other than as rotation on the 3 axis?
A set of two (or three) angles is not the only way to represent an orientation in 3-d. In fact, it's quite often a poor way to represent an orientation.

Before trying to offer further advice though, let me ask: what sort of objects are you wanting to simulate here? Humanoids? Spaceships? Rigid bodies in a physics simulation? Projectiles? Ground-based vehicles?

There are a number of ways to represent orientation in 3-d, including a pair of angles (which can represent a subset of possible orientations), three angles, a quaternion, an axis-angle pair, a pair of basis vectors, three basis vectors, a 3x3 matrix, etc. Which of these will work best as your primary representation depends on the circumstances, which is why some more information would be useful.

This topic is closed to new replies.

Advertisement