向量的叉乘:
数学运算:a(ax,ay,az) x b(bx,by,bz) = c(aybz-azby,azbx-axby,axby-aybx)

几何意义:得到一个新的向量,同时垂直于a向量和b向量,垂直于ab向量所组成的平面,c向量是ab平面的法向量
左手螺旋定则:四指指向a,握向b,大拇指指向c
作用
1、求顺指针,逆时针关系(y>0,y<0)
2、求平面的法向量
 
四元数(威廉·哈密顿):
万向节死锁(Gimbal Lock):
四元数和欧拉角的优缺点:
欧拉角缺点:万向节死锁 http://v.youku.com/v_show/id_XNzkyOTIyMTI=.html
 
四元数:xyzw四个分量,w为实部
超复数:是由一个实部 + 三个虚部组成的,如4 + 2i + 3j + 4k
复数:实部 + 虚部,如3 + 2i,5 - 3i。复数的实部为零,即为虚数;虚部为零,即为实数。
----虚数:如果一个数的平方等于负一,那么这个数就是虚数单位,如x^2 = -1,3i,10k,9j
----实数:有理数和无理数的集合
--------有理数:有限的或者无限循环的,如1/3
--------无理数:无限不循环的小数,如PI,e,根号2
四元数中存储的是轴角对儿<n(x,y,z), theta>
x = n.x * sin(theta/2)
y = n.y * sin(theta/2)
z = n.z * sin(theta/2)
w = cos(theta/2)
 
比如:绕着y轴旋转90度:Quaternion(0,0.707,0,0.707)
1、我们用乘法来表示四元数的旋转量的叠加
欧拉角和四元数互转
public static Quaternion Euler(Vector3 euler);
public static Quaternion Euler(float x, float y, float z);
public static Quaternion LookRotation(Vector3 forward);
public static Quaternion LookRotation(Vector3 forward, [DefaultValue("Vector3.up")] Vector3 upwards);
插值:from + (to - from) * t
线性插值:
public static Vector3 Lerp(Vector3 a, Vector3 b, float t);
a:from是起始的位置
b:to是目标位置
t:在from到to之间插值
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LerpTest : MonoBehaviour
{
public Transform sphere;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//API:Vector3.Lerp,线性插值,起始位置from,目标位置to,每次插剩余距离(from-to)的百分之多少t
transform.position = Vector3.Lerp(transform.position, sphere.position, 0.05f);
//Lerp的匀速移动
transform.position = Vector3.Lerp(transform.position, sphere.position, /Vector3.Distance(transform.position,sphere.position) * 0.05F);
//Lerp的匀速旋转
//API:Mathf.Lerp();
}
}

判断方位

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyLookPlayer : MonoBehaviour {
public Transform player;
public float result1;
public float result2;
// Use this for initialization
void Start () {
Vector3 forwardVector = transform.forward;
Vector3 enemy2Player = player.position - transform.position;
//result1 = enemy2Player.x * forwardVector.x + enemy2Player.y * forwardVector.y + enemy2Player.z + forwardVector.z;
result1 = Vector3.Dot(forwardVector, enemy2Player);
Vector3 result = Vector3.Cross(forwardVector, enemy2Player);
result2 = result.y;
} // Update is called once per frame
void Update () { }
public void OnGUI()
{
if (result1 > )
{
if (result2 > )
{
GUILayout.Label("玩家在我的方位:右前方");
}
else
{
GUILayout.Label("玩家在我的方位:左前方");
}
}
else
{
if (result2 > )
{
GUILayout.Label("玩家在我的方位:右后方");
}
else
{
GUILayout.Label("玩家在我的方位:左后方");
}
} }
}

路点移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FindPath : MonoBehaviour {
public Transform[] paths;
public Vector3 dir;
public float moveSpeed;
public float rotSpeed;
int index = ;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
if (Vector3.Distance(paths[index].position, transform.position) <= 0.5f)
{
index++;
index %= paths.Length;
}
dir = paths[index].position - transform.position;
transform.Translate(dir *Time.deltaTime* moveSpeed,Space.World);
Quaternion targetRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation,/ Quaternion.Angle(transform.rotation, targetRotation)* rotSpeed);
}
}

