Unity3D学习笔记(七):叉乘和四元素
生活随笔
收集整理的這篇文章主要介紹了
Unity3D学习笔记(七):叉乘和四元素
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
向量的叉乘: 數(shù)學(xué)運(yùn)算:a(ax,ay,az) x b(bx,by,bz) = c(aybz-azby,azbx-axby,axby-aybx) 幾何意義:得到一個(gè)新的向量,同時(shí)垂直于a向量和b向量,垂直于ab向量所組成的平面,c向量是ab平面的法向量 左手螺旋定則:四指指向a,握向b,大拇指指向c 作用 1、求順指針,逆時(shí)針關(guān)系(y>0,y<0) 2、求平面的法向量 四元數(shù)(威廉·哈密頓): 萬向節(jié)死鎖(Gimbal Lock): https://v.qq.com/x/cover/e055516g79w/e055516g79w.html http://www.cnitblog.com/luckydmz/archive/2010/09/07/68674.html,最全 四元數(shù)和歐拉角的優(yōu)缺點(diǎn): 歐拉角缺點(diǎn):萬向節(jié)死鎖 http://v.youku.com/v_show/id_XNzkyOTIyMTI=.html 四元數(shù):xyzw四個(gè)分量,w為實(shí)部 超復(fù)數(shù):是由一個(gè)實(shí)部 + 三個(gè)虛部組成的,如4 + 2i + 3j + 4k 復(fù)數(shù):實(shí)部 + 虛部,如3 + 2i,5 - 3i。復(fù)數(shù)的實(shí)部為零,即為虛數(shù);虛部為零,即為實(shí)數(shù)。 ----虛數(shù):如果一個(gè)數(shù)的平方等于負(fù)一,那么這個(gè)數(shù)就是虛數(shù)單位,如x^2 = -1,3i,10k,9j ----實(shí)數(shù):有理數(shù)和無理數(shù)的集合 --------有理數(shù):有限的或者無限循環(huán)的,如1/3 --------無理數(shù):無限不循環(huán)的小數(shù),如PI,e,根號(hào)2 四元數(shù)中存儲(chǔ)的是軸角對(duì)兒<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軸旋轉(zhuǎn)90度:Quaternion(0,0.707,0,0.707) 1、我們用乘法來表示四元數(shù)的旋轉(zhuǎn)量的疊加 歐拉角和四元數(shù)互轉(zhuǎn) 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是目標(biāo)位置 t:在from到to之間插值 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LerpTest : MonoBehaviour
{public Transform sphere;// Use this for initializationvoid Start(){}// Update is called once per framevoid Update(){//API:Vector3.Lerp,線性插值,起始位置from,目標(biāo)位置to,每次插剩余距離(from-to)的百分之多少ttransform.position = Vector3.Lerp(transform.position, sphere.position, 0.05f);//Lerp的勻速移動(dòng)transform.position = Vector3.Lerp(transform.position, sphere.position, 1/Vector3.Distance(transform.position,sphere.position) * 0.05F);//Lerp的勻速旋轉(zhuǎn)//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 initializationvoid 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 framevoid Update () {}public void OnGUI(){if (result1 > 0){if (result2 > 0){GUILayout.Label("玩家在我的方位:右前方");}else{GUILayout.Label("玩家在我的方位:左前方");}}else{if (result2 > 0){GUILayout.Label("玩家在我的方位:右后方");}else{GUILayout.Label("玩家在我的方位:左后方");}}} }路點(diǎn)移動(dòng)
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 = 0;// Use this for initializationvoid Start () {}// Update is called once per framevoid 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,1/ Quaternion.Angle(transform.rotation, targetRotation)* rotSpeed);} }用單例類來管理路點(diǎn)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PathManager : MonoBehaviour {//用單例類來管理路點(diǎn)public static PathManager instanse = null;public static PathManager Instanse{get{return instanse;}}int index = 0;void Awake(){instanse = this;}// Use this for initializationvoid Start () {//transform.GetChild(1); }// Update is called once per framevoid Update () {}public Vector3 GetCurrentPos(){return transform.GetChild(index).position;}public bool IsReached(Vector3 targetPos){Vector3 currPos = GetCurrentPos();//忽略路點(diǎn)的高度currPos.y = targetPos.y;//根據(jù)步徑調(diào)整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 initializationvoid Start(){//把旋轉(zhuǎn)量賦值給transform.rotationtransform.rotation = new Quaternion(0, Mathf.Sin(45 * Mathf.Deg2Rad), 0, Mathf.Cos(45 * Mathf.Deg2Rad));//用乘法表示旋轉(zhuǎn)量疊加transform.rotation *= new Quaternion(0, Mathf.Sin(45 * Mathf.Deg2Rad), 0, Mathf.Cos(45 * Mathf.Deg2Rad));//API:Quaternion.AngleAxis,輸入軸角對(duì)兒,返回四元數(shù),如繞著y軸旋轉(zhuǎn)90度transform.rotation = Quaternion.AngleAxis(90, Vector3.up);//用乘法表示旋轉(zhuǎn)量疊加,先繞著y軸旋轉(zhuǎn)45度,再繞著y軸旋轉(zhuǎn)90度,結(jié)果是-225(135)transform.rotation = Quaternion.AngleAxis(45, Vector3.up) * Quaternion.AngleAxis(90, Vector3.up);//四元數(shù)不滿足乘法交換律//先繞著y軸旋轉(zhuǎn)45度,再繞著x軸旋轉(zhuǎn)45度,結(jié)果是(45,45,0)transform.rotation = Quaternion.AngleAxis(45, Vector3.up) * Quaternion.AngleAxis(45, Vector3.right);//先繞著x軸旋轉(zhuǎn)45度,再繞著y軸旋轉(zhuǎn)45度transform.rotation = Quaternion.AngleAxis(45, Vector3.right) * Quaternion.AngleAxis(45, Vector3.up);}// Update is called once per framevoid Update(){//API:Quaternion LookRotation(Vector3 forward);Vector3 dir = girl.position - transform.position;//transform.rotation = Quaternion.LookRotation(dir);//目標(biāo)位置Quaternion targetRotation = Quaternion.LookRotation(dir);//Slerp球形插值,每次0.01f慢慢插//transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 0.01f);// transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 1 / Quaternion.Angle(transform.rotation, targetRotation));} } using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerControl : MonoBehaviour {public float moveSpeed = 1;public float rotSpeed = 1;public float radius = 5;// Use this for initializationvoid Start(){}// Update is called once per framevoid Update(){Vector3 targetPos = PathManager.Instanse.GetCurrentPos();Vector3 moveDir = targetPos - transform.position;moveDir.y = 0;//目標(biāo)與主角y值一致targetPos.y = transform.position.y;//移動(dòng)transform.position = Vector3.Lerp(transform.position, targetPos, 1 / Vector3.Distance(transform.position, targetPos) * moveSpeed);//旋轉(zhuǎn)transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDir), 1 / Quaternion.Angle(transform.rotation, Quaternion.LookRotation(moveDir)) * rotSpeed);//畫線 Debug.DrawLine(transform.position, targetPos, Color.green);//判斷是否達(dá)到if (PathManager.Instanse.IsReached(transform.position)){PathManager.Instanse.MoveNext();}}private void OnDrawGizmos(){Gizmos.color = new Color(0, 0, 1, 0.3f);Gizmos.DrawSphere(transform.position, radius);} }補(bǔ)充內(nèi)容
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 = 5;// 1、向量加法的幾何意義// 2、向量減法的幾何意義// 3、向量的點(diǎn)乘的幾何意義// 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 都是單位向量的話,兩個(gè)向量的夾角的余弦值 void Start () {if( Vector3.Angle(transform.forward, target.position - transform.position) < 90 ){}}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 = 50;List<T> items = new List<T>();public Pool(int capacity = 50){this.Capacity = capacity;}public bool Push(T item){if (items.Count >= Capacity - 1) return false;items.Add(item); return true;}public T Pop(){if (items.Count == 0) return default(T);T item = items[items.Count - 1];items.RemoveAt(items.Count - 1);return item;} }?
轉(zhuǎn)載于:https://www.cnblogs.com/cnwuchao/p/10362875.html
總結(jié)
以上是生活随笔為你收集整理的Unity3D学习笔记(七):叉乘和四元素的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019年美国大学生数学建模竞赛(MCM
- 下一篇: JNI 学习笔记