-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallScript.cpp
More file actions
55 lines (36 loc) · 1.52 KB
/
Copy pathBallScript.cpp
File metadata and controls
55 lines (36 loc) · 1.52 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
#include "BallScript.h"
void BallScript::startScript() {
}
void BallScript::tickScript(float deltaTime) {
//cout << "tick ball" << endl;
ComponentHandle<Transform> transform = entity->get<Transform>();
transform->position += currDir * deltaTime / 2.f;
if (transform->position.x < 0 && currDir.x < 0) currDir.x *= -1;
if (transform->position.x > limits.x && currDir.x > 0) currDir.x *= -1;
if (transform->position.y < 0 && currDir.y < 0) currDir.y *= -1;
if (transform->position.y > limits.y && currDir.y > 0) {
currDir = glm::vec2(0.f, 1.f);
transform->position = glm::vec2(400.f, 400.f);
}
CheckCollisions();
}
void BallScript::CheckCollisions() {
ComponentHandle<Transform> transform = entity->get<Transform>();
ComponentHandle<BoxCollider> collider = entity->get<BoxCollider>();
world->each<BoxCollider>([&](Entity* other_ent, ComponentHandle<BoxCollider> other_collider) {
if (other_ent->getEntityId() == entity->getEntityId()) {
return;
}
ComponentHandle<Transform> other_transform = other_ent->get<Transform>();
glm::vec2 p1 = transform->position;
glm::vec2 p2 = other_transform->position;
if (p1.x - collider->width / 2 < p2.x + other_collider->width / 2 &&
p1.x + collider->width / 2 > p2.x - other_collider->width / 2 &&
p1.y - collider->height / 2 < p2.y + other_collider->height / 2 &&
p1.y + collider->height / 2 > p2.y - other_collider->height / 2)
{
currDir = glm::normalize(transform->position - other_transform->position);
other_collider->collidedWith = true;
}
});
}