-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCHitBox.cpp
More file actions
104 lines (82 loc) · 2.81 KB
/
CHitBox.cpp
File metadata and controls
104 lines (82 loc) · 2.81 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "CHitBox.h"
CHitBox::~CHitBox()
{
entity = nullptr;
}
CHitBox::CHitBox(CEntity * entity, float offsetx, float offsety, float width, float height, bool isSolid)
{
this->entity = entity;
_isSolid = isSolid;
_halfSize = vector3D(width * .5f, height * .5f, 0);
_center = vector3D(offsetx+_halfSize.x, offsety+_halfSize.y, 0);
}
void CHitBox::setSize(vector3D val)
{
_center.x = (_center.x - _halfSize.x) + val.x;
_center.y = (_center.y - _halfSize.y) + val.y;
_halfSize = val * .5f;
}
void CHitBox::setOffSet(vector3D val)
{
_center.x = _halfSize.x + val.x;
_center.y = _halfSize.y + val.y;
}
void CHitBox::solidCollide(CHitBox * other)
{
//Calculate How much to move to get out of collision moving towards last collisionless point
CHitBox * otherbox = other;
if (otherbox->entity != nullptr && entity != nullptr)
{
//Calculate how far in we went
float distx = (other->entity->position.x + otherbox->center().x) - (entity->position.x + _center.x);
//distx = (float)Math.Sqrt(distx * distx);
float disty = (other->entity->position.y + otherbox->center().y) - (entity->position.y + _center.y);
//disty = (float)Math.Sqrt(disty * disty);
float lenx = halfWidth() + other->halfWidth();
float leny = halfHeight() + other->halfHeight();
int px = 1;
int py = 1;
if (otherbox->entity->position.x + otherbox->center().x < entity->position.x + _center.x)
px = -1;
if (otherbox->entity->position.y + otherbox->center().y < entity->position.y + _center.y)
py = -1;
float penx = px*(distx - lenx);
float peny = py*(disty - leny);
//Resolve closest to previous position
float diffx = (entity->position.x + penx) - entity->getOldPosition().x;
diffx *= diffx;
float diffy = (entity->position.y + peny) - entity->getOldPosition().y;
diffy *= diffy;
if (diffx < diffy)
entity->position.x += penx; //TODO: dont make a new vector every time
else if (diffx > diffy)
entity->position.y += peny; //Same here
else
entity->position = vector3D(entity->position.x + penx, entity->position.y + peny, 0); //Corner cases
}
}
void CHitBox::addCollision(CEntity * collider)
{
if (entity)
entity->addCollision(collider);
}
std::string CHitBox::getEntType()
{
if (entity)
return entity->type();
return "__NOENT";
}
bool CHitBox::checkCollision(CHitBox &otherBox)
{
float distance = 0;
float length = 0;
distance = abs((entity->position.x + _center.x) - (otherBox.entity->position.x + otherBox._center.x));
length = _halfSize.x + otherBox._halfSize.x;
if (distance < length)
{
distance = abs((entity->position.y + _center.y) - (otherBox.entity->position.y + otherBox._center.y));
length = _halfSize.y + otherBox._halfSize.y;
return distance < length;
}
return false;
}