-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics.h
More file actions
43 lines (26 loc) · 997 Bytes
/
physics.h
File metadata and controls
43 lines (26 loc) · 997 Bytes
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
#pragma once
#include "glsl_core.h"
#include <cmath>
namespace Physics {
struct AABB {
glsl::vec2 pos;
glsl::vec2 halfSize;
};
inline bool CheckCollision(const AABB& a, const AABB& b) {
float deltaX = std::abs(a.pos.x - b.pos.x);
float deltaY = std::abs(a.pos.y - b.pos.y);
float intersectX = a.halfSize.x + b.halfSize.x;
float intersectY = a.halfSize.y + b.halfSize.y;
return (deltaX < intersectX) && (deltaY < intersectY);
}
inline bool ResolvePaddleBounce(const AABB& ball, const AABB& paddle, glsl::vec2& ballVel) {
if (CheckCollision(ball, paddle)) {
ballVel.x *= -1.0f;
float hitOffset = ball.pos.y - paddle.pos.y;
ballVel.y += hitOffset * 0.05f;
ballVel.x *= 1.05f;
return true;
}
return false;
}
}