-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathfind.js
110 lines (88 loc) · 3.08 KB
/
pathfind.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
function Pathfind( x1, y1, x2, y2, step, world, angleTest=PI/2 ){
function vec( pos ){
return createVector( pos.x, pos.y )
}
function pointDistance(a, b){
return vec(a).dist(vec(b))
}
this.startPoint = { x: x1, y: y1 }
this.endPoint = { x: x2, y: y2 }
this.endPoint.counter = 0
this.points = [ [this.endPoint] ]
this.ready = false
this.path = []
this.getNextPoints = function(){
var nPoints = []
this.points[ this.points.length - 1 ].forEach( p => {
for (let i = 0; i <= 2 * PI ; i += angleTest) {
let angleVector = p5.Vector.fromAngle( i )
angleVector.setMag( step )
let vecNewPoint = vec(p).copy().add( angleVector )
let newPoint = {
x: vecNewPoint.x, y: vecNewPoint.y,
counter: p.counter + 1
}
if( pointDistance( this.startPoint, newPoint ) < step ){
this.ready = true
}
let lowestDistance = Infinity
this.points.forEach( l => {
l.forEach( pp => {
let distance = vecNewPoint.dist( vec( pp ) )
lowestDistance = distance < lowestDistance ? distance : lowestDistance
} )
} )
nPoints.forEach( pp => {
let distance = vecNewPoint.dist( vec( pp ) )
lowestDistance = distance < lowestDistance ? distance : lowestDistance
} )
if( !world.collision( newPoint ) && lowestDistance > 9.9 ){
nPoints.push( newPoint )
}
}
} )
nPoints = nPoints.filter((item, index) => nPoints.indexOf(item) === index);
this.points.push( nPoints )
}
this.showPath = function(){
this.path.forEach( p => {
fill( 255 )
circle( p.x, p.y, 5 )
} )
}
this.showPoints = function(){
this.points.forEach( line => {
line.forEach( p => {
fill( p.counter * 255 / this.points.length )
circle( p.x, p.y, 5 )
} )
} )
}
this.getPath = function (){
var path = [ this.startPoint ]
while( vec( path[ path.length -1 ] ).dist( vec(this.endPoint) ) > step ){
path.push( nexStep(path[ path.length - 1], this.points) )
}
path.shift()
this.path = path
return path
}
}
function nexStep( pos, points){
nearPoints = []
points.forEach( l => {
l.forEach( p => {
pVect = createVector( p.x, p.y )
posVect = createVector( pos.x, pos.y )
dist = pVect.dist( posVect )
if (dist < 15){
nearPoints.push( p )
}
})
} )
lessCounter = { counter: Infinity }
nearPoints.forEach( p => {
lessCounter = p.counter < lessCounter.counter ? p : lessCounter
} )
return lessCounter
}