书籍推荐:

  1. 3D数学基础:图形与游戏开发——游戏软件开发专家系列(美)邓恩
  2. Unity Shader入门精要 冯乐乐(92年)
  3. 数据结构(Python语言描述)
  4. 数据结构、算法与应用(C++语言描述)
  5. 算法导论 3
  6. 黑客与画家
  7. 程序员生存之道
  8. 流体力学:八叉数空间划分
 
四元数的几个概念:
四元数的模长:Mathf.Sqrt(x^2 + y^2 + z^2 + w^2)
标准四元数:模长为1的四元数叫标准四元数:在Unity中所有用来表示旋转的四元数都是标准四元数
单位四元数:[0,0,0,1],单位四元数表示无旋转,Quaternion.identity
共轭四元数:让原四元数虚数部分取负,让原四元数反向旋转,Unity中没有共轭四元数
四元数 * 共轭四元数 = 单位四元数
四元数的逆:类似求导,
四元素的逆 = 共轭四元数 / 四元数的模长,即q-1 = *q / |q|
在Unity中,因为所有表示旋转的四元数都是标准四元数,模长均为1,所以四元素的逆 = 共轭四元数
 
四元数让向量旋转:
四元数不仅可以旋转点,还可以旋转向量
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RotateVector : MonoBehaviour {
  5. // Use this for initialization
  6. void Start () {
  7. Vector3 dir = new Vector3(,,);
  8. Debug.DrawLine(Vector3.zero,dir,Color.red,);
  9. //单位四元数表示无旋转
  10. Debug.Log(Quaternion.identity);
  11. //四元数的逆
  12. Quaternion q = Quaternion.AngleAxis(, Vector3.up);
  13. Quaternion tempQ = Quaternion.Inverse(q);
  14. Debug.Log(q);
  15. Debug.Log(tempQ);
  16. //旋转四元数
  17. Quaternion rotateQ = Quaternion.AngleAxis(, Vector3.up);
  18. Vector3 newVector = rotateQ * dir;
  19. Debug.DrawLine(Vector3.zero, newVector, Color.green, );
  20. //自己写一个RotationAround
  21. //RotateAround(Vector3 point, Vector3 axis, float angle);
  22. }
  23. // Update is called once per frame
  24. void Update () {
  25.  
  26. }
  27. }
四元数和欧拉角的优缺点:
1、欧拉角的优点:
(1)、可以同时绕着三根轴一起旋转
(2)、欧拉角节省内存,节省33.3333%的内存
(3)、直观
2、欧拉角的缺点:
(1)、万向节死锁
(2)、误差大
1、四元数的优点:
(1)、没有万向节死锁
(2)、误差小
(3)、可以绕任意一根轴旋转
2、四元数的缺点:
(1)、只能一根一根轴旋转
(2)、比欧拉角多了33.3333%的内存
(3)、不直观
锁可以固定游戏物体的检视面板,方便把其他游戏物体拖拽进来

models和prefabs的区别
models模型拖到场景,再拖回资源,可以变成预制体
四方连续,向四周重复地连续和延伸扩展而成的图案形式。
二方连续,向上下或左右两个方向反复连续而形成的纹样。
世界坐标系转屏幕坐标系
Vector2 screenPos = Camera.main.WorldToScreenPoint(target.position);
屏幕坐标系转世界坐标系
Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, -Camera.main.transform.position.z));
 
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RotateAroundScript : MonoBehaviour {
  5. public Transform sphere;
  6. public float rotateSpeed = ;
  7. // Use this for initialization
  8. void Start () {
  9.  
  10. }
  11.  
  12. // Update is called once per frame
  13. void Update () {
  14. //调用RotateAround
  15. RotateAround(sphere.position, Vector3.up, rotateSpeed);
  16. }
  17.  
  18. public void myRotateAround(Vector3 point, Vector3 axis, float angle)
  19. {
  20. Vector3 A = transform.position - point;
  21. //Vector3 n = Vector3.Project();
  22. //Vector3.Dot(axis, A) = | n | * axis.magnitude;
  23. //| n | = Vector3.Dot(axis, A) / axis.magnitude;
  24. //Vector3 n = axis.normalized * | n |;
  25. Vector3 n = axis.normalized * Vector3.Dot(axis, A) / axis.magnitude;
  26. Vector3 a = A - n;
  27. Quaternion q = Quaternion.AngleAxis(angle, axis);
  28. Vector3 b = q * a;
  29. }
  30. }