用单例类来管理路点

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathManager : MonoBehaviour {
//用单例类来管理路点
public static PathManager instanse = null;
public static PathManager Instanse
{
get
{
return instanse;
}
}
int index = ;
void Awake()
{
instanse = this;
}
// Use this for initialization
void Start () {
//transform.GetChild(1);
}
// Update is called once per frame
void Update () { }
public Vector3 GetCurrentPos()
{
return transform.GetChild(index).position;
}
public bool IsReached(Vector3 targetPos)
{
Vector3 currPos = GetCurrentPos();
//忽略路点的高度
currPos.y = targetPos.y;
//根据步径调整0.5f的值
return Vector3.Distance(currPos, targetPos) < 0.5f;
}
public void MoveNext()
{
index++;
//index = index % transform.childCount;
index %= transform.childCount;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuaternionTest : MonoBehaviour
{
public Transform girl;
// Use this for initialization
void Start()
{
//把旋转量赋值给transform.rotation
transform.rotation = new Quaternion(, Mathf.Sin( * Mathf.Deg2Rad), , Mathf.Cos( * Mathf.Deg2Rad));
//用乘法表示旋转量叠加
transform.rotation *= new Quaternion(, Mathf.Sin( * Mathf.Deg2Rad), , Mathf.Cos( * Mathf.Deg2Rad)); //API:Quaternion.AngleAxis,输入轴角对儿,返回四元数,如绕着y轴旋转90度
transform.rotation = Quaternion.AngleAxis(, Vector3.up);
//用乘法表示旋转量叠加,先绕着y轴旋转45度,再绕着y轴旋转90度,结果是-225(135)
transform.rotation = Quaternion.AngleAxis(, Vector3.up) * Quaternion.AngleAxis(, Vector3.up); //四元数不满足乘法交换律
//先绕着y轴旋转45度,再绕着x轴旋转45度,结果是(45,45,0)
transform.rotation = Quaternion.AngleAxis(, Vector3.up) * Quaternion.AngleAxis(, Vector3.right);
//先绕着x轴旋转45度,再绕着y轴旋转45度
transform.rotation = Quaternion.AngleAxis(, Vector3.right) * Quaternion.AngleAxis(, Vector3.up);
}
// Update is called once per frame
void Update()
{
//API:Quaternion LookRotation(Vector3 forward);
Vector3 dir = girl.position - transform.position;
//transform.rotation = Quaternion.LookRotation(dir);
//目标位置
Quaternion targetRotation = Quaternion.LookRotation(dir);
//Slerp球形插值,每次0.01f慢慢插
//transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 0.01f);
//
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, / Quaternion.Angle(transform.rotation, targetRotation));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public float moveSpeed = ;
public float rotSpeed = ;
public float radius = ;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 targetPos = PathManager.Instanse.GetCurrentPos();
Vector3 moveDir = targetPos - transform.position;
moveDir.y = ;
//目标与主角y值一致
targetPos.y = transform.position.y;
//移动
transform.position = Vector3.Lerp(transform.position, targetPos, / Vector3.Distance(transform.position, targetPos) * moveSpeed);
//旋转
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDir), / Quaternion.Angle(transform.rotation, Quaternion.LookRotation(moveDir)) * rotSpeed);
//画线
Debug.DrawLine(transform.position, targetPos, Color.green);
//判断是否达到
if (PathManager.Instanse.IsReached(transform.position))
{
PathManager.Instanse.MoveNext();
}
}
private void OnDrawGizmos()
{
Gizmos.color = new Color(, , , 0.3f);
Gizmos.DrawSphere(transform.position, radius);
}
}

