unity中向量叉乘 点乘的点乘和叉乘的区别

1719人阅读
Unity3D(47)
[Unity3D学习]3D数学基础之向量
向量是2D、3D数学研究的标准工具,在3D游戏中向量是基础。
1、向量的数学定义
2、向量的几何意义
3、向量与点
4、点和向量的关系:任意一点都能用 从原点开始的向量来表达。
二、向量运算
3、向量大小(长度或模)
4、标量与向量的乘法
5、标准化向量
6、向量的加法和减法
7、距离公式
8、向量点乘
9、向量投影
10、向量叉乘
转载:/archives/347.html
Dot Product 点乘
The dot product takes two vectors and returns a scalar. This scalar is equal to the magnitudes of the two vectors multiplied together and the result multiplied by the cosine of the angle between the vectors. When both vectors are normalized, the cosine essentially
states how far the first vector extends in the second's direction (or vice-versa - the order of the parameters doesn't matter).
两个向量想成的结果是一个标量。此标是等于两个向量长度相乘结果乘以向量之间的夹角的余弦。当两个向量都为单位向量时,余弦的定义就表示为第一个向量在第二个向量上面的射影长度(或反之亦然 - 参数的顺序并不重要) 。
It is easy enough to think in terms of angles and then find the corresponding cosines using a calculator. However, it is useful to get an intuitive understanding of some of the main cosine values as shown in the diagram below:-
容易想像,在角度方面可以通过计算器计算余弦值。然而,下面图标中的一些主要的余弦值是会经常用到的。
The dot product is a very simple operation that can be used in place of the Mathf.Cos function or the vector magnitude operation in some circumstances (it doesn't do exactly the same thing but sometimes the effect is equivalent). However, calculating the dot
product function takes much less CPU time and so it can be a valuable optimization.
点程是一个非常简单的操作,可以在需要矢量长度操作的地方用Mathf.Cos功能(它不会做同样的事情,但有时效果是等价的)。尽管如此,计算点乘功能需要CPU时间要少得多,因此它可以是一个有价值的优化。
Cross Product 叉乘
The other operations are defined for 2D and 3D vectors and indeed vectors with any number of dimensions. The cross product, by contrast, is only meaningful for 3D vectors. It takes two vectors as input and returns another vector as its result.
其他操作被定义为二维和三维向量和任意维数向量。叉乘只能用来计算3D向量,它需要输入两个向量返回结果是另一个向量。
The result vector is perpendicular to the two input vectors. The &left hand rule& can be used to remember the direction of the output vector from the ordering of the input vectors. If the first parameter is matched up to the thumb of the hand and the second
parameter to the forefinger, then the result will point in the direction of the middle finger. If the order of the parameters is reversed then the resulting vector will point in the exact opposite direction but will have the same magnitude.
得到的结果垂直于输入的两个向量。&左手坐标系&可以用来表示输入和输出的向量的方向。如果第一个参数匹配手的拇指和食指匹配第二个参数,结果将是中指的方向。如果参数的顺序是相反的结果向量将指向正好相反的方向,但将有相同长度。
The magnitude of the result is equal to the magnitudes of the input vectors multiplied together and then that value multiplied by the sine of the angle between them. Some useful values of the sine function are shown below:-
结果的大小等于输入向量的乘积,然后通过它们之间的角度的正弦值乘以该值的大小。
The cross product can seem complicated since it combines several useful pieces of information in its return value. However, like the dot product, it is very efficient mathematically and can be used to optimize code that would otherwise depend on slow transcendental
functions.
叉乘比较复杂,因为在其返回值结合了一些有用的信息。但是像点乘那样,这些非常有效的数学可以用来优化代码,否则将依赖于缓慢的超越函数。
转载:/Manual/UnderstandingVectorArithmetic.html
一个向量在另一个向量方向的投影量值
汽车的里程表通常按车轮的旋转速度计算。汽车可能不是直接向前移动(如发生侧滑),这种情况下,部分运动是里程表无法测量的。对象 rigidbody.velocity 向量的量值使速度以总体运动方向为方向,但会脱离前进方向上的速度,此时应使用点积:-
var fwdSpeed = Vector3.Dot(rigidbody.velocity, transform.forward);
当然,方向可以是您喜欢的任意方向,但方向向量应始终设为单位向量,以便进行计算。不仅结果比速度向量更准确,而且无需减缓平方根运算来计算量值。
在做rpg类游戏的过程中,经常遇到要判断周围怪物相对自身的方位
1.判断目标在自己的前后方位可以使用下面的方法:
& &Vector3.Dot(transform.forward, target.position)
& & & &返回值为正时,目标在自己的前方,反之在自己的后方
2.判断目标在机子的左右方位可以使用下面的方法:
& &Vector3.Cross(transform.forward, target.position).y
& & & 返回值为正时,目标在自己的右方,反之在自己的左方
3.在这里顺便解说下关于空间向量的点积和叉积:
& 点积的计算方式为: &a·b=|a|·|b|cos&a,b& &其中|a|和|b|表示向量的模,&a,b&表示两个向量的夹角。另外在 点积 中,&a,b&和&b,a& 夹角是不分顺序的。&
& 所以通过点积,我们其实是可以计算两个向量的夹角的。&
& 另外通过点积的计算我们可以简单粗略的判断当前物体是否朝向另外一个物体: 只需要计算当前物体的transform.forward向量与 otherObj.transform.position 的点积即可, 大于0则在前方,否则在后方。
& 叉积的定义: c =a x b &其中a,b,c均为向量。即两个向量的叉积得到的还是向量!&
& 性质1: c⊥a,c⊥b,即向量c垂直与向量a,b所在的平面 。&
& 性质2: 模长|c|=|a||b|sin&a,b&&
& 性质3: 满足右手法则 。从这点我们有axb ≠ bxa,而axb = – bxa。所以我们可以使用叉积的正负值来判断向量a,b的相对位置,即向量b是处于向量a的顺时针方向还是逆时针方向
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:147603次
积分:2035
积分:2035
排名:第16199名
转载:305篇
(1)(3)(2)(5)(1)(4)(9)(8)(12)(4)(12)(6)(9)(10)(20)(10)(16)(27)(9)(30)(30)(34)(41)(3)(1)(4)(2)在Unity3D中,&Vector3.Dot&表示求两个向量的点积;&Vector3.Cross&表示求两个向量的叉积。&& 点积计算的结果为数值,而叉积计算的结果为向量。两者要注意区别开来。&& 在几何数学中:
&&&1.点积&& 点积的计算方式为:&&a&b=|a|&|b|cos&a,b&&&其中|a|和|b|表示向量的模,&a,b&表示两个向量的夹角。另外在&点积&中,&a,b&和&b,a& 夹角是不分顺序的。&& 所以通过点积,我们其实是可以计算两个向量的夹角的。&& 另外通过点积的计算我们可以简单粗略的判断当前物体是否朝向另外一个物体: 只需要计算当前物体的transform.forward向量与 (otherObj.transform.position & transform.position)的点积即可, 大于0则面对,否则则背对着。当然这个计算也会有一点误差,但大致够用。&
&2.叉积&& 叉积的定义:&c =a x b&&其中a,b,c均为向量。即两个向量的叉积得到的还是向量!&& 性质1:&c&a,c&b,即向量c垂直与向量a,b所在的平面&。&& 性质2:&模长|c|=|a||b|sin&a,b&&& 性质3:&满足右手法则&。从这点我们有axb & bxa,而axb = & bxa。所以我们可以使用叉积的正负值来判断向量a,b的相对位置,即向量b是处于向量a的顺时针方向还是逆时针方向。&& 根据上面的性质2,我们也同样的可以计算出两个向量的夹角。&& 下面是示例代码:
using UnityE
using System.C
public class MainScript : MonoBehaviour
private Vector3
private Vector3
void Start ()
//向量的初始化
a = new Vector3 (1, 2, 1);
b = new Vector3 (5, 6, 0);
void OnGUI ()
//点积的返回值
float c = Vector3.Dot (a, b);
//向量a,b的夹角,得到的值为弧度,我们将其转换为角度,便于查看!
float angle = Mathf.Acos (Vector3.Dot (a.normalized, b.normalized)) * Mathf.Rad2D
GUILayout.Label ("向量a,b的点积为:" + c);
GUILayout.Label ("向量a,b的夹角为:" + angle);
//叉积的返回值
Vector3 e = Vector3.Cross (a, b);
Vector3 d = Vector3.Cross (b, a);
//向量a,b的夹角,得到的值为弧度,我们将其转换为角度,便于查看!
angle = Mathf.Asin (Vector3.Distance (Vector3.zero, Vector3.Cross (a.normalized, b.normalized))) * Mathf.Rad2D
GUILayout.Label ("向量axb为:" + e);
GUILayout.Label ("向量bxa为:" + d);
GUILayout.Label ("向量a,b的夹角为:" + angle);
&上面的示例中,我们定义了两个向量a和b。分别求出了他们的点积和叉积,并通过点积和叉积来反过来计算他们的夹角。&& 这里要&说明&的是:&
& 1.&a.normalized&&和&&b.normalized&&表示的是两个向量的单位向量, 因为在公式里,有向量和模的除法,得出来的结果就是单位向量,所以我们这里和后面都直接用单位向量来计算,省去不少麻烦。&& 2.&Mathf.Rad2Deg&表示的是 单位弧度的度数。详情请见作者文章:&&&& 3.通过叉积计算度数是通过公式&|c|=|a||b|sin&a,b&&来逆向求值。|c| 其实就是叉积的模,换句话说,也代表着&Vector3.Distance (Vector3.zero, Vector3.Cross (a.normalized, b.normalized))的值。&& 结果图如下:&
阅读(...) 评论()1537人阅读
Unity游戏开发(16)
四元数和向量相乘
& &Quaternion.Euler(x,y,z) 返回一个绕x轴旋转x度再绕y轴旋转y度再绕z轴旋转z度的Quaternion,因此Quaternion.Euler(0,90,0)返回一个绕y轴旋转90度的旋转操作.
Quaternion作用于Vector3的右乘操作(*)返回一个将向量做旋转操作后的向量.
因此Quaternion.Euler(0,90,0)*Vector3(0.0,0.0,-10)表示将向量Vector3(0.0,0.0,-10)做绕y轴90度旋转后的结果.因该等于Vector3(-10,0,0).
& & & & 两个向量点乘得到一个标量 ,数值等于两个向量长度相乘后再乘以二者夹角的余弦值 。如果两个向量a,b均 为单位 向量 ,那么a.b等于向量b在向量a方向上的投影的长度
点乘后得到的是一个值:
若结果 == o,则 两向量 互垂直 。
若结果 & 0 &,则 两向量夹角大于90°。
若结果 &0 &,则两向量夹角小于 90°。
& & & 两个向量的叉乘得到一个新的向量 ,新向量垂直于原来的两个向量再乘夹角的正弦值&叉乘后得到的还是一个向量
& & & &在unity3D里面。两个向量的点乘所得到的是两个向量的余弦值,也就是-1 到1之间,0表示垂直,-1表示相反,1表示相同方向。两个向量的叉乘所得到的是两个向量所组成的面的垂直向量,分两个方向。
简单的说,点乘判断角度,叉乘判断方向。 形象的说当一个敌人在你身后的时候,叉乘可以判断你是往左转还是往右转更好的转向敌人,点乘得到你当前的面朝向的方向和你到敌人的方向的所成的角度大小。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:7761次
排名:千里之外
原创:11篇
转载:11篇
(1)(3)(3)(3)(1)(1)(3)(7)向量的点乘和叉乘,以及几何意义_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
向量的点乘和叉乘,以及几何意义
上传于|0|0|暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢

我要回帖

更多关于 unity 点乘和叉乘 的文章

 

随机推荐