标签:blog io ar os java on 2014 log cti
#pragma strict public var m_pA : Vector3 = new Vector3(2.0f, 4.0f, 0.0f); public var m_pB : Vector3 = new Vector3(-4.0f, 2.0f,0.0f); private var m_pTemp : Vector3 = new Vector3(0.0f,0.0f,0.0f); private var m_fTemp : float = 0.0f; private var m_fAngle : float = 0.0f; function Awake(){ Debug.Log("向量缩小2倍 :" + VectorScale(m_pA,2).ToString()); //Debug.Log(m_pA.operator * 2();); Debug.Log("向量的负向量 :" + FVector(m_pB).ToString()); Debug.Log("向量标准化 :" + VectorZuo3(m_pA).ToString()); Debug.Log(Vector3.Normalize(m_pA)); Debug.Log("a向量到b向量的长度 :" + Vector2VectorDis(m_pA,m_pB)); Debug.Log(Vector3.Distance(m_pA,m_pB)); Debug.Log("b向量与a向量的夹角 :" + VectorAndVectorAngle(m_pA,m_pB)); Debug.Log(Vector3.Angle(m_pA,m_pB)); Debug.Log("a向量垂直于b向量的向量 :" + VectorZuoye6(m_pA,m_pB).ToString()); Debug.Log(m_pA - Vector3.Project(m_pA,m_pB)); Debug.Log("两个向量在平面上的夹角 :" + VectorZuoye7(m_pA,m_pB)); } //求向量的模 function VerctorMagnitudeTest(a : Vector3) : float{ return Mathf.Sqrt(a.x * a.x + a.y * a.y + a.z * a.z); } //求向量的点积 function VectorDotTest(a : Vector3,b : Vector3) : float{ m_fTemp = a.x * b.x + a.y * b.y + a.z * b.z; return m_fTemp; } //求向量的叉乘 function VectorChaTest(a : Vector3 , b : Vector3) : Vector3{ m_pTemp.x = a.y * b.z - a.z * b.y; m_pTemp.y = a.z * b.x - a.x * b.z; m_pTemp.z = a.x * b.y - a.y * b.x; return m_pTemp; } //向量进行缩放 function VectorScale(a : Vector3,n : int) : Vector3{ m_pTemp = a; if(n != 0) //判0 { m_pTemp.x /= n; m_pTemp.y /= n; m_pTemp.z /= n; } return m_pTemp; } //向量的负向量 function FVector(a : Vector3) : Vector3{ m_pTemp = a; m_pTemp.x = 0.0f -m_pTemp.x; m_pTemp.y = 0.0f -m_pTemp.y; m_pTemp.z = 0.0f -m_pTemp.z; return m_pTemp; } //向量标准化 function VectorZuo3(a : Vector3) : Vector3{ m_pTemp = a; //var fDis : float = a.magnitude;//qiu mo de var fDis : float = VerctorMagnitudeTest(a);//求模 m_pTemp.x = a.x / fDis;//不需要判0 m_pTemp.y = a.y / fDis; m_pTemp.z = a.z / fDis; return m_pTemp; } //a向量到b向量的长度 function Vector2VectorDis(a : Vector3,b : Vector3) : float{ m_pTemp.x = b.x - a.x; m_pTemp.y = b.y - a.y; m_pTemp.z = b.z - a.z; m_fTemp = VerctorMagnitudeTest(m_pTemp);//用自定义的求模函数 return m_fTemp; } //b向量与a向量的夹角 // arcos((a与b点乘)/(a模*b模)) function VectorAndVectorAngle(a : Vector3,b : Vector3) : float{ var fDot : float = VectorDotTest(a,b);//向量的点乘 var fDisM : float = VerctorMagnitudeTest(a) * VerctorMagnitudeTest(b); var fTemp : float = Mathf.Acos(fDot / fDisM); m_fAngle = fTemp * Mathf.Rad2Deg; return m_fAngle; } //a向量垂直于b向量的向量 // a向量 – b*((a与b的点乘)/b模的平方) function VectorZuoye6(a : Vector3, b : Vector3) : Vector3{ var pTemp1 : Vector3 = b; var pTemp2 : Vector3 = a; pTemp1.x *= VectorDotTest(a,b)/(VerctorMagnitudeTest(b)*VerctorMagnitudeTest(b));//用自定义的点积和求模函数 pTemp1.y *= VectorDotTest(a,b)/(VerctorMagnitudeTest(b)*VerctorMagnitudeTest(b));//用自定义的点积和求模函数 pTemp1.z *= VectorDotTest(a,b)/(VerctorMagnitudeTest(b)*VerctorMagnitudeTest(b));//用自定义的点积和求模函数 m_pTemp.x = pTemp2.x - pTemp1.x; m_pTemp.y = pTemp2.y - pTemp1.y; m_pTemp.z = pTemp2.z - pTemp1.z; return m_pTemp; } //a向量与b向量在 向量a与向量b所形成平面 上的夹角 // arsin(a与b叉乘的模/(a模*b模)) function VectorZuoye7(a : Vector3,b : Vector3) : float{ m_fTemp = Mathf.Asin( VerctorMagnitudeTest( VectorChaTest(a,b)) / ( VerctorMagnitudeTest(a) * VerctorMagnitudeTest(b) ) );//用自定义的叉乘和求模函数 m_fAngle = m_fTemp * Mathf.Rad2Deg; return m_fAngle; }
Unity3d修炼之路:游戏开发中,3d数学知识的练习【1】(不断更新.......)
标签:blog io ar os java on 2014 log cti
原文地址:http://blog.csdn.net/xiaxiang123/article/details/41217985