-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrigidentity.cpp
More file actions
74 lines (61 loc) · 1.81 KB
/
rigidentity.cpp
File metadata and controls
74 lines (61 loc) · 1.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
#include "rigidentity.h"
#include <math.h>
//using namespace std;
int RigidEntity::Update(float deltaTime) {
float time = deltaTime;
acceleration.x = (force.x / mass);
acceleration.y = ((force.y - gravity * mass) / mass);
force.x = 0;
force.y = 0;
velocity += acceleration * time * 200;
Vector delta = velocity * time;
testMove(delta);
return 0;
}
RigidEntity* RigidEntity::Clone() {
Transform t = transform;
return RigidEntity::Clone(t);
}
RigidEntity* RigidEntity::Clone(Transform t) {
RigidEntity *cloneEntity = new RigidEntity;
cloneEntity->Create(this->getSprite(), t);
cloneEntity->setHitbox(this->hitbox.x, this->hitbox.y, this->hitbox.w, this->hitbox.h);
cloneEntity->setClone();
return cloneEntity;
}
int RigidEntity::setHitbox(int x, int y, int w, int h) {
hitbox.x = x;
hitbox.y = y;
hitbox.w = w;
hitbox.h = h;
return 0;
}
int RigidEntity::addForce(Vector forceVec) {
force.x += forceVec.x;
force.y += forceVec.y;
return 0;
}
int RigidEntity::addForceX(float xForce) {
force.x += xForce;
return 0;
}
int RigidEntity::addForceY(float yForce) {
force.y += yForce;
return 0;
}
int RigidEntity::testMove(Vector delta) {
for (auto element : entityTracker) {
if (element.first == Entity::id) continue;
if (dynamic_cast<RigidEntity*>(element.second)) {
RigidEntity* rigidEntity = dynamic_cast<RigidEntity*>(element.second);
SDL_Rect dst1 = { (int)(transform.position.x + hitbox.x + delta.x), (int)(transform.position.y + hitbox.y + delta.y), hitbox.w, hitbox.h };
SDL_Rect dst2 = { (int)rigidEntity->transform.position.x + rigidEntity->hitbox.x, (int)rigidEntity->transform.position.y + rigidEntity->hitbox.y, rigidEntity->hitbox.w, rigidEntity->hitbox.h };
if (SDL_HasIntersection(&dst1, &dst2)) {
velocity = (0, 0);
return 1;
}
}
}
transform.position += delta;
return 0;
}