-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollider.h
84 lines (81 loc) · 2.01 KB
/
Collider.h
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
#pragma once
#include "Vector2.h"
#include "Coords2.h"
#include "AABB2.h"
#include "Geom.h"
#include <assert.h>
#include <algorithm>
#include "Collision.h"
#include "Manifold.h"
#include "RigidBody.h"
#include <map>
struct Collider
{
void FindCollisions(RigidBody *bodies, size_t bodiesCount)
{
for (ManifoldMap::iterator man = manifolds.begin(); man != manifolds.end(); man++)
{
man->second.isMerged = 0;
}
for (size_t bodyIndex1 = 0; bodyIndex1 < bodiesCount; bodyIndex1++)
{
for (size_t bodyIndex2 = bodyIndex1 + 1; bodyIndex2 < bodiesCount; bodyIndex2++)
{
if (bodies[bodyIndex1].geom.aabb.Intersects(bodies[bodyIndex2].geom.aabb))
{
if (bodyIndex1 == 0 && bodyIndex2 == 1)
{
int pp = 1;
}
BodyPair pair(&bodies[bodyIndex1], &bodies[bodyIndex2]);
ManifoldMap::iterator man = manifolds.find(pair);
if (man != manifolds.end())
{
man->second.isMerged = true;
}
else
{
manifolds[pair] = Manifold(pair.body1, pair.body2);
}
}
}
}
for (ManifoldMap::iterator man = manifolds.begin(); man != manifolds.end();)
{
man->second.Update();
if (man->second.collisionsCount == 0)
{
ManifoldMap::iterator next = man;
next++;
manifolds.erase(man);
man = next;
}
else
{
man++;
}
}
}
struct BodyPair
{
BodyPair()
{
body1 = body2 = 0;
}
BodyPair(RigidBody *body1, RigidBody *body2)
{
this->body1 = body1;
this->body2 = body2;
}
bool operator < (const BodyPair &other) const
{
if (body1 < other.body1) return 1;
if ((body1 == other.body1) && (body2 < other.body2)) return 1;
return 0;
}
RigidBody *body1;
RigidBody *body2;
};
typedef std::map<BodyPair, Manifold> ManifoldMap;
ManifoldMap manifolds;
};