이전글: 게임 수학 C++ | 1. 기반
https://mpi-devlog.tistory.com/24
게임 수학 C++ | 1. 기반
과제겸 공부겸 게임 수학 관련해서 프로그래밍한 코드 쭉 올립니다 게시글에는 딱히 이론에 대한 이야기는 없으니 먼저 이론을 알고 계셔야 코드 이해하실 수 있을 거예요 지금은 벡터나 행렬 �
mpi-devlog.tistory.com
과제겸 공부겸 게임 수학 관련해서 프로그래밍한 코드 쭉 올립니다
게시글에는 딱히 이론에 대한 이야기는 없으니 먼저 이론을 알고 계셔야 코드 이해하실 수 있을 거예요
여기서 사용한 벡터는 3요소 벡터(x, y, z)입니다.
사실 나중을 위해 4요소 벡터(x, y, z, w)를 써줘야되지만, 지금은 귀찮쿨럭 이대로 하고 시간될 때 수정해보도록 하죠.
(사실 요즘 좀 바빠서...)
한 변수에 종속적이지 않은(?) 연산함수들은 static 함수로 만들었습니다.
이렇게 하면 어느 하나의 벡터 변수(알맞게 쓰면 클래스로 만든 객체지만 변수처럼 사용할거라서)에 특정 계산을 종속시키지 않아도 됩니다.
- Vector.h
- Vector.cpp
Vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include "RootMath.h"
class vec3
{
public:
float x, y, z;
public:
vec3();
vec3(float x, float y, float z);
vec3(float vec[]);
vec3 operator+(const vec3& operand);
vec3 operator-(const vec3& operand);
vec3 operator*(const float& operand); // Scalar multiply
vec3 operator/(const float& operand); // Scalar devide
float GetLength();
public:
static vec3 Normalize(vec3 vec);
static float Dot(vec3 a, vec3 b);
static vec3 Cross(vec3 a, vec3 b);
static vec3 ProjectionAB(vec3 a, vec3 b);
static vec3 VerticalAB(vec3 a, vec3 b);
static float GetAngleAB(vec3 a, vec3 b);
};
#endif
Vector.cpp
#include "Vector.h"
vec3::vec3()
{
x = 0;
y = 0;
z = 0;
}
vec3::vec3(float x, float y, float z)
{
this->x = x;
this->y = y;
this->z = z;
}
vec3::vec3(float vec[])
{
this->x = vec[0];
this->y = vec[1];
this->z = vec[2];
}
vec3 vec3::operator+(const vec3& operand)
{
vec3 result;
result.x = x + operand.x;
result.y = y + operand.y;
result.z = z + operand.z;
return result;
}
vec3 vec3::operator-(const vec3& operand)
{
vec3 result;
result.x = x - operand.x;
result.y = y - operand.y;
result.z = z - operand.z;
return result;
}
vec3 vec3::operator*(const float& operand)
{
vec3 result;
result.x = x * operand;
result.y = y * operand;
result.z = z * operand;
return result;
}
vec3 vec3::operator/(const float& operand)
{
vec3 result;
result.x = x / operand;
result.y = y / operand;
result.z = z / operand;
return result;
}
float vec3::GetLength()
{
return sqrt((x * x) + (y * y) + (z * z));
}
vec3 vec3::Normalize(vec3 vec)
{
float length = vec.GetLength();
if (length == 0)
{
return vec;
}
return (vec / length);
}
float vec3::Dot(vec3 a, vec3 b)
{
float result = (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
return result;
}
vec3 vec3::Cross(vec3 a, vec3 b)
{
vec3 result = vec3((a.y * b.z) - (a.z * b.y), (a.z * b.x) - (a.x * b.z), (a.x * b.y) - (a.y * b.x));
return result;
}
vec3 vec3::ProjectionAB(vec3 a, vec3 b)
{
if (b.GetLength() == 0)
{
return vec3(0, 0, 0);
}
vec3 result = b * (Dot(a, b) / (b.GetLength() * b.GetLength()));
return result;
}
vec3 vec3::VerticalAB(vec3 a, vec3 b)
{
vec3 result = a - ProjectionAB(a, b);
return result;
}
float vec3::GetAngleAB(vec3 a, vec3 b)
{
float result = radToDeg(acosf(Dot(a, b) / (a.GetLength() * b.GetLength())));
return result;
}
다음글: 게임 수학 C++ | 3. 행렬 & 변환
https://mpi-devlog.tistory.com/27
게임 수학 C++ | 3. 행렬 & 변환
이전글: 게임 수학 C++ | 2. 벡터 https://mpi-devlog.tistory.com/25 게임 수학 C++ | 2. 벡터 이전글: 게임 수학 C++ | 1. 기반 https://mpi-devlog.tistory.com/24 게임 수학 C++ | 1. 기반 과제겸 공부겸 게임..
mpi-devlog.tistory.com
'개발 > 게임\그래픽스' 카테고리의 다른 글
게임 수학 C++ | 3. 행렬 & 변환 (0) | 2020.05.23 |
---|---|
게임 수학 C++ | 1. 기반 (0) | 2020.05.22 |
WinAPI_비트맵 실습 (0) | 2019.12.29 |
WinApi 메모장 (0) | 2019.11.08 |
2019_Under the Chamber (0) | 2019.10.26 |