-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot.js
More file actions
68 lines (58 loc) · 1.98 KB
/
Copy pathdot.js
File metadata and controls
68 lines (58 loc) · 1.98 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
class Dot{
constructor(posX, posY){
this.pos = createVector(posX, posY);
this.initialPos = createVector(posX, posY);
this.speed = 4;
this.arrived = false;
}
get position(){
return this.pos;
}
setTarget(targetX, targetY){
this.target = createVector(targetX, targetY);
}
changePosition(deltaX, deltaY){
this.pos.x = this.pos.x + deltaX;
this.pos.y = this.pos.y + deltaY;
}
moveConstantSpeedX(){
return abs(this.target.x - this.pos.x) < 2 ? 0 : this.speed*(this.target.x - this.pos.x)/abs(this.target.x - this.pos.x);
}
moveConstantSpeedY(){
return abs(this.target.y - this.pos.y) < 2 ? 0 : this.speed*(this.target.y - this.pos.y)/abs(this.target.y - this.pos.y);
}
moveConstantSpeedForBothAxis(){
let deltaX = this.moveConstantSpeedX();
let deltaY = this.moveConstantSpeedY();
if(deltaX == 0 && deltaY == 0){
this.arrived = true;
console.log("Arrived: " + this.arrived, this.pos);
}
return [deltaX, deltaY];
}
moveXFirst(){
let deltaX = this.moveConstantSpeedX();
let deltaY = deltaX != 0 ? 0 : this.moveConstantSpeedY();
return [deltaX, deltaY];
}
moveYFirst(){
let deltaY = this.moveConstantSpeedY();
let deltaX = deltaY != 0 ? 0 : this.moveConstantSpeedX();
return [deltaX, deltaY];
}
moveArriveSameMoment(){
let deltaX = (this.target.x - this.initialPos.x)/60;
let deltaY = (this.target.y - this.initialPos.y)/60;
return [deltaX, deltaY];
}
move(){
if(!this.arrived){
let delta = this.moveConstantSpeedForBothAxis();
this.changePosition(delta[0], delta[1]);
if(dist(this.pos.x, this.pos.y, this.target.x, this.target.y) < 2.6 || (delta[0] == 0 && delta[1] == 0)){
this.pos = this.target;
this.arrived = true;
}
}
}
}