-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision.hpp
103 lines (81 loc) · 1.85 KB
/
Collision.hpp
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
#ifndef COLLISION_HPP_INCLUDED
#define COLLISION_HPP_INCLUDED
#include <SFML/System/Vector2.hpp>
#include <memory>
#include <cassert>
enum class Type
{
Aabb,
Circle,
};
class Shape
{
public:
virtual ~Shape() = default;
Type getType() const;
protected:
Shape(Type type);
private:
const Type mType;
};
class Circle : public Shape
{
public:
Circle(float radius);
float getRadius() const;
void setRadius(float radius);
private:
float mRadius;
};
class Aabb : public Shape
{
public:
Aabb(sf::Vector2f size);
Aabb(float width, float height);
sf::Vector2f getSize() const;
void setSize(sf::Vector2f size);
void setSize(float width, float height);
private:
sf::Vector2f mSize;
};
class Ray
{
public:
Ray(sf::Vector2f direction, float length);
Ray(float x, float y, float length);
sf::Vector2f getDirection() const;
void setDirection(sf::Vector2f direction);
void setDirection(float x, float y);
float getLength() const;
void setLength(float length);
private:
sf::Vector2f mDirection;
float mLength;
};
struct Manifold
{
Manifold();
bool colliding;
sf::Vector2f normal;
sf::Vector2f contact;
float depth;
};
struct Raycast
{
Raycast();
bool hit;
sf::Vector2f normal;
sf::Vector2f contact;
float t;
};
Manifold solve(const Shape& a, sf::Vector2f posA, const Shape& b,
sf::Vector2f posB);
Raycast solve(const Ray& a, sf::Vector2f posA, const Shape& b,
sf::Vector2f posB);
template<typename T>
const T& castShape(const Shape& shape)
{
assert(dynamic_cast<const T*>(&shape) == &shape);
return *static_cast<const T*>(&shape);
}
#endif // COLLISION_HPP_INCLUDED