首页 > 学院 > 开发设计 > 正文

四元数在旋转的运用-圆形烟火弹道轨迹

2019-11-11 04:19:12
字体:
来源:转载
供稿:网友

向量和四元数经常混淆,四元数可以看做是一个向量+这个向量的旋转,这样容易理解。

这个例子是用于射击游戏,类似于喷子类型武器发射出多个子弹的弹道轨迹,是以瞄准方向以圆形半径或者角度的几个点,所以用了Quaternion.AngleAxis函数来获得这几个轨迹感觉运算能简便写,有更好的算法的同学不妨留言探讨学习下。

效果图

using UnityEngine;using System.Collections;//圆形烟火弹道轨迹 add by thinbug.LPJ 2017.2.7public class CTestQu : MonoBehaviour { public Transform[] show; //几个盒子,用来测试显示弹道方向 public float angle = 5f; //扩散角度(圆形大小0-90f之间) void OnGUI() { string txt = string.Format("当前度数:{0:F},Tan:{1:F3}", angle,Mathf.Tan(Mathf.Deg2Rad * angle)); GUI.Label(new Rect(0, 0, 680, 50), txt); } void Update () { int i; Vector3 dir; int shuliang = show.Length; //显示几条边的烟火特效 dir = transform.forward; //当前的瞄准方向 Vector3 at = transform.position; Debug.DrawRay(transform.position, dir*150f, Color.red); //红色线表示当前方向 for ( i = 0; i < shuliang; i++) { //首先计算垂直于forward和right的 //1单位向量位置找其他发散点 Vector3 tempPointAt = at + dir; //旋转Z方向获得旋转 Quaternion qtemp = Quaternion.AngleAxis(i / (float)shuliang * 360f, -transform.forward); //右方向是旋转的扩散方向 Vector3 newdir = (qtemp * transform.right).normalized; //黄色线表示发散方向 Debug.DrawRay(tempPointAt, newdir, Color.yellow); //获取到其他发散点 , 根据参数角度,计算发散长度 Vector3 tempPoint = tempPointAt + newdir * Mathf.Tan(Mathf.Deg2Rad * angle); //蓝色显示发散的向量 Debug.DrawLine(tempPointAt, tempPoint, Color.blue); //绿色显示最终的圆形烟火弹道轨迹 Debug.DrawLine(at, tempPoint, Color.green); Vector3 newDir = tempPoint - at; //圆形烟火弹道轨迹 Quaternion q = Quaternion.LookRotation(newDir.normalized); show[i].rotation = q ; } }}

这里做下笔记用于后面旋转参考。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表