點乘和投影,扇形判斷

轉自:https://www.cnblogs.com/vuciao/p/10362823.html

三角函數:

概念:用來描述三角形中某個角和對應的三條邊的比例關係。

正弦:sin<θ>(sin<theta>)=對邊/斜邊

餘弦:cos<θ>(cos<theta>)=鄰邊/斜邊

正切:tan<θ>(tan<theta>)=對邊/鄰邊

 

正弦函數曲線:隨着θ角度不斷增大,sinθ的值的變化週期

 

餘弦函數曲線:正弦函數曲線左移90度

 

反三角函數:已知比例關係,反推出角度或者弧度。

反正弦:Arcsin(0.5)=30度

反餘弦:Arccos

反正切:Arctan

 

向量的乘法:

向量的點乘:a(ax,ay,az) · b(bx.by.bz)

數學運算:a·b = axbx + ayby + azbz,各分量分別相乘再相加

1、滿足乘法交換律

2、結果是標量

幾何意義:

a·b = |a| * |b| * cos<a,b>,<a,b>夾角範圍0~180度,兩個向量夾角越小,相似度越高

a·b = (|a| * cos<a,b>) * |b|,最重要的幾何意義就是求投影

1、a向量在b向量方向上投影的長度再乘以b向量的模長

2、當b向量爲單位向量的時候,結果就是:a向量在b向量方向上投影的長度

3、當a向量和b向量都是單位向量的時候,結果就是:兩個向量的夾角的餘弦

 

敵人的正前方和敵人與自己連線的夾角,小於90度,在敵人前方,大於90度,在敵人後方

API Vector3.Dot(Vector3 lhs,Vector3 rhs),兩個向量點乘滿足乘法交換律,但叉乘不行

API Vector3.Angle,直接計算兩個向量之間的夾角

 

思考題:入射光線和反射光線不在一個水平線上

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyLook : MonoBehaviour
{
    public Transform player;
    public float result;
    public float distance;
    // Use this for initialization
    void Start()
    {
        ////向量點乘的數學運算
        //Vector3 enemy2Player = player.position - transform.position;
        //Vector3 enemyForward = transform.forward;
        //result = enemy2Player.x * enemyForward.x + enemy2Player.y * enemyForward.y + enemy2Player.z + enemyForward.z;
        //把向量變成單位向量
        Vector3 enemy2Player = (player.position - transform.position).normalized;
        Vector3 enemyForward = transform.forward;
        //計算兩個向量之間的夾角
        result = Vector3.Dot(enemy2Player, enemyForward);
        //計算兩個物體之間的距離
        distance = Vector3.Distance(player.position, transform.position);
        //API:計算兩個向量之間的夾角
        //Vector3.Angle();
    }
    // Update is called once per frame
    void Update()
    {
    }
    private void OnGUI()
    {
        //if (result > 0)
        //{
        //    GUILayout.Label("在敵人前方");
        //}
        //else
        //{
        //    GUILayout.Label("在敵人後方");
        //}
        if (result > Mathf.Cos(30 * Mathf.Deg2Rad) && distance < 5)
        {
            GUILayout.Label("在扇形範圍內:" + Mathf.Acos(result) * Mathf.Rad2Deg + "度");
        }
        else
        {
            GUILayout.Label("不在扇形範圍內:" + Mathf.Acos(result) * Mathf.Rad2Deg + "度");
        }
    }
}

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestTrans : MonoBehaviour {
    public Transform Target;
    public float distance;
    private float SkillDistance = 5;//扇形距離
    private float SkillJiaodu = 60;//扇形的角度
    // Use this for initialization
    void Start () {
        ////偶然性編程
        distance = Vector3.Distance(transform.position, Target.position);//距離
        Vector3 norVec = transform.rotation * Vector3.forward;
        Vector3 temVec = Target.position - transform.position;
        Debug.DrawLine(transform.position, norVec, Color.red);//畫出技能釋放者面對的方向向量
        Debug.DrawLine(transform.position, Target.position, Color.green);//畫出技能釋放者與目標點的連線
        float jiajiao = Mathf.Acos(Vector3.Dot(norVec.normalized, temVec.normalized)) * Mathf.Rad2Deg;
        if (distance <= SkillDistance)
        {
            if (jiajiao <= SkillJiaodu)
            {
                Debug.Log("在扇形範圍內");
            }
            else
            {
                Debug.Log("不在扇形範圍內");
            }
        }
    }
       
       // Update is called once per frame
       void Update () {
      
    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CrystalMove : MonoBehaviour {
    //public Transform startTrans;
    //public float moveSpeed = 1;
    //public float rotSpeed = 1;
    public float rotateSpeed = 30;
    //調節上下頻率和浮動
    public float amplitude = 1f;
    public float frequency = 1f;
    // Use this for initialization
    void Start () {
              
       }
       
       // Update is called once per frame
       void Update () {
        //    transform.Rotate(Vector3.up, Space.World);
        //    Vector3 delta = Vector3.up * Mathf.Sin(Time.time) * moveSpeed*0.25F;
        //    transform.position = startTrans.position + delta;
        transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);
        float sin = Mathf.Sin(Time.time * frequency) * amplitude;
        transform.Translate(Vector3.up * sin, Space.World);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章