forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline intersection.js
More file actions
153 lines (113 loc) · 3.6 KB
/
Copy pathline intersection.js
File metadata and controls
153 lines (113 loc) · 3.6 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('balls', 'assets/sprites/balls.png', 17, 17);
}
var handle1;
var handle2;
var handle3;
var handle4;
var line1;
var line2;
function create() {
game.stage.backgroundColor = '#124184';
handle1 = game.add.sprite(100, 200, 'balls', 0);
handle1.inputEnabled = true;
handle1.input.enableDrag(true);
handle2 = game.add.sprite(400, 300, 'balls', 0);
handle2.inputEnabled = true;
handle2.input.enableDrag(true);
handle3 = game.add.sprite(200, 400, 'balls', 1);
handle3.inputEnabled = true;
handle3.input.enableDrag(true);
handle4 = game.add.sprite(500, 500, 'balls', 1);
handle4.inputEnabled = true;
handle4.input.enableDrag(true);
line1 = new Phaser.Line(handle1.x, handle1.y, handle2.x, handle2.y);
line2 = new Phaser.Line(handle3.x, handle3.y, handle4.x, handle4.y);
}
/*---------------------------------------------------------------------------
Returns an Object with the following properties:
intersects -Boolean indicating if an intersection exists.
start_inside -Boolean indicating if Point A is inside of the polygon.
end_inside -Boolean indicating if Point B is inside of the polygon.
intersections -Array of intersection Points along the polygon.
centroid -A Point indicating "center of mass" of the polygon.
"pArry" is an Array of Points.
----------------------------------------------------------------------------*/
// a = point
// b = point
// pArry = polygons array
/*
function lineIntersectPoly(A, B, pArry) {
var An =1;
var Bn =1;
var C = {x: 0, y: 0};
var D = {x: 0, y: 0};
var i = {x: 0, y: 0};
var cx=0;
var cy=0;
var result = {};
var pa = pArry.slice(); //Copy to prevent growing points when connecting ends.
pa.push(pa[0]); //Create line from last Point to beginning Point
result.intersects = false;
result.intersections=[];
result.start_inside=false;
result.end_inside=false;
var n = pa.length-1;
while(n > -1){
C.x = pa[n].x;
C.y = pa[n].y;
if(n > 0){
cx+=C.x;
cy+=C.y;
D=Point(pa[n-1])||Point(pa[0]);
i=lineIntersectLine(A,B,C,D);
if(i != null){
result.intersections.push(i);
}
if(lineIntersectLine(A,new Point(C.x+D.x,A.y),C,D) != null){
An++;
}
if(lineIntersectLine(B,new Point(C.x+D.x,B.y),C,D) != null){
Bn++;
}
}
n--;
}
if(An % 2 == 0){
result.start_inside=true;
}
if(Bn % 2 == 0){
result.end_inside=true;
}
result.centroid=new Point(cx/(pa.length-1),cy/(pa.length-1));
result.intersects = result.intersections.length > 0;
return result;
}
*/
var c = 'rgb(255,255,255)';
var p = new Phaser.Point();
function update() {
line1.fromSprite(handle1, handle2, true);
line2.fromSprite(handle3, handle4, true);
p = line1.intersects(line2, true);
if (p)
{
c = 'rgb(0,255,0)';
}
else
{
c = 'rgb(255,255,255)';
}
}
function render() {
game.debug.renderLine(line1, c);
game.debug.renderLine(line2, c);
game.debug.renderLineInfo(line1, 32, 32);
game.debug.renderLineInfo(line2, 32, 100);
if (p)
{
game.context.fillStyle = 'rgb(255,0,255)';
game.context.fillRect(p.x - 2, p.y - 2, 5, 5);
}
}