Repository navigation
Expand file tree
/
Copy pathcollider.h
More file actions
56 lines (43 loc) · 1.56 KB
/
Copy pathcollider.h
File metadata and controls
56 lines (43 loc) · 1.56 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
#ifndef COLLIDER_H
#define COLLIDER_H
#include <QtCore/qpoint.h>
#include <QtGui/qpainter.h>
#include "Vec2.h"
class Collider
{
public:
virtual ~Collider() = default;
/**
* @return the couple (p_c, n_c) defined in Equations.pdf
*/
virtual std::array<Vec2, 2> getCollisionParameters(Vec2 particlePos, float particleRadius) const = 0;
virtual void render(QPainter *painter, const std::function<Vec2(const Vec2&)>& worldToView) = 0;
};
class PlanCollider: public Collider
{
public:
PlanCollider(Vec2 _position, Vec2 _normal) : position(_position), normal(_normal) {}
~PlanCollider() override = default;
inline std::array<Vec2, 2> getCollisionParameters(Vec2, float) const override {
return {position, normal};
}
void render(QPainter *painter, const std::function<Vec2(const Vec2&)>& worldToView) override;
Vec2 position;
Vec2 normal;
};
class SphereCollider: public Collider
{
public:
SphereCollider(Vec2 _center, float _radius) : center(_center), radius(_radius) {}
~SphereCollider() override = default;
inline std::array<Vec2, 2> getCollisionParameters(Vec2 particlePos, float particleRadius) const override {
float sdf = length(particlePos - this->center) - (radius + particleRadius);
Vec2 n_c = 1/length(particlePos - this->center) * (particlePos - this->center);
Vec2 p_c = particlePos - sdf * n_c;
return {p_c, n_c};
}
void render(QPainter *painter, const std::function<Vec2(const Vec2&)>& worldToView) override;
Vec2 center;
float radius;
};
#endif // COLLIDER_H