复习

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class w06d4 : MonoBehaviour {
  5. public Transform target;
  6. public Vector3 cubeSize;
  7. //欧拉角是如何表示旋转的
  8. //使用x y z来表示旋转 x表示绕x轴旋转多少度 y表示绕y轴旋转多少度 z表示绕z轴旋转多少度
  9. //四元数是如何表示旋转的
  10. //使用x y z w来表示旋转 通过轴角对来表示旋转 n轴转45度 x = n.x * sin<theta/2> , y = n.y * sin<theta/2>, z = n.z * sin<theta/2>, w = cos<theta/2>
  11. //欧拉角和四元数的优缺点
  12. //欧拉角
  13. // 优点: 直观,容易理解
  14. // 缺点: 有万向节锁
  15. // 只能绕固定轴旋转
  16. // 不能实现旋转的平滑差值
  17. //四元数
  18. // 优点: 可以实现任何角度的旋转
  19. // 可以实现旋转叠加
  20. // 没有万向节锁 、
  21. // 可以绕任意轴旋转
  22. // 可以实现旋转的平滑差值
  23. // 缺点: 不直观,不容易理解
  24. //API
  25. //使当前游戏物体的旋转变为 绕x轴旋转90
  26. //1)、使用欧拉角的形式赋值 //transform.eulerAngles = new Vector3(90, 0, 0);
  27. // 使用四元数的形式赋值 //transform.rotation = Quaternion.AngleAxis(90, Vector3.right);
  28. //2)、使当前游戏物体直接看向目标
  29. transform.LookAt(target);
  30. transform.rotation = Quaternion.LookRotation(target.position - transform.position);
  31. //3)、使当前游戏物体缓缓看向目标
  32. transform.rotation = Quaternion.Slerp(transform.rotation,
  33. Quaternion.LookRotation(target.position - transform.position),
  34. /Quaternion.Angle(transform.rotation, Quaternion.LookRotation(target.position - transform.position)));
  35. //4)、把一个欧拉角转换为四元数
  36. transform.rotation = Quaternion.Euler(, , );
  37. //5)、使用鼠标的水平轴值控制当前物体旋转(使用四元数叠加)
  38. float mouseX = Input.GetAxis("Mouse X");
  39. //自身的 绕Y轴转 XX 度
  40. transform.rotation *= Quaternion.Euler(, mouseX, );
  41. //世界的 绕Y轴转 XX 度
  42. transform.rotation = Quaternion.Euler(, mouseX, ) * transform.rotation;
  43. void Start () {
  44. transform.rotation = Quaternion.AngleAxis(, Vector3.right);
  45. transform.eulerAngles = new Vector3(, , );
  46. transform.LookAt(target);
  47. transform.rotation = Quaternion.LookRotation(target.position - transform.position);
  48. transform.rotation = Quaternion.Euler(, , );
  49. }
  50.  
  51. void Update () {
  52. transform.rotation = Quaternion.Slerp(
  53. transform.rotation,
  54. Quaternion.LookRotation(target.position - transform.position),
  55. / Quaternion.Angle(transform.rotation, Quaternion.LookRotation(target.position - transform.position)));
  56. float mouseX = Input.GetAxis("Mouse X");
  57. // 自身的 绕Y轴转 XX 度
  58. transform.rotation *= Quaternion.Euler(, mouseX, );
  59. // 世界的 绕Y轴转 XX 度
  60. transform.rotation = Quaternion.Euler(, mouseX, ) * transform.rotation;
  61. }
  62. private void OnDrawGizmos()
  63. {
  64. Gizmos.color = new Color(, , , 0.2f);
  65. Gizmos.DrawSphere(transform.position, );
  66. }
  67. private void OnDrawGizmosSelected()
  68. {
  69. Gizmos.color = Color.red;
  70. Gizmos.DrawCube(transform.position, cubeSize);
  71. }
  72.  
  73. }
 

