SA.FullBodyIK._LimitXY C# (CSharp) Method

_LimitXY() public static method

public static _LimitXY ( Vector3 &dir, float limitXMinus, float limitXPlus, float limitYMinus, float limitYPlus ) : bool
dir Vector3
limitXMinus float
limitXPlus float
limitYMinus float
limitYPlus float
return bool
		public static bool _LimitXY(
			ref Vector3 dir,				// dirZ
			float limitXMinus,				// X-
			float limitXPlus,				// X+
			float limitYMinus,				// Z-
			float limitYPlus )				// Z+
		{
			bool isXPlus = (dir.x >= 0.0f);
			bool isYPlus = (dir.y >= 0.0f);
			float xLimit = isXPlus ? limitXPlus : limitXMinus;
			float yLimit = isYPlus ? limitYPlus : limitYMinus;

			bool isLimited = false;
			if( xLimit <= IKEpsilon && yLimit <= IKEpsilon ) {
				Vector3 limitedDir = new Vector3( 0.0f, 0.0f, 1.0f );
				Vector3 temp = limitedDir - dir;
				if( Mathf.Abs( temp.x ) > IKEpsilon || Mathf.Abs( temp.y ) > IKEpsilon || Mathf.Abs( temp.z ) > IKEpsilon ) {
					dir = limitedDir;
					isLimited = true;
				}
			} else {
				float inv_xLimit = (xLimit >= IKEpsilon) ? (1.0f / xLimit) : 0.0f;
				float inv_yLimit = (yLimit >= IKEpsilon) ? (1.0f / yLimit) : 0.0f;
				float localX = dir.x * inv_xLimit;
				float localY = dir.y * inv_yLimit;
				float localLen = SAFBIKSqrt( localX * localX + localY * localY + dir.z * dir.z );

				float inv_localLen = (localLen > IKEpsilon) ? (1.0f / localLen) : 0.0f;
				float nrm_localX = localX * inv_localLen; // Counts as sinTheta
				float nrm_localY = localY * inv_localLen; // Counts as cosTheta

				if( localLen > 1.0f ) { // Outer circle.
					if( !isLimited ) {
						isLimited = true;
						localX = nrm_localX;
						localY = nrm_localY;
					}
				}

				float worldX = isLimited ? (localX * xLimit) : dir.x;
				float worldY = isLimited ? (localY * yLimit) : dir.y;

				bool isInverse = (dir.z < 0.0f);

				if( isLimited ) {
					float limitSinSq = (worldX * worldX + worldY * worldY);
					float limitSin = SAFBIKSqrt( limitSinSq );
					float limitCos = SAFBIKSqrt( 1.0f - limitSin * limitSin );
					dir.x = worldX;
					dir.y = worldY;
					dir.z = limitCos;
				} else if( isInverse ) {
					isLimited = true;
					dir.z = -dir.z;
				}
			}

			return isLimited;
		}
FullBodyIK