ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [DirectX] 2-2. Vector3 및 Vector2 클래스 제작
    Game Development/DirectX 2024. 6. 8. 05:50

    이번 글에서는 저번 글에서 정리했던 벡터 연산들을 Vector 클래스를 통해서 구현해 보도록 하겠습니다.

     

    먼저 3차원 벡터 클래스인 Vector3 클래스부터 제작하도록 하겠습니다.

    구현할 내용들 저번 글에서 정리한 내용들과 크게 다르지 않습니다.

    // Vector3.h
    
    #pragma once
    #include <math.h>
    
    class Vector3 {
    public:
    	// 각 축에 대한 크기
    	float x, y, z;
    
    	// (0, 0, 0)
    	static const Vector3 zero;
    	// (1, 1, 1)
    	static const Vector3 one;
    	// (0, 0, 1)
    	static const Vector3 forward;
    	// (0, 1, 0)
    	static const Vector3 up;
    	// (1, 0, 0)
    	static const Vector3 right;
    
    	Vector3();
    	Vector3(float x, float y, float z);
    
    	// 벡터의 크기 반환
    	float Length();
    	// 내적
    	float Dot(const Vector3& oth);
    	// 정규화해서 반환
    	Vector3 Normalize();
    	// 외적
    	Vector3 Cross(const Vector3& oth);
    
    	// 대입
    	Vector3& operator=(const Vector3& oth);
    
    	// 벡터 + 벡터
    	Vector3 operator+(const Vector3& oth);
    	Vector3& operator+=(const Vector3& oth);
    	
    	// 벡터 - 벡터
    	Vector3 operator-(const Vector3& oth);
    	Vector3& operator-=(const Vector3& oth);
    
    	// 벡터 * 스칼라
    	Vector3 operator*(const float& oth);
    	Vector3& operator*=(const float& oth);
    
    	// 벡터 / 스칼라
    	Vector3 operator/(const float& oth);
    	Vector3& operator/=(const float& oth);
    
    };
    // Vector3.cpp
    
    #include "Vector3.h"
    
    const Vector3 Vector3::zero(0, 0, 0);
    const Vector3 Vector3::one(1, 1, 1);
    const Vector3 Vector3::forward(0, 0, 1);
    const Vector3 Vector3::up(0, 1, 0);
    const Vector3 Vector3::right(1, 0, 0);
    
    Vector3::Vector3() : x(0), y(0), z(0) {}
    
    Vector3::Vector3(float x, float y, float z) : x(x), y(y), z(z) {}
    
    float Vector3::Length() {
    	return sqrtf(x * x + y * y + z * z);
    }
    
    float Vector3::Dot(const Vector3& oth) {
    	return (x * oth.x) + (y * oth.y) + (z * oth.z);
    }
    
    Vector3 Vector3::Normalize() {
    	float len = Length();
    	return *this / len;
    }
    
    Vector3 Vector3::Cross(const Vector3& oth) {
    	float _x = y * oth.z - z * oth.y;
    	float _y = z * oth.x - x * oth.z;
    	float _z = x * oth.y - y * oth.x;
    	return Vector3(_x, _y, _z);
    }
    
    Vector3& Vector3::operator=(const Vector3& oth) {
    	x = oth.x;
    	y = oth.y;
    	z = oth.z;
    	return *this;
    }
    
    Vector3 Vector3::operator+(const Vector3& oth) {
    	return Vector3(x + oth.x, y + oth.y, z + oth.z);
    }
    
    Vector3& Vector3::operator+=(const Vector3& oth) {
    	x += oth.x;
    	y += oth.y;
    	z += oth.z;
    	return *this;
    }
    
    Vector3 Vector3::operator-(const Vector3& oth) {
    	return Vector3(x - oth.x, y - oth.y, z - oth.z);
    }
    
    Vector3& Vector3::operator-=(const Vector3& oth) {
    	x -= oth.x;
    	y -= oth.y;
    	z -= oth.z;
    	return *this;
    }
    
    Vector3 Vector3::operator*(const float& oth) {
    	return Vector3(x * oth, y * oth, z * oth);
    }
    
    Vector3& Vector3::operator*=(const float& oth) {
    	x *= oth;
    	y *= oth;
    	z *= oth;
    	return *this;
    }
    
    Vector3 Vector3::operator/(const float& oth) {
    	return Vector3(x / oth, y / oth, z / oth);
    }
    
    Vector3& Vector3::operator/=(const float& oth) {
    	x /= oth;
    	y /= oth;
    	z /= oth;
    	return *this;
    }

     

    이렇게해서 3차원 벡터를 완성했습니다.

    2차원 벡터도 3차원 벡터와 크게 다르지 않습니다.

    하지만 외적은 3차원에서만 되기 때문에 외적이 빠져있습니다.

    // Vector2.h
    
    #pragma once
    #include <math.h>
    
    class Vector2 {
    public:
    	float x, y;
    
    	Vector2();
    	Vector2(float x, float y);
    
    	float Length();
    	float Dot(const Vector2 oth);
    	Vector2 Normalize();
    
    	Vector2& operator=(const Vector2& oth);
    
    	Vector2 operator+(const Vector2& oth);
    	Vector2& operator+=(const Vector2& oth);
    
    	Vector2 operator-(const Vector2& oth);
    	Vector2& operator-=(const Vector2& oth);
    
    	Vector2 operator*(const float& oth);
    	Vector2& operator*=(const float& oth);
    
    	Vector2 operator/(const float& oth);
    	Vector2& operator/=(const float& oth);
    
    };
    // Vector2.cpp
    
    #include "Vector2.h"
    
    Vector2::Vector2() : x(0), y(0) {}
    
    Vector2::Vector2(float x, float y) : x(x), y(y) {}
    
    float Vector2::Length() {
    	return sqrtf(x * x + y * y);
    }
    
    float Vector2::Dot(const Vector2 oth) {
    	return x * oth.x + y * oth.y;
    }
    
    Vector2 Vector2::Normalize() {
    	float len = Length();
    	return *this / len;
    }
    
    Vector2& Vector2::operator=(const Vector2& oth) {
    	x = oth.x;
    	y = oth.y;
    	return *this;
    }
    
    Vector2 Vector2::operator+(const Vector2& oth) {
    	return Vector2(x + oth.x, y + oth.y);
    }
    
    Vector2& Vector2::operator+=(const Vector2& oth) {
    	x += oth.x;
    	y += oth.y;
    	return *this;
    }
    
    Vector2 Vector2::operator-(const Vector2& oth) {
    	return Vector2(x - oth.x, y - oth.y);
    }
    
    Vector2& Vector2::operator-=(const Vector2& oth) {
    	x -= oth.x;
    	y -= oth.y;
    	return *this;
    }
    
    Vector2 Vector2::operator*(const float& oth) {
    	return Vector2(x * oth, y * oth);
    }
    
    Vector2& Vector2::operator*=(const float& oth) {
    	x *= oth;
    	y *= oth;
    	return *this;
    }
    
    Vector2 Vector2::operator/(const float& oth) {
    	return Vector2(x / oth, y / oth);
    }
    
    Vector2& Vector2::operator/=(const float& oth) {
    	x /= oth;
    	y /= oth;
    	return *this;
    }

     

    벡터 클래스들의 내용 자체는 저번 글에서 다 설명했었기 때문에

    저번 글을 잘 이해하셨다면 그렇게 어렵지 않을 것 같습니다.

     

    그래도 벡터 연산이라는 게 배우지 않았으면 좀 어려웠을 수는 있다고 생각합니다.

    당장 저만해도 행렬식 같은 것을 배운 적이 없어서 이해하는데 좀 오래 걸렸으니까요.

     

    그럼 다음 글에서는 Matrix4 클래스를 만들기 위해서 변환 행렬들에 대해서 한번 알아보도록 하겠습니다.

    읽어주셔서 감사합니다.

    'Game Development > DirectX' 카테고리의 다른 글

    [DirectX] 3-2. World 행렬  (0) 2024.06.17
    [DirectX] 3-1. 행렬  (0) 2024.06.17
    [DirectX] 2-1. 벡터  (0) 2024.06.05
    [DirectX] 1. DirectX 초기화 및 배경 색 칠하기  (0) 2024.06.01
    [DirectX] 0. 창 띄우기  (0) 2024.05.31
Designed by Tistory.