Unity实现绕任意轴任意角度旋转向量
本文实例为大家分享了Unity实现绕任意轴任意角度旋转向量的具体代码,供大家参考,具体内容如下
游戏中有一需求,就是一个矩形或者Cube绕着某一点旋转任意角度,现在给出下面算法。
publicstaticVector3RotateRound(Vector3position,Vector3center,Vector3axis,floatangle)
{
Vector3point=Quaternion.AngleAxis(angle,axis)*(position-center);
Vector3resultVec3=center+point;
returnresultVec3;
}
测试用例
usingUnityEngine;
usingSystem.Collections;
publicclassRotateTest:MonoBehaviour
{
publicLineRendererline1;
publicLineRendererline2;
publicfloatangle=30f;
privateVector3v0;
privateVector3v1;
privateVector3v2;
privateVector3v3;
privateVector3v4;
privateVector3vCenter;
voidStart()
{
v0=newVector3(3f,0f,1f);
v1=newVector3(1f,0f,3f);
v2=newVector3(4f,0f,6f);
v3=newVector3(6f,0f,4f);
vCenter=newVector3(2f,0f,2f);
}
//Usethisforinitialization
voidUpdate()
{
line1.SetVertexCount(5);
line1.SetPosition(0,v0);
line1.SetPosition(1,v1);
line1.SetPosition(2,v2);
line1.SetPosition(3,v3);
line1.SetPosition(4,v0);
line2.SetVertexCount(5);
Vector3v01=MathUtils.RotateRound(v0,vCenter,Vector3.up,angle);
Vector3v11=MathUtils.RotateRound(v1,vCenter,Vector3.up,angle);
Vector3v21=MathUtils.RotateRound(v2,vCenter,Vector3.up,angle);
Vector3v31=MathUtils.RotateRound(v3,vCenter,Vector3.up,angle);
Vector3v41=MathUtils.RotateRound(v4,vCenter,Vector3.up,angle);
line2.SetPosition(0,v01);
line2.SetPosition(1,v11);
line2.SetPosition(2,v21);
line2.SetPosition(3,v31);
line2.SetPosition(4,v01);
}
}
效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。