目录

1、叉乘

2、四元素

3、四元素旋转

4、四元素和四元素相乘

5、鼠标控制物体旋转

6、发射子弹

7、环形发射子弹

8、子弹缓冲池


1、叉乘

两个向量叉乘,得到一个新的向量,新向量跟原始两个向量都垂直,也就是得到由两个向量所确定平面的法向量。

a(x, y , z)    b(i , j , k)

a  *  b   =    (y * k  -  z * j  ,   x * k -  z  *  i  ,  x * j - y * i)

public class CrossTest : MonoBehaviour
{public GameObject cube;public GameObject sphere;void Update(){Vector3 v = sphere.transform.position - cube.transform.position;Vector3 cross = Vector3.Cross(cube.transform.forward, v);Debug.DrawLine(cube.transform.position, cube.transform.forward * 10, Color.blue);Debug.DrawLine(cube.transform.position, sphere.transform.position , Color.red);Debug.DrawRay(cube.transform.position, cross, Color.green);if (cross.y > 0){Debug.Log("在右边");}else{Debug.Log("在左边");}}
}

2、四元素

万向节死锁  <x,y,z>

实数:分为有理数和无理数

虚数:i ^ 2 = -1

复数:实数 + 虚数

超复数:(x,y,z,w)

        X = n * x * sin(/2)

        Y = n * y * sin(/2)

        Z = n * z * sin(/2)

        W = cos(/ 2)

public class QuaternionTest : MonoBehaviour
{public GameObject cube;void Start(){cube.transform.rotation = new Quaternion(0, 0, Mathf.Sin(30 * Mathf.Deg2Rad),Mathf.Cos(30 * Mathf.Deg2Rad));//cube绕z轴旋转60度Quaternion q1 = Quaternion.Euler(0, 30, 0);cube.transform.rotation = q1;//cube绕y轴旋转30度Quaternion q2 = Quaternion.Euler(new Vector3(0, 30, 0));cube.transform.rotation = q2;Quaternion q3 = Quaternion.AngleAxis(30, Vector3.forward);cube.transform.rotation = q3;}}

3、四元素旋转

1)、cube位置rest,cube 从沿y轴从0度到60度旋转(Lerp(差值))

public class QuaternionRotate : MonoBehaviour
{public GameObject cube;public Transform target;void Update(){Quaternion q2 = Quaternion.Euler(0, 60, 0);cube.transform.rotation = Quaternion.Lerp(cube.transform.rotation, q2, 0.1f);Debug.Log("旋转角度:" + cube.transform.rotation.eulerAngles.y);//0-60Debug.Log("cube.transform.rotation.y" + cube.transform.rotation.y);//0-0.5}
}

2)、用下面这个方法是错误的,q1的y值一直都是从0 - 32.975变化,没更新一次都执行:Quaternion q1 = Quaternion.Euler(0, 30, 0);

public class QuaternionRotate : MonoBehaviour
{public GameObject cube;void Update(){Quaternion q1 = Quaternion.Euler(0, 30, 0);Quaternion q2 = Quaternion.Euler(0, 60, 0);q1 = Quaternion.Lerp(q1, q2, 0.1f);cube.transform.rotation = q1;//cube.transform.rotation.eulerAngles.y = 32.975}
}

正确的做法:q1变量放方法外。

public class QuaternionRotate : MonoBehaviour
{public GameObject cube;Quaternion q1 = Quaternion.Euler(0, 30, 0);void Update(){Quaternion q2 = Quaternion.Euler(0, 60, 0);q1 = Quaternion.Lerp(q1, q2, 0.1f);cube.transform.rotation = q1;//0 - 60}
}

3)、旋转看向目标物体

1.LookAt

​
public class QuaternionLookRotate : MonoBehaviour
{public GameObject cube;public Transform target;void Update(){//cube主角z轴瞬间看向目标物体cube.transform.LookAt(target);}
}​

2.public static Quaternion LookRotation(Vector3 forward);

public class QuaternionLookRotate : MonoBehaviour
{public GameObject cube;void Update(){Vector3 dir = new Vector3(30, 0, 0);//(30,0,0)相当于(1,0,0)//cube主角的z轴看向目标物体(dir)即x轴正方向cube.transform.rotation = Quaternion.LookRotation(dir);}
}

        

当改变dir的值时,cube的z轴正方向看向的位置也改变

public class QuaternionLookRotate : MonoBehaviour
{public GameObject cube;void Update(){Vector3 dir = new Vector3(0, 10, 0);cube.transform.rotation = Quaternion.LookRotation(dir);}
}

3.public static Quaternion LookRotation(Vector3 forward, [DefaultValue("Vector3.up")] Vector3 upwards);

public class QuaternionLookRotate : MonoBehaviour
{public GameObject cube;public Transform target;private void Start(){Debug.Log("Vector3.up:" + Vector3.up);//(0,1,0)}void Update(){Quaternion q1 = Quaternion.LookRotation(target.position - cube.transform.position);cube.transform.rotation = Quaternion.Lerp(cube.transform.rotation, q1, 0.1f);//最后cube的z轴指向targetDebug.DrawLine(cube.transform.position, target.position,Color.blue);}
}

4、四元素和四元素相乘

就是两个相加

    public GameObject cube;public Transform target;void Start(){Quaternion q1 = Quaternion.Euler(0, 30, 0);Quaternion q2 = Quaternion.Euler(0, 60, 0);Quaternion q3 = q1 * q2;cube.transform.rotation = q3;Debug.Log("cube.transfor.rotation.eulerAngles: " +                 cube.transform.rotation.eulerAngles);}

5、鼠标控制物体旋转

public class MouseRotate : MonoBehaviour
{public GameObject cube;private float mousex;void Update(){mousex = Input.GetAxis("Mouse X");Quaternion q = Quaternion.AngleAxis(mousex, Vector3.up);cube.transform.rotation *= q;}
}

6、发射子弹

biubullet类挂载在发射子弹的主角物体上,一定角度生成子弹

public class biubullet : MonoBehaviour
{public GameObject bullet;private float timer;void Start(){bullet = Resources.Load<GameObject>("Prefabs/bullet");}void Update(){timer += Time.deltaTime;if(timer > 3){Vector3 dir = Quaternion.AngleAxis(-15, Vector3.up) * transform.forward;GameObject go = Instantiate(bullet);go.transform.position = transform.position;go.transform.rotation = Quaternion.LookRotation(dir);timer = 0;}}
}

BulletController类挂载在子弹上,控制子弹的移动

public class BulletController : MonoBehaviour
{void Update(){transform.Translate(Vector3.forward * 0.01f, Space.Self);}
}

7、环形发射子弹

设置timer的时间,3秒后发射一次环形子弹,发射完一次环形子弹就将timer设置回0

public class biubullet : MonoBehaviour
{public GameObject bullet;private float timer;void Start(){bullet = Resources.Load<GameObject>("Prefabs/bullet");}void Update(){timer += Time.deltaTime;if(timer > 3){//for(int i = 0; i < 5; i++)//{//    Vector3 dir = Quaternion.AngleAxis(-30 + i *15, Vector3.up) * transform.forward;//    GameObject go = Instantiate(bullet);//    go.transform.position = transform.position;//    go.transform.rotation = Quaternion.LookRotation(dir);//}//timer = 0;for (int i = 0; i < 24; i++){Vector3 dir = Quaternion.AngleAxis(-30 + i * 15, Vector3.up) * transform.forward;GameObject go = Instantiate(bullet);go.transform.position = transform.position;go.transform.rotation = Quaternion.LookRotation(dir);}timer = 0;}}
}
public class BulletController : MonoBehaviour
{void Update(){transform.Translate(Vector3.forward * 0.01f, Space.Self);}
}

8、子弹缓冲池

1)、PoolMain    缓冲池单例;挂载在创建的缓冲池空物体上

public class PoolMain : MonoBehaviour
{public static PoolMain Instance;private void Awake(){Instance = this;}GameObject bulletPool;public GameObject GetBulletPool(){if(bulletPool == null){bulletPool = GameObject.Find("bulletPool");}return bulletPool;}
}

2)、biubullet   主角控制子弹的生成,缓冲池里没有子物体时克隆子弹(因为每3秒生成一圈子弹,而每5秒后将已生成的子弹设为缓冲池的子物体,所以克隆一圈子弹),缓冲池里有子物体时,就取出子物体。

public class biubullet : MonoBehaviour
{public GameObject bullet;private float timer;void Start(){bullet = Resources.Load<GameObject>("Prefabs/bullet");}void Update(){timer += Time.deltaTime;if(timer > 3){for (int i = 0; i < 24; i++){GameObject go = null;//找到缓冲池父物体GameObject pool = PoolMain.Instance.GetBulletPool();//判断缓冲池子物体数量是否足够if(pool.transform.childCount >0){//取缓冲池里的第一个子物体go = pool.transform.GetChild(0).gameObject;//把子弹物体迅速从缓冲池物体内取出go.transform.parent = null;//将游戏物体(子弹)设置为激活状态go.SetActive(true);}else{//缓冲池里没有子物体,就需要克隆子弹go = Instantiate(bullet);}Vector3 dir = Quaternion.AngleAxis(-30 + i * 15, Vector3.up) * transform.forward;go.transform.position = transform.position;go.transform.rotation = Quaternion.LookRotation(dir);}timer = 0;}}
}

3)、BulletController   控制子弹的移动,每5秒后将生成的子弹设置缓冲池物体的子物体,并失活

public class BulletController : MonoBehaviour
{private float timer;private GameObject pool;//缓冲池(空物体)private void Start(){pool = PoolMain.Instance.GetBulletPool();}void Update(){timer += Time.deltaTime;if(timer > 5){//将生成的子弹的父物体设置为缓冲池物体transform.parent = pool.transform;timer = 0;//作为缓冲池物体的子物体后失活gameObject.SetActive(false);}transform.Translate(Vector3.forward * 0.01f, Space.Self);}
}

3d 数学(叉乘、四元素、四元素旋转、四元素和四元素相乘、鼠标控制物体旋转、发射子弹、环形发射子弹、子弹缓冲池)相关推荐

  1. WebGL入门(三十四)-三维空间中鼠标控制物体旋转,用鼠标控制立方体的旋转

    用鼠标控制立方体的旋转 1. demo效果 2. 实现要点 2.1 注册鼠标事件 2.1.1 注册鼠标事件函数的声明 2.1.2 注册鼠标事件函数的调用 2.2 纹理图片加载 2.3 图形绘制 3. ...

  2. css3魔方鼠标怎么用,CSS3之3D魔方鼠标控制酷炫效果

    前面文章有制作水晶魔方,这次我们升级一下它的功能,通过鼠标控制魔方旋转. 大家先看效果 这酷炫的效果,你怎么看? 这次效果,咱们需要用JS实现.主要是监听鼠标事件,计算鼠标滑动距离,改变魔方的rota ...

  3. Unity基础(四)--3D数学

    文章目录 一.向量 基本属性 向量与标量的乘除 二.三角函数 角的度量方式 三角函数 点乘(Dot) 叉乘 三.欧拉角与四元数 欧拉角 四元数 基本运算 1.与向量相乘 2.与四元数相乘 四.三维向量 ...

  4. 《 Python List列表全实例详解系列(四)》__列表删除元素(4种方法)删除重复元素(去重)(8种方法)

    <  Python List列表全实例详解系列(四)> __列表删除元素(4种方法)删除重复元素(去重)(8种方法) 我的技术成长&学习资料整理分享之路 我遇到问题查找资料时,经常 ...

  5. 天梭携合作款计时器亮相进博会;乐高集团连续第四年于进博会发布中国文化元素玩具新品 | 知消...

    天梭携合作款计时器亮相第四届中国国际进口博览会.第四届中国国际进口博览会(进博会)于上海成功开幕,瑞士知名制表品牌TISSOT天梭表携精美时计新品再度惊艳亮相.本届进博会是天梭连续第三年参展,也是品牌 ...

  6. 3D数学之四元组应用及实现

    第一次接触四元组是在使用OGRE引擎的时候,那个时候非常好奇,为什么4个数字就能表示一个旋转,另外为什么要用四元组来表示旋转,用旋转矩阵不是挺好的吗?经过一段的时间的学习,算是对四元组有了基础的认识, ...

  7. python列表元素都加倍_关于python列表增加元素的三种操作方法

    关于python列表增加元素的三种操作方法 1.insert方法,该方法包含两个参数,第一个参数为插入的位置参数,第二个参数为插入内容 a = [0,0,0] b = [1,2,3] a.insert ...

  8. 3D数学读书笔记——向量运算及在c++上的实现

    本系列文章由birdlove1987编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhurui_idea/article/details/24782661 开始之前:接 ...

  9. Unity 3D数学\图形学基础-游戏开发(向量)

    Unity 3D数学\图形学基础-游戏开发(向量) 向量运算的几何意义 标量与向量的计算 向量的模长 标准化向量 normalize 0向量 向量与向量的加减法 两点间距离公式(向量间距离) 点积,点 ...

最新文章

  1. 高翔Slambook第七讲代码解读(特征点提取)
  2. ssh免密登录linux服务器
  3. Intel qsv + ffmpeg 硬解h264
  4. python format 槽中槽_printf中的槽和实参--对比python struct包
  5. BZOJ3738 : [Ontak2013]Kapitał
  6. ESP32在Ubuntu16.04的环境搭建
  7. yii2组件之下拉框带搜索功能(yii-select2)
  8. harmonyos2.0测评,爆料:荣耀30 Pro开始测试华为鸿蒙HarmonyOS 2.0
  9. Linux C函数之文件及目录函数
  10. Dubbo注册中心-监控中心安装笔记(CentOS7)
  11. onlaunch 异步_微信小程序之onLaunch与onload异步问题
  12. shell学习之创建函数
  13. Mongodb 学习
  14. (转)黄金交易革命即将到来?区块链技术让你像刷卡一样“刷黄金”
  15. C#技术分享【PDF转换成图片——10种方案】(2013-07-25重新整理)
  16. 迅雷上如何下载热映的电影大片~~
  17. Django ---uploads files
  18. 一文读懂 select count(*) 底层原理
  19. 【DB2】DB2错误号汇总
  20. CRT显示器和液晶显示器

热门文章

  1. android 复制u盘文件到手机本地_原来把手机资料传到U盘里这么简单!
  2. apicloud地图导航
  3. 利用ps制作pbr贴图
  4. python两个变量互换值编程_在编程中实现两个变量的值交换
  5. 抖音壁纸小程序分销源代码
  6. Python读取Excel单元格的内容
  7. 模型压缩整理2020.5.6
  8. vue+elementUI实现自定义表单模板组件(一)
  9. pscad仿真 采用pscad搭建220kv三相空载输电线路,仿真合空线,切空线过电压
  10. Spring IOC(控制反转)思想笔记