Jump to content

  • Log In with Google      Sign In   
  • Create Account


rotating a point on a 3D Plane... help!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Protricity   Members   -  Reputation: 122

Like
Likes
Like

Posted 03 October 2001 - 09:05 AM

Im doing my first 3D game now, so I dont know how to calculate rotation yet. I know how to do rotation on a 2D plane, thats easy. So, basically, I have a point (ex. (1.0f,2.0f,1.5f) ) and I need to rotate it according to 3 Vector angles. ( ex. Xrotate = 45.0f, Yrotate = 35.0f, Zrotate = 25.0f) (0.0f,0.0f,0.0f) is the origin I need to rotate around. so, I need to make an algorithm for some method along the lines of Rotate( &X, &Y, &Z, Xrot, Yrot, Zrot) // (All are floats.) { } like I said, X,Y,Z is the point coordinates, and Xrot, Yrot, Zrot are the angles (from 0.0f to 360.0f) Using this information, how to I come up with a function that takes in the information and outputs the new point which has been rotated? I dont know if this is an advanced question, or its common knowledge, but if anyone can help me solve it Id very much apreciate it! thx, Protricity

Sponsor:

#2 Dark   Members   -  Reputation: 122

Like
Likes
Like

Posted 04 October 2001 - 12:06 AM

Hi


Well... I will just give you the basic operations.

Here is the header file



//---------------------------------------------------------------------------------------------
// Copyright © 2001 by Nucleus Software, Inc.
// All rights reserved.
//
// $File:: CVector3D.H
// $Dependencies:: none
// $Revision:: 1
// $Author:: Ebor Folkertsma
// $Date:: 07-15-2001 8:42pm
//
// $Description:: The CVector3D class offers some nice functionality for 3D vectors
//---------------------------------------------------------------------------------------------
#if _MSC_VER >= 1000
#pragma once
#endif

#ifndef _CVector3D_H_
#define _CVector3D_H_

#include

class CVector3D;
typedef CVector3D *LPCVector3D;

