-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollisionSystem.js
More file actions
47 lines (37 loc) · 1.51 KB
/
collisionSystem.js
File metadata and controls
47 lines (37 loc) · 1.51 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
var CollisionSystem = function(options, quadTree) {
var collisionSystem = {};
collisionSystem.quadTree = quadTree;
collisionSystem.update = function (scene) {
var entity;
var toDir;
for (i = 0; i < scene.entities.length; i++) {
entity = scene.entities[i];
collisionSystem.checkCollision(entity, scene);
}
};
collisionSystem.checkCollision = function(entity, scene) {
entity.out = collisionSystem.isOut(entity);
entity.collision = collisionSystem.isCollision(entity, scene);
};
collisionSystem.isOut = function (entity) {
return options.stageWidth() < (entity.pos.x - entity.radius) ||
(entity.pos.x + entity.radius) < 0 ||
options.stageHeight() < (entity.pos.y - entity.radius) ||
(entity.pos.y + entity.radius) < 0
};
collisionSystem.isCollision = function (entityToCheck, scene) {
var entitiesInQuadrant = collisionSystem.quadTree.retrieve(entityToCheck);
var candidateEntities = entitiesInQuadrant.filter(function(e) { return e.id != entityToCheck.id; });
for (j = 0; j < candidateEntities.length; j++) {
currEntity = candidateEntities[j];
if (currEntity.intersects(entityToCheck)) {
return true;
}
}
return false;
};
collisionSystem.setQuadTree = function(quadTree) {
collisionSystem.quadTree = quadTree;
}
return collisionSystem;
};