Original Post
Hi GameDev Readers, I have tried to create a C++ function that takes two points on a sphere (their cartesian coordinate values) and finds a third surface point that is interpolated between the two points based on a scalar ( 0.0 - 1.0 ). Here's the code:
Vector3 SphereCoordFind( const Vector3 _vStart, const Vector3 _vEnd, const float _fScalar ) { // Start, End and Interpolated Cartesian Coordinates Vector3 vStart = _vStart; Vector3 vEnd = _vEnd; Vector3 vPoint; // vStart Spherical Coordinates float fStartAzimuth; float fStartPolar; float fStartLength; float fStartSqrX; float fStartSqrY; float fStartSqrZ; // vEnd Spherical Coordinates float fEndAzimuth; float fEndPolar; float fEndLength; float fEndSqrX; float fEndSqrY; float fEndSqrZ; // vPoint Spherical Coordinates float fAzimuth; float fPolar; float fLength; float fPolarSin; float fAzimuthSin; float fPolarCos; float fAzimuthCos; // Calculate vector lengths fStartLength = _vStart.Magnitude(); fEndLength = _vEnd.Magnitude(); // Normalize vectors vStart.Normalize(); vEnd.Normalize(); // Calculate squares of vStart[x,y,z] fStartSqrX = vStart.x * vStart.x; fStartSqrY = vStart.y * vStart.y; fStartSqrZ = vStart.z * vStart.z; // Calculate squares of vEnd[x,y,z] fEndSqrX = vEnd.x * vEnd.x; fEndSqrY = vEnd.y * vEnd.y; fEndSqrZ = vEnd.z * vEnd.z; // Find Azimuth angle of vStart/vEnd fStartAzimuth = atan2f( vStart.y, vStart.x ); fEndAzimuth = atan2f( vEnd.y, vEnd.x ); // Find Polar angle of vStart/vEnd fStartPolar = acosf( vStart.z / fStartLength ); fEndPolar = acosf( vEnd.z / fEndLength ); // Interpolate the angles between start and finish fLength = fEndLength + ( (fStartLength - fEndLength) * _fScalar ); fAzimuth = fEndAzimuth + ( (fStartAzimuth - fEndAzimuth) * _fScalar ); fPolar = fEndPolar + ( (fStartPolar - fEndPolar) * _fScalar ); // Find the interpolated Cartesian Coordinates fPolarSin = sinf( fPolar ); fPolarCos = cosf( fPolar ); fAzimuthSin = sinf( fAzimuth ); fAzimuthCos = cosf( fAzimuth ); vPoint.x = fLength * fAzimuthCos * fPolarSin; vPoint.y = fLength * fAzimuthSin * fPolarSin; vPoint.z = fLength * fPolarCos; return vPoint; } Can anyone tell me what I'm doing wrong here? It doesn't seem to generate a valid vector. For example, when I pass in: _vStart.x = 1.0f; _vStart.y = 0.0f; _vStart.z = 0.0f; _vEnd.x = -1.0f; _vEnd.y = 0.0f; _vEnd.z = 0.0f; _fScalar = 0.5f the returned vector is: x = -4.37114e-008f y = 1.0f z = -4.37114e-008f when it should be x = 0.0f y = 1.0f z = 0.0f Any thoughts? Graham