补充内容

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class w06d3 : MonoBehaviour {
public Transform target;
public Transform cam;
public Vector3 camerPosOffset;
public float hOffset;
public float moveSpeed = ;
// 1、向量加法的几何意义
// 2、向量减法的几何意义
// 3、向量的点乘的几何意义
// a.b = a.x * b.x + a.y * b.y + a.z * b.z
// a.b = |a| * |b| * cos<a, b>
// a.b = (|b| * cos<a, b>) * |a|
// a.b = (|a| * cos<a, b>) * |b| a向量在b向量上的投影长度 * b向量的模长
// b是单位向量的话, a向量在b向量上的投影长度
// a、b 都是单位向量的话,两个向量的夹角的余弦值
void Start () { if( Vector3.Angle(transform.forward, target.position - transform.position) < )
{
}
} void Update () {
cam.position = transform.position + camerPosOffset;
cam.LookAt(transform.position + Vector3.up * hOffset);
transform.Translate((target.position - transform.position).normalized * moveSpeed * Time.deltaTime, Space.World); }
private void OnTriggerEnter(Collider other)
{ }
private void OnCollisionEnter(Collision collision)
{ }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pool<T> where T : UnityEngine.Object
{
public readonly int Capacity = ;
List<T> items = new List<T>();
public Pool(int capacity = )
{
this.Capacity = capacity;
}
public bool Push(T item)
{
if (items.Count >= Capacity - ) return false;
items.Add(item); return true;
}
public T Pop()
{
if (items.Count == ) return default(T);
T item = items[items.Count - ];
items.RemoveAt(items.Count - );
return item;
}
}

Unity3D学习笔记(七):叉乘和四元素的更多相关文章

  1. Unity3D学习笔记(八):四元素和书籍推荐

    书籍推荐: 3D数学基础:图形与游戏开发——游戏软件开发专家系列(美)邓恩 Unity Shader入门精要 冯乐乐(92年) 数据结构(Python语言描述) 数据结构.算法与应用(C++语言描述) ...

  2. Typescript 学习笔记七:泛型

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  3. iOS 学习笔记七 【博爱手把手教你使用2016年gitHub Mac客户端】

    iOS 学习笔记七 [博爱手把手教你使用gitHub客户端] 第一步:首先下载git客户端 链接:https://desktop.github.com 第二步:fork 大神的代码[这里以我的代码为例 ...

  4. Linux学习笔记(七) 查询系统

    1.查看命令 (1)man 可以使用 man 命令名称 命令查看某个命令的详细用法,其显示的内容如下: NAME:命令名称 SYNOPSIS:语法 DESCRIPTION:说明 OPTIONS:选项 ...

  5. go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer)

    目录 go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer) demo demo server demo client 池 dao service p2c ro ...

  6. Java IO学习笔记七:多路复用从单线程到多线程

    作者:Grey 原文地址:Java IO学习笔记七:多路复用从单线程到多线程 在前面提到的多路复用的服务端代码中, 我们在处理读数据的同时,也处理了写事件: public void readHandl ...

  7. Unity3D学习笔记2——绘制一个带纹理的面

    目录 1. 概述 2. 详论 2.1. 网格(Mesh) 2.1.1. 顶点 2.1.2. 顶点索引 2.2. 材质(Material) 2.2.1. 创建材质 2.2.2. 使用材质 2.3. 光照 ...

  8. Unity3D学习笔记8——GPU实例化(3)

    目录 1. 概述 2. 详论 2.1. 自动实例化 2.2. MaterialPropertyBlock 3. 参考 1. 概述 在前两篇文章<Unity3D学习笔记6--GPU实例化(1)&g ...

  9. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  10. Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...

随机推荐

  1. 订阅号助手App发布 手机也能管理公众号了

    盼着许久的微信订阅号助手app终于发布了!“ 微信团队发布「订阅号助手」App,支持公众号运营者在手机上发表内容.查看和回复消息.管理已关注用户和帐号.暂时只支持iOS平台,Android平台敬请期待 ...

  2. 全套 AR 应用设计攻略都在这里!

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/jILRvRTrc/article/details/79823908 通过将虚拟内容与现实世界融合,增 ...

  3. .NET Core 2.0 官方下载地址及中文教程

    开发.net core 应用需要安装.NET Core 2.0 SDK http://www.microsoft.com/net/download/core#/sdk 电脑上运行 .net core ...

  4. 教你在Android手机上使用全局代理

    前言:在Android上使用系统自带的代理,限制灰常大,仅支持系统自带的浏览器.这样像QQ.飞信.微博等这些单独的App都不能使用系统的代理.如何让所有软件都能正常代理呢?ProxyDroid这个软件 ...

  5. Ubuntu单用户模式(安全模式)

           说下我遇到的情况,ubuntu服务器,防火墙关闭,连的外网.服务器中毒,病毒自动生成用户,然后病毒进程开启启动,进程启动后,cpu立马占满,服务器立马卡死,本想着服务器启动后通过top命 ...

  6. 【Cocos2dx 3.3 Lua】SpriteBatchNode和SpriteFrameCache使用

    精灵帧缓存类 一.SpriteFrameCache     精灵帧缓冲类SpriteFrameCache用于存储精灵帧,SpriteFrameCache是一个单例模式,不属于某一个精灵,是所有精灵共享 ...

  7. python 多线程小练习

    需求:有100个数据,启动5个线程,每个线程分20个数据,怎么把这20个数据分别传给每个线程. 1. 利用多线程实现 import threading nums = list(range(100)) ...

  8. C/S模型之命名管道

    说明:利用管道实现服务端与客户端之间的交互.效果等同于利用socket. 命名管道(NamedPipe)是一种简单的进程间通信(IPC)机制,是服务器进程和一个或多个客户进程之间通信的单向或双向管道. ...

  9. memcache 基础原理

    memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要频繁访问数据库的网站访问速 ...

  10. linux基础命令---mswap

    mkswap 在Linux设备或者文件中创建交换分区,创建完成之后必须使用swapon来使用它.一般在“/etc/fstab”中有一个交换分区列表,这样开机的时候就可以使用它. 此命令的适用范围:Re ...