class CVector3D
{
public:

__inline CVector3D() {}
__inline ~CVector3D() {}

//---------------------------------------------------------------------------------------------
// CVector3D(const CVector3D& cVector3D)
//
// This is the initialize constructor, it enables you to specify startup values for the vector
//---------------------------------------------------------------------------------------------
__inline CVector3D(const CVector3D& cVector3D)
{
fX = cVector3D.fX;
fY = cVector3D.fY;
fZ = cVector3D.fZ;
}




//---------------------------------------------------------------------------------------------
// CVector3D(const float& fNewX, const float& fNewY, const float& fNewZ)
//
// This is the initialize constructor, it enables you to specify startup values for the vector
//---------------------------------------------------------------------------------------------
__inline CVector3D(const float &fNewX, const float &fNewY, const float &fNewZ)
{
fX = fNewX;
fY = fNewY;
fZ = fNewZ;
}




//---------------------------------------------------------------------------------------------
// CVector3D operator !(const CVector3D& cVector3D) const
//
// Reverses the vector
//---------------------------------------------------------------------------------------------
__inline CVector3D operator !() const
{
return (*this * -1.0f);
}




//---------------------------------------------------------------------------------------------
// bool operator != (const CVector3D& cVector3D) const
//
// This function tests wheter two CVector3D classes are different`
//---------------------------------------------------------------------------------------------
__inline bool operator != (const CVector3D& cVector3D) const
{
if((fX == cVector3D.fX) &&
(fY == cVector3D.fY) &&
(fZ == cVector3D.fZ))
{
return false;
}

return true;
}




//---------------------------------------------------------------------------------------------
// CVector3D operator * (const float& fValue) const
//
// This function multiplies the calling vector''s components by the give value
//---------------------------------------------------------------------------------------------
__inline CVector3D operator * (const float& fValue) const
{
return CVector3D(fX * fValue, fY * fValue, fZ * fValue);
}



//---------------------------------------------------------------------------------------------
// void operator *= (const float& fValue)
//
// This function multiplies the vector''s components by the give value
//---------------------------------------------------------------------------------------------
__inline void operator *= (const float& fValue)
{
fX *= fValue;
fY *= fValue;
fZ *= fValue;
}




//---------------------------------------------------------------------------------------------
// CVector3D operator + (const CVector3D& cVector3D) const
//
// This function adds this vector and the parameter vector and returns the result
//---------------------------------------------------------------------------------------------
__inline CVector3D operator + (const CVector3D& cVector3D) const
{
return CVector3D(fX + cVector3D.fX, fY + cVector3D.fY, fZ + cVector3D.fZ);
}




//---------------------------------------------------------------------------------------------
// void operator += (const CVector3D& cVector3D)
//
// This function adds this vector and the parameter vector and sets the calling vector''s value
// to the sum
//---------------------------------------------------------------------------------------------
__inline void operator += (const CVector3D& cVector3D)
{
fX += cVector3D.fX;
fY += cVector3D.fY;
fZ += cVector3D.fZ;
}




//---------------------------------------------------------------------------------------------
// CVector3D operator - (const CVector3D& cVector3D) const
//
// This function substract the parameter vector from this vector and returns the result
//---------------------------------------------------------------------------------------------
__inline CVector3D operator - (const CVector3D& cVector3D) const
{
return CVector3D(fX - cVector3D.fX, fY - cVector3D.fY, fZ - cVector3D.fZ);
}




//---------------------------------------------------------------------------------------------
// void operator -= (const CVector3D& cVector3D)
//
// This function substract the parameter vector from this vector and sets the calling vector''s
// value to the result
//---------------------------------------------------------------------------------------------
__inline void operator -= (const CVector3D& cVector3D)
{
fX -= cVector3D.fX;
fY -= cVector3D.fY;
fZ -= cVector3D.fZ;
}




//---------------------------------------------------------------------------------------------
// CVector3D operator / (const float& fValue) const
//
// This function divides the calling vector''s components by the give value
//---------------------------------------------------------------------------------------------
__inline CVector3D operator / (const float& fValue) const
{
return CVector3D(fX / fValue, fY / fValue, fZ / fValue);
}




//---------------------------------------------------------------------------------------------
// void operator /= (const float& fValue)
//
// This function divides the vector''s components by the give value
//---------------------------------------------------------------------------------------------
__inline void operator /= (const float& fValue)
{
fX /= fValue;
fY /= fValue;
fZ /= fValue;
}




//---------------------------------------------------------------------------------------------
// void operator = (const CVector3D& cVector3D)
//
// This function copies the properties from the parameter''s CVector3D class to the function
// calling class
//---------------------------------------------------------------------------------------------
__inline void operator = (const CVector3D& cVector3D)
{
fX = cVector3D.fX;
fY = cVector3D.fY;
fZ = cVector3D.fZ;
}



//---------------------------------------------------------------------------------------------
// bool operator == (const CVector3D& cVector3D) const
//
// This function tests wheter two CVector3D classes are equal
//---------------------------------------------------------------------------------------------
__inline bool operator == (const CVector3D& cVector3D) const
{
if((fX == cVector3D.fX) &&
(fY == cVector3D.fY) &&
(fZ == cVector3D.fZ))
{
return true;
}

return false;
}




//---------------------------------------------------------------------------------------------
// static float Angle(const CVector3D& cVector3D1, const CVector3D& cVector3D2)
//
// Returns the angle between the two vectors
// Note that the parameter vectors must be NORMALIZED vectors!!
//---------------------------------------------------------------------------------------------
__inline static float Angle(const CVector3D& cVector3D1, const CVector3D& cVector3D2)
{
return acosf(DotProduct(cVector3D1, cVector3D2));
}




//---------------------------------------------------------------------------------------------
// float Angle(const CVector3D& cVector3D)
//
// Returns the angle between THIS vector and the cVector3D vector
// Note that the THIS vector AND the parameter vector must be NORMALIZED vectors!!
//---------------------------------------------------------------------------------------------
__inline float Angle(const CVector3D& cVector3D)
{
return acosf(DotProduct(*this, cVector3D));
}




//---------------------------------------------------------------------------------------------
// static CVector3D CrossProduct(const CVector3D& cVector3D1, const CVector3D& cVector3D2)
//
// Calculate the cross-product of the two vectors and return the result vector
//---------------------------------------------------------------------------------------------
__inline static CVector3D CrossProduct(const CVector3D &cVector3D1, const CVector3D &cVector3D2)
{
return CVector3D(cVector3D1.fY * cVector3D2.fZ - cVector3D1.fZ * cVector3D2.fY, cVector3D1.fZ * cVector3D2.fX - cVector3D1.fX * cVector3D2.fZ, cVector3D1.fX * cVector3D2.fY - cVector3D1.fY * cVector3D2.fX);
}




//---------------------------------------------------------------------------------------------
// static float DotProduct(const CVector3D &cVector3D)
//
// Calculate the dotproduct of the vector with itself
//---------------------------------------------------------------------------------------------
__inline static float DotProduct(const CVector3D &cVector3D)
{
return cVector3D.fX * cVector3D.fX + cVector3D.fY * cVector3D.fY + cVector3D.fZ * cVector3D.fZ;
}




//---------------------------------------------------------------------------------------------
// static float DotProduct(const CVector3D &cVector3D1, const CVector3D &cVector3D2)
//
// Calculate the dotproduct of the vector with itself
//---------------------------------------------------------------------------------------------
__inline static float DotProduct(const CVector3D &cVector3D1, const CVector3D &cVector3D2)
{
return cVector3D1.fX * cVector3D2.fX + cVector3D1.fY * cVector3D2.fY + cVector3D1.fZ * cVector3D2.fZ;
}




//---------------------------------------------------------------------------------------------
// static CVector3D CalculateNormal(const CVector3D &v1, const CVector3D &v2, const CVector3D &v3)
//
// Calculates a normal vector based on 3 vectors
//---------------------------------------------------------------------------------------------
__inline static CVector3D CalculateNormal(const CVector3D &v0, const CVector3D &v1, const CVector3D &v2)
{
CVector3D vNormal;
float fNormalLength;

vNormal.fX = (v0.fY - v1.fY) * (v0.fZ - v2.fZ) - (v0.fZ - v1.fZ) * (v0.fY - v2.fY);
vNormal.fY = (v0.fZ - v1.fZ) * (v0.fX - v2.fX) - (v0.fX - v1.fX) * (v0.fZ - v2.fZ);
vNormal.fZ = (v0.fX - v1.fX) * (v0.fY - v2.fY) - (v0.fY - v1.fY) * (v0.fX - v2.fX);

fNormalLength = sqrtf(vNormal.fX * vNormal.fX + vNormal.fY * vNormal.fY + vNormal.fZ * vNormal.fZ);
vNormal.fX /= fNormalLength;
vNormal.fY /= fNormalLength;
vNormal.fZ /= fNormalLength;

return vNormal;
}



//---------------------------------------------------------------------------------------------
// float DotProduct()
//
// Calculate the dot-product of the vector
//---------------------------------------------------------------------------------------------
__inline float DotProduct() const
{
return fX * fX + fY * fY + fZ * fZ;
}




//---------------------------------------------------------------------------------------------
// float Length()
//
// Calculates the length of the vector
//---------------------------------------------------------------------------------------------
__inline float Length() const
{
return sqrtf(fX * fX + fY * fY + fZ * fZ);
}


//---------------------------------------------------------------------------------------------
// void Normalize()
//
// Normalizes the vector
//---------------------------------------------------------------------------------------------
__inline void Normalize()
{
float fLength;

fLength = sqrtf(fX * fX + fY * fY + fZ * fZ);

fX /= fLength;
fY /= fLength;
fZ /= fLength;
}




//---------------------------------------------------------------------------------------------
// static CVector3D Reflection(const CVector3D& vVector3D, const CVector3D& vNormal)
//
// Reflects the vector about a normal vector
//---------------------------------------------------------------------------------------------
__inline static CVector3D Reflection(const CVector3D& vVector3D, const CVector3D& vNormal)
{
CVector3D newvector = vVector3D / vVector3D.Length();
return (newvector - vNormal * 2.0f * CVector3D::DotProduct(newvector, vNormal)) * vVector3D.Length();
}




//---------------------------------------------------------------------------------------------
// void Reflection(const CVector3D& vNormal)
//
// Reflects THIS vector about a normal vector
//---------------------------------------------------------------------------------------------
__inline void Reflection(const CVector3D& vNormal)
{
CVector3D newvector = *this / this->Length();
*this = (newvector - vNormal * 2.0f * CVector3D::DotProduct(newvector, vNormal)) * this->Length();
}




//---------------------------------------------------------------------------------------------
// void SetVector(const float& fNewX, const float& fNewY, const float& fNewZ)
//
// Set the data members of the vector
//---------------------------------------------------------------------------------------------
__inline void Set(const float &fNewX, const float &fNewY, const float &fNewZ)
{
fX = fNewX;
fY = fNewY;
fZ = fNewZ;
}

void RotateX(const float &fAngle);
void RotateY(const float &fAngle);
void RotateZ(const float &fAngle);
void Scale(const float &fFactor);
void Scale(const float &fFactorX, const float &fFactorY, const float &fFactorZ);
void TaperX(const float &fRate);
void TaperY(const float &fRate);
void TaperZ(const float &fRate);
void Transform(const float *p_fMatrix); // Has to point to 16 floats
void Translate(const float &fTX, const float &fTY, const float &fTZ);

// The components are public members for easy access
float fX, fY, fZ; // (x, y, z) coordinates for the 3D vector point
};

#endif




Here is the CPP file




//---------------------------------------------------------------------------------------------
// Copyright © 2001 by Nucleus Software, Inc.
// All rights reserved.
//
// $File:: CVector3D.CPP
// $Dependencies:: none
// $Revision:: 1
// $Author:: Ebor Folkertsma
// $Date:: 09-24-2001 8:05pm
//
// $Description:: The CVector3D class offers some nice functionality for 3D vectors
//---------------------------------------------------------------------------------------------
#include "CVector3D.H"
#include


namespace _CVector3D_
{
static CVector3D cVector; // Used for quick transformations
static float fCosAngle;
static float fSinAngle;
}




//-------------------------------------------------------------------------------------------------
// void RotateX(const float &fAngle)
//
// This function rotates the vector around the x-axis
//
// Parameters
// fAngle The rotation angle in degrees
//-------------------------------------------------------------------------------------------------
void CVector3D::RotateX(const float &fAngle)
{
_CVector3D_::fCosAngle = cosf(fAngle * 0.017453292f);
_CVector3D_::fSinAngle = sinf(fAngle * 0.017453292f);

_CVector3D_::cVector.fY = fY * _CVector3D_::fCosAngle - fZ * _CVector3D_::fSinAngle;
_CVector3D_::cVector.fZ = fY * _CVector3D_::fSinAngle + fZ * _CVector3D_::fCosAngle;
fY = _CVector3D_::cVector.fY;
fZ = _CVector3D_::cVector.fZ;

return;
}




//-------------------------------------------------------------------------------------------------
// void RotateY(const float &fAngle)
//
// This function rotates the vector around the y-axis
//
// Parameters
// fAngle The rotation angle in degrees
//-------------------------------------------------------------------------------------------------
void CVector3D::RotateY(const float &fAngle)
{
_CVector3D_::fCosAngle = cosf(fAngle * 0.017453292f);
_CVector3D_::fSinAngle = sinf(fAngle * 0.017453292f);

_CVector3D_::cVector.fX = fX * _CVector3D_::fCosAngle + fZ * _CVector3D_::fSinAngle;
_CVector3D_::cVector.fZ = fX * -_CVector3D_::fSinAngle + fZ * _CVector3D_::fCosAngle;
fX = _CVector3D_::cVector.fX;
fZ = _CVector3D_::cVector.fZ;

return;
}




//-------------------------------------------------------------------------------------------------
// void RotateZ(const float &fAngle)
//
// This function rotates the vector around the z-axis
//
// Parameters
// fAngle The rotation angle in degrees
//-------------------------------------------------------------------------------------------------
void CVector3D::RotateZ(const float &fAngle)
{
_CVector3D_::fCosAngle = cosf(fAngle * 0.017453292f);
_CVector3D_::fSinAngle = sinf(fAngle * 0.017453292f);

_CVector3D_::cVector.fX = fX * _CVector3D_::fCosAngle - fY * _CVector3D_::fSinAngle;
_CVector3D_::cVector.fY = fX * _CVector3D_::fSinAngle + fY * _CVector3D_::fCosAngle;
fX = _CVector3D_::cVector.fX;
fY = _CVector3D_::cVector.fY;

return;
}




//-------------------------------------------------------------------------------------------------
// void Scale(const float &fFactor)
//
// This function scales the vector uniform
//
// Parameters
// fFactor The scaling factor
//-------------------------------------------------------------------------------------------------
void CVector3D::Scale(const float &fFactor)
{
CVector3D::fX *= fFactor;
CVector3D::fY *= fFactor;
CVector3D::fZ *= fFactor;

return;
}




//-------------------------------------------------------------------------------------------------
// void Scale(const float &fFactorX, const float &fFactorY, const float &fFactorZ)
//
// This function scales the vector non-uniform
//
// Parameters
//
// fFactorX The scale factor for the scaling along the x-axis
// fFactorY The scale factor for the scaling along the y-axis
// fFactorZ The scale factor for the scaling along the z-axis
//-------------------------------------------------------------------------------------------------
void CVector3D::Scale(const float &fFactorX, const float &fFactorY, const float &fFactorZ)
{
CVector3D::fX *= fFactorX;
CVector3D::fY *= fFactorY;
CVector3D::fZ *= fFactorZ;

return;
}




//-------------------------------------------------------------------------------------------------
// void TaperX(const float &fRate)
//
// This function tapers the vector along the x-axis
//
// Parameters
// fRate The rate of tapering along the x-axis
//-------------------------------------------------------------------------------------------------
void CVector3D::TaperX(const float &fRate)
{
CVector3D::fY *= 1.0f + fRate * fX;
CVector3D::fZ *= 1.0f + fRate * fX;

return;
}




//-------------------------------------------------------------------------------------------------
// void TaperY(const float &fRate)
//
// This function tapers the vector along the y-axis
//
// Parameters
// fRate The rate of tapering along the y-axis
//-------------------------------------------------------------------------------------------------
void CVector3D::TaperY(const float &fRate)
{
CVector3D::fX = 1.0f + fRate * fY;
CVector3D::fZ = 1.0f + fRate * fY;

return;
}




//-------------------------------------------------------------------------------------------------
// void TaperZ(const float &fRate)
//
// This function tapers the vector along the z-axis
//
// Parameters
// fRate The rate of tapering along the z-axis
//-------------------------------------------------------------------------------------------------
void CVector3D::TaperZ(const float &fRate)
{
CVector3D::fX = 1.0f + fRate * fZ;
CVector3D::fY = 1.0f + fRate * fZ;

return;
}




//-------------------------------------------------------------------------------------------------
// void Transform(const float *p_fMatrix)
//
// This function transforms the vector
//
// Parameters
// pMatrix The transformation matrix that is to be applied to the vector (This must be a pointer to a 16 floats array)
//-------------------------------------------------------------------------------------------------
void CVector3D::Transform(const float *p_fMatrix)
{
_CVector3D_::cVector.fX = fX * p_fMatrix[0] + fY * p_fMatrix[1] + fZ * p_fMatrix[2] + p_fMatrix[3];
_CVector3D_::cVector.fY = fX * p_fMatrix[4] + fY * p_fMatrix[5] + fZ * p_fMatrix[6] + p_fMatrix[7];
_CVector3D_::cVector.fZ = fX * p_fMatrix[8] + fY * p_fMatrix[9] + fZ * p_fMatrix[10] + p_fMatrix[11];
fX = _CVector3D_::cVector.fX;
fY = _CVector3D_::cVector.fY;
fZ = _CVector3D_::cVector.fZ;

return;
}




//-------------------------------------------------------------------------------------------------
// void Translate(const float &fTX, const float &fTY, const float &fTZ)
//
// This function translates the vector
//
// Parameters
// fTX The translation along the x-axis
// fTY The translation along the y-axis
// fTZ The translation along the z-axis
//-------------------------------------------------------------------------------------------------
void CVector3D::Translate(const float &fTX, const float &fTY, const float &fTZ)
{
CVector3D::fX += fTX;
CVector3D::fY += fTY;
CVector3D::fZ += fTZ;

return;
}





Please do not mind the bad layouts... it looks decent in my editor!

Hope this was of any help, Dark

PS: Sorry about the long reply






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS