Unity Vector

김동현·2022년 7월 14일
0
post-thumbnail

가상세계의 수학은 Vector이다.

수치만 가지면 스칼라
자동차의 속력이 스칼라이고
속도는 벡터이다 (방향까지 포함한 데이터)

공간상의 벡터가 어떻게 표현되는지 알아야 벡터를 사용할 수 있다.

벡터의 시점은 정해져 있지 않다.

V = [1, 2] : 2차원 백터
V = [1, 2, 3] : 3차원 백터

벡터 연산

덧셈 (같은 차원만 가능)

V1 = [1, 2]
V2 = [2, 3]

V1 + V2 = [1 + 2, 2 + 3]

교환법칙이 성립한다.

뺄셈 (같은 차원만 가능)

V1 - V2 = [1 - 2, 2 - 3]

시점을 맞춰주어야 계산하기 편하다.

두 정점을 이어주고 앞에있는 백터쪽으로 방향이 결정된다

한 오브젝트에서 다른 오브젝트까지의 거리를 구하는데 사용된다.

스칼라배(Scalar Multiplication)

2 V1 = [1 2, 2 2]
-3
V1 = [1 -3, 2 -3]

음수가 들어오면 방향이 반대가 된다. 꼭 잘 확인하자

내적 (dot product)

두 벡터를 받아 스칼라를 반환
두 벡터의 크기를 곱하고 그 결과에 두 벡터 사이의 각에 대한 코사인을 곱한 것과 같습니다.

A = [1, 2]
B = [2, 3]

A º B = |A||B|con(x)

A º B = 0 => 두 백터가 직각을 이룸
A º B = 1 => 같은 방향을 바라보는 평행
A º B = -1 => 서로 마주보는 평행

외적 (cross product)

3D 벡터에만 해당됨
두개의 3D 벡터를 입력으로 사용하고 다른 3D 벡터를 반환합니다.

법선 벡터
법선 벡터 : 어떤 평면에 대한 수직 벡터

방향만 알고 싶을 때

크기가 1인 백터 => 방향벡터(Directional Vector) => 정규화된 벡터(Nomalized Vector) => 단위 벡터 (Unit Vector)

크기를 1로 만드는 과정을 정규화(Nomalize) 라고 한다

벡터의 크기(magnitude)를 1로 만들고

백터의 크기 : 스칼라값
구하는법 : 피타고라스의 정의를 이용
sqrt(X ^ 2 + Y ^ 2)
3차원이면
sqrt(X ^ 2 + Y ^ 2 + Z ^ 2)
가 된다

삼각함수

삼각함수를 까먹었었으니 다시 배우자

호도법

호도법 : 호의 길이를 이용해서 각도를 표시하는방법

역 삼각함수

각도를 구할 수 있다.
arc sin
arc cos
arc tan

외접 내접 유니티 구현

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DotProductTest : MonoBehaviour
{
    public Transform Target;
    public GameObject MyPlayer;
    Rigidbody ri;

    private float speed = 5f;
    private float movePos = 0.3f;

    void Start()
    {
        ri = GetComponent<Rigidbody>();    
    }

    void Update()
    {
        // 내접
        Vector3 distanceVector = Target.position - transform.position;

        Debug.Log(Vector3.Dot(transform.forward, distanceVector.normalized));

        Debug.DrawRay(transform.position, transform.forward * 10f, Color.black);
        Debug.DrawRay(transform.position, distanceVector, Color.red);

        // ri.AddForce(0, 0, speed);
        // ri.velocity = new Vector3(0, 0, speed);
        // Z 축으로 왕복하는 거
        if (Vector3.Dot(transform.forward, distanceVector.normalized) < -movePos && speed == 5f)
        {
            speed *= -1;
        }
        else if (Vector3.Dot(transform.forward, distanceVector.normalized) > movePos && speed == -5f)
        {
            speed *= -1;
        }

        // 외접 
        Vector3 normalVector = Vector3.Cross(transform.forward, distanceVector.normalized);

        Debug.DrawRay(transform.position, normalVector * 10, Color.green);
    }
}

        normalVector.magnitude; // 벡터의 크기
        normalVector.normalized; // 정규화된 벡터
        normalVector.sqrMagnitude; // 벡터의 크기^2
        normalVector.x;
        normalVector.y;
        normalVector.z;

        normalVector[0] == normalVector.x;
        normalVector[1] == normalVector.y;
        normalVector[2] == normalVector.z;

        normalVector = new Vector3(0, 0, 0); // 새로운 백터 만들기

        Vector3.Angle(); // 두 벡터의 사이각
        Vector3.Cross(); // 외적
        Vector3.Dot(); // 내적
        Vector3.Distance(); // 두 벡터 사이의 거리 == distanceVector.magnitude;
        Vector3.Max(); // 두 벡터 중 큰 크기를 가지는 벡터
        Vector3.Min(); // 두 벡터 중 작은 크기를 가지는 벡터
        Vector3.Normalize(); // 정규화 == vector.normalized;
        Vector3.Project(); // 한 쪽으로 사영한 벡터를 얻는다.
        Vector3.Reflect(); // 입사각 반사각을 얻을 수 있다.
        
                Quaternion.LookRotation(); // 회전에 필요한 값을 생성 // LookAt과는 조금 다름
        Quaternion.Angle(); // 얼마나 틀어졌는지 확인
        Quaternion.AngleAxis(); // 어쩐 축에서 얼마나 틀어져있는지 확인
        Quaternion.FromToRotation();

쿼터니언

오일러각을 사용한 회전은 짐벌락이 걸리기 때문에
유니티의 회전은 쿼터니언을 사용한다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EulerTest : MonoBehaviour
{
    float x;
    float y;
    float z;
    float speed = 5f;

    void Update()
    {
        x += Time.deltaTime * speed;
        y += Time.deltaTime * speed;
        z += Time.deltaTime * speed;
        transform.rotation = Quaternion.Euler(x, y, z);
    }
}
profile
해보자요

0개의 댓글