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

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

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

效果图

using UnityEngine;
using System.Collections;

//圆形烟火弹道轨迹 add by thinbug.LPJ 2017.2.7
public 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 ;

        }
    }
}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值