Unity3D学习笔记(八):四元素和书籍推荐的更多相关文章

  1. Unity3D学习笔记(四)Unity的网络基础(C#)

    一 网络下载可以使用WWW类下载资源用法:以下载图片为例WWW date = new WWW("<url>");yield return date;texture = ...

  2. [转]Unity3D学习笔记(四)天空、光晕和迷雾

    原文地址:http://bbs.9ria.com/thread-186942-1-1.html 作者:江湖风云 六年前第一次接触<魔兽世界>的时候,被其绚丽的画面所折服,一个叫做贫瘠之地的 ...

  3. Unity3D学习笔记(四):物理系统碰撞和预制体

    Rigidbody(刚体组件):加了此组件游戏物体就变成刚体了 ----Mass(质量,单位kg):重力G = 质量m * 重力加速度g(g=9.81 m/s^2) --------冲量守恒定理 动量 ...

  4. python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例

    python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...

  5. X-Cart 学习笔记(四)常见操作

    目录 X-Cart 学习笔记(一)了解和安装X-Cart X-Cart 学习笔记(二)X-Cart框架1 X-Cart 学习笔记(三)X-Cart框架2 X-Cart 学习笔记(四)常见操作 五.常见 ...

  6. C++Primer第5版学习笔记(四)

    C++Primer第5版学习笔记(四) 第六章的重难点内容         你可以点击这里回顾第四/五章的内容       第六章是和函数有关的知识,函数就是命名了的代码块,可以处理不同的情况,本章内 ...

  7. Go语言学习笔记八: 数组

    Go语言学习笔记八: 数组 数组地球人都知道.所以只说说Go语言的特殊(奇葩)写法. 我一直在想一个人参与了两种语言的设计,但是最后两种语言的语法差异这么大.这是自己否定自己么,为什么不与之前统一一下 ...

  8. 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版

    目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...

  9. 【opencv学习笔记八】创建TrackBar轨迹条

    createTrackbar这个函数我们以后会经常用到,它创建一个可以调整数值的轨迹条,并将轨迹条附加到指定的窗口上,使用起来很方便.首先大家要记住,它往往会和一个回调函数配合起来使用.先看下他的函数 ...

随机推荐

  1. python count()

    count() 描述 Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. 语法 count()方法语法: str.count(sub, st ...

  2. 【Espruino】NO.07 获取电压值

    http://blog.csdn.net/qwert1213131/article/details/27985645 本文属于个人理解,能力有限,纰漏在所难免.还望指正! [小鱼有点电] 前几节的内容 ...

  3. 文件上传 - iframe上传

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. [js]ext.js探索

    Ext JS 经常会遇到布局等头疼的问题,一直在用bootstrap,但是我不喜欢这玩意出的效果想找个合适的js架构入手 http://examples.sencha.com/extjs/6.6.0/ ...

  5. [django]JsonResponse序列化数据

    def home(request): data = { 'name': 'maotai', 'age': 22 } import json return HttpResponse(json.dumps ...

  6. CentOS6.5 升级 Python 2.7 版本

    转载请注明出处http://write.blog.csdn.net/mdeditor 目录 目录 前言 安装Python-279 解决YUM与Python279的兼容问题 前言 CentOS 6.5中 ...

  7. PHP自定义函数返回多个值

    PHP自定义函数只允许用return语句返回一个值,当return执行以后,整个函数的运行就会终止. 有时要求函数返回多个值时,用return是不可以把值一个接一个地输出的. return语句可以返回 ...

  8. VB.net 与线程

    Imports System.Threading Imports System Public Class Form1 Dim th1, th2 As Thread Public Sub Method1 ...

  9. shell应用技巧

    Shell 应用技巧 Shell是一个命令解释器,是在内核之上和内核交互的一个层面. Shell有很多种,我们所使用的的带提示符的那种属于/bin/bash,几乎所有的linux系统缺省就是这种she ...

  10. python ( EOL while scanning string literal)

    python错误: EOL while scanning string literal: 这个异常造成的原因是字符串,引号没有成对出现 参考:http://www.jb51.net/article/6 ...