Repository navigation
Expand file tree
/
Copy pathCollisionComponent.cpp
More file actions
89 lines (75 loc) · 2.07 KB
/
Copy pathCollisionComponent.cpp
File metadata and controls
89 lines (75 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "CollisionComponent.h"
#include "Actor.h"
#include <algorithm>
#
CollisionComponent::CollisionComponent(class Actor* owner)
: Component(owner)
, mWidth(0.0f)
, mHeight(0.0f)
{
}
CollisionComponent::~CollisionComponent()
{
}
bool CollisionComponent::Intersect(const CollisionComponent* other) const
{
// checks for left, right, top, and bottom intersection
bool noIntersection = this->GetMax().x < other->GetMin().x ||
other->GetMax().x < this->GetMin().x ||
this->GetMax().y < other->GetMin().y ||
other->GetMax().y < this->GetMin().y;
return !noIntersection;
}
Vector2 CollisionComponent::GetMin() const
{
//returns minimum point for hit box
float x = GetCenter().x - (mWidth * mOwner->GetScale()) / 2.0f;
float y = GetCenter().y - (mHeight * mOwner->GetScale()) / 2.0f;
return Vector2(x, y);
}
Vector2 CollisionComponent::GetMax() const
{
//returns maximum point for hit box
float x = GetCenter().x + (mWidth * mOwner->GetScale()) / 2.0f;
float y = GetCenter().y + (mHeight * mOwner->GetScale()) / 2.0f;
return Vector2(x, y);
}
const Vector2& CollisionComponent::GetCenter() const
{
//returns center of hitbox
return mOwner->GetPosition();
}
CollSide CollisionComponent::GetMinOverlap(const CollisionComponent* other, Vector2& offset) const
{
offset = Vector2::Zero;
//no itersections
if (!Intersect(other))
{
return CollSide::None;
}
float topDist = other->GetMin().y - GetMax().y;
float bottomDist = other->GetMax().y - GetMin().y;
float leftDist = other->GetMin().x - GetMax().x;
float rightDist = other->GetMax().x - GetMin().x;
// Determine the minimum overlap side
float minOverlap =
std::min(std::abs(topDist),
std::min(std::abs(bottomDist), std::min(std::abs(leftDist), std::abs(rightDist))));
if (minOverlap == std::abs(topDist))
{
offset.y = topDist;
return CollSide::Top;
}
if (minOverlap == std::abs(bottomDist))
{
offset.y = bottomDist;
return CollSide::Bottom;
}
if (minOverlap == std::abs(leftDist))
{
offset.x = leftDist;
return CollSide::Left;
}
offset.x = rightDist;
return CollSide::Right;
}