-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
115 lines (102 loc) · 3.05 KB
/
utils.js
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
104
105
106
107
108
109
110
111
112
113
114
115
function drawLinesFromBBPoly(sprite, bbPoly, num, color) {
if (!sprite.redLines) {
sprite.redLines = {};
}
var g = new PIXI.Graphics()
g.lineStyle(2, color, 1);
_.each(bbPoly.calcPoints, function(point, idx) {
var x = bbPoly.pos.x+point.x;
var y = bbPoly.pos.y+point.y;
if (idx === 0) {
g.moveTo(x,y);
}
else {
g.lineTo(x,y);
}
});
oldSprite = sprite.redLines[num];
redLineSprite = g//new PIXI.Sprite(g.generateTexture());
if (sprite.parent.children.indexOf(oldSprite) !== -1) {
sprite.parent.removeChild(oldSprite);
}
//redLineSprite.position.x = bbPoly.pos.x;
//redLineSprite.position.y = bbPoly.pos.y;
//redLineSprite.anchor.x = .5;
//redLineSprite.anchor.y = .5;
sprite.redLines[num] = redLineSprite;
sprite.parent.addChild(redLineSprite);
}
function setupBBs(collisionObjects, stage, skipDraw, carMode) {
_.each(collisionObjects, function(obj) {
if (carMode) {
obj.bbPoly = BBFromSprite(obj.sprite);
}
else {
obj.bbPoly = BBFromSprite(obj);
};
if (skipDraw) {
return
}
// draw red lines around bbs for testing:
var ttt = obj.bbPoly;
var g = new PIXI.Graphics();
g.lineStyle(2, 0xff0000, 1);
if (carMode) {
g.moveTo(ttt.pos.x-6,ttt.pos.y-12);
}
else {
g.moveTo(ttt.pos.x,ttt.pos.y);
}
_.each(ttt.points, function(point) {
//console.log("X: " + (ttt.pos.x+point.x) + ", Y: " + (ttt.pos.y+point.y));
g.lineTo(ttt.pos.x+point.x,ttt.pos.y+point.y);
});
stage.addChild(g);
});
}
function BBFromSprite(sprite) {
// take a pixi sprite, return a SAT.js polygon
// SAT.js polygon requires a position and then counterclockwise points referenced off of initial position
var bb = new SAT.Polygon(new SAT.Vector(sprite.position.x,sprite.position.y), sprite.polygonPoints);
bb=bb.setAngle(sprite.rotation);
return bb
}
function BBFromPoints(pos, points) {
var pp = [];
_.each(points, function(point) {
pp.push(new SAT.Vector(point[0],point[1]));
});
return new SAT.Polygon(new SAT.Vector(pos[0],pos[1]), pp);
}
function getLineBB(points) {
return new SAT.Polygon(new SAT.Vector(points[0][0], points[0][1]), [new SAT.Vector(points[0][0],points[0][1]), new SAT.Vector(points[1][0],points[1][1])]);
}
function setupIntersectionDefs(intersectionDefs) {
var prepared = {}
_.each(intersectionDefs, function(def,intersectionId) {
var polygonPoints = [];
firstPoint = def[0];
_.each(def, function(point) {
polygonPoints.push(new SAT.Vector(point[0]-firstPoint[0],point[1]-firstPoint[1]));
});
prepared[intersectionId] = new SAT.Polygon(new SAT.Vector(firstPoint[0],firstPoint[1]),polygonPoints);
});
return prepared
}
function toRadians (angle) {
return angle * (Math.PI / 180);
}
function toDegrees(radians) {
return radians * (180/Math.PI);
}
function consoleLog(msg) {
if (globalOptions.debugMode) {
console.log(msg);
}
}
function getRandomColor() {
return Math.random().toString(16).substr(-6);
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}