스칼라:
벡터:
크기와 방향을 가집니다.
표현: 화살표로 표시하며, 시작점 없이 방향과 크기만으로 나타냅니다.
예시: 중력, 바람의 세기와 방향 등.
벡터는 목적지 - 시작점의 계산 결과입니다:
벡터 곱셈:
벡터 덧셈과 뺄셈:
교환 법칙:
결합 법칙:
벡터의 크기(길이)는 피타고라스 정리를 사용:
정규화 (Normalization):
struct Vector {
Vector() {}
Vector(float x, float y) : x(x), y(y) {}
Vector operator+(const Vector& other) {
Vector ret;
ret.x = x + other.x;
ret.y = y + other.y;
return ret;
}
Vector operator-(const Vector& other) {
Vector ret;
ret.x = x - other.x;
ret.y = y - other.y;
return ret;
}
Vector operator*(float value) {
Vector ret;
ret.x = x * value;
ret.y = y * value;
return ret;
}
void operator+=(const Vector& other) {
x += other.x;
y += other.y;
}
float Length() {
return ::sqrt(x * x + y * y);
}
void Normalize() {
float length = Length();
if (length < 0.00000001f)
return;
x /= length;
y /= length;
}
float x = 0;
float y = 0;
};
기능별 설명:
1. 생성자:
Vector(float x, float y)를 통해 초기화.연산자 오버로딩:
벡터 길이 계산:
Length: ( )을 계산.정규화:
Normalize: 벡터를 단위 벡터로 변환.void Missile::Update() {
float deltaTime = GET_SINGLE(TimeManager)->GetDeltaTime();
if (_target == nullptr) {
_pos.x += _stat.speed * deltaTime * ::cos(_angle);
_pos.y -= _stat.speed * deltaTime * ::sin(_angle);
_sumTime += deltaTime;
if (_sumTime >= 0.2f) {
const vector<Object*>& objects = GET_SINGLE(ObjectManager)->GetObjects();
for (Object* object : objects) {
if (object->GetObjectType() == ObjectType::Monster) {
_target = object;
break;
}
}
}
} else {
Vector dir = _target->GetPos() - GetPos();
dir.Normalize();
_pos += dir * deltaTime * _stat.speed;
}
const vector<Object*>& objects = GET_SINGLE(ObjectManager)->GetObjects();
for (Object* object : objects) {
if (object == this || object->GetObjectType() != ObjectType::Monster)
continue;
Pos dir = object->GetPos() - GetPos();
if (dir.Length() < 25) {
GET_SINGLE(ObjectManager)->Remove(object);
GET_SINGLE(ObjectManager)->Remove(this);
return;
}
}
}
상세 분석:
1. 초기 이동:
유도 미사일 활성화:
_sumTime이 ( 0.2 )초 이상이면 타겟 검색.추적:
Vector dir = _target->GetPos() - GetPos();:dir.Normalize();:_pos += dir * deltaTime * _stat.speed;:충돌 처리: