-
Notifications
You must be signed in to change notification settings - Fork 0
/
ii2d_obstacle.js
178 lines (152 loc) · 4.33 KB
/
ii2d_obstacle.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class Circle {
constructor(center, radius) {
this.center = center;
this.radius = radius;
this.color = "#FF0000";
this.oldCenter = new Vector(0,0);
}
draw(){
ctx.beginPath();
ctx.arc(this.center.x, this.center.y, this.radius, 0, 2 * Math.PI);
ctx.strokeStyle = this.color;
ctx.stroke();
}
move(m){
//déplacement du centre du cercle de m
this.center.add(m);
}
distance(m){
return Math.abs(Math.sqrt(Math.pow((this.center.x-m.x),2)+Math.pow((this.center.y-m.y),2)) - this.radius);
}
is_inside(p){
let dist = Math.sqrt(Math.pow((this.center.x-p.x),2)+Math.pow((this.center.y-p.y),2));
return (dist <= this.radius);
}
intersect(p1,p2){
// p1 => x_old
// p2 => x_new
let normal = new Vector(0,0);
let position = new Vector(0,0);
let p1In = this.is_inside(p1);
let p2In = this.is_inside(p2);
let isIntersect = ((p1In && !p2In) || (!p1In && p2In));
//si intersection :
if (isIntersect){
position.set(p1);
normal.set(p1.sous(this.center));
normal.div(Math.sqrt(Math.pow(normal.x,2) + Math.pow(normal.y,2)));
}
return {
isIntersect: isIntersect,
normale: normal,
position: position
}
}
update(){
this.oldCenter.set(this.center);
}
oldCorrec(p){
let d = this.center.clone().sous(this.oldCenter);
return d.add(p.oldPosition);
}
}
class Segment {
constructor(a, b) {
this.a = a;
this.b = b;
this.color = "#FF0000";
this.zone = null; // 0 = a, 1 = b, 2 = line
this.olda = new Vector(0,0);
this.oldb = new Vector(0,0);
}
draw(){
ctx.beginPath();
ctx.moveTo(this.a.x, this.a.y);
ctx.lineTo(this.b.x, this.b.y);
ctx.strokeStyle = this.color;
ctx.stroke();
}
move(m){
//déplacement de a et b de m
if (this.zone == 0){
this.a.add(m);
} else if (this.zone == 1) {
this.b.add(m);
} else {
this.a.add(m);
this.b.add(m);
}
}
distance(m){
let vAB = new Vector(this.b.x - this.a.x, this.b.y - this.a.y);
let vBA = new Vector(this.a.x - this.b.x, this.a.y - this.b.y);
let vAM = new Vector(m.x - this.a.x, m.y - this.a.y);
let vBM = new Vector(m.x - this.b.x, m.y - this.b.y);
let nAB = new Vector(- (this.b.y - this.a.y), this.b.x - this.a.x) //(-y, x)
if ((vAM.x*vAB.x+vAM.y*vAB.y) < 0){ //zone A
this.zone = 0;
return Math.sqrt(Math.pow((this.a.x-m.x),2)+Math.pow((this.a.y-m.y),2));
} else if ((vBM.x*vBA.x+vBM.y*vBA.y) < 0) { // Zone B
this.zone = 1;
return Math.sqrt(Math.pow((this.b.x-m.x),2)+Math.pow((this.b.y-m.y),2));
} else { // Zone Line
this.zone = 2;
return Math.abs((nAB.x*vAM.x+nAB.y*vAM.y))/Math.sqrt(Math.pow((nAB.x),2)+Math.pow((nAB.y),2));
}
}
signe(p,a,n){
let signe = p.clone().sous(a).x*n.x+p.clone().sous(a).y*n.y;
return (signe > 0)?true:false;
}
intersect(p1,p2){
let nAB = new Vector(- (this.b.y - this.a.y), this.b.x - this.a.x);
let np = new Vector(- (p2.y - p1.y), p2.x - p1.x);
let position = new Vector(0,0);
//calcul des signes => intersection ou non ?
let intersect = ((this.signe(p1, this.a, nAB)) != (this.signe(p2, this.a, nAB))
&& (this.signe(this.a, p1, np)) != (this.signe(this.b, p1, np)))?true:false;
(intersect)?position.set(p1):true;
return {
isIntersect: intersect,
normale: nAB,
position: position
}
}
update(){
this.olda.set(this.a);
this.oldb.set(this.b);
}
oldCorrec(p){
let oldMid = new Vector((this.olda.x + this.oldb.x)/2, (this.olda.y + this.oldb.y)/2);
let newMid = new Vector((this.a.x + this.b.x)/2, (this.a.y + this.b.y)/2);
let d = new Vector(newMid.x - oldMid.x, newMid.y - oldMid.y);
return d.add(p.oldPosition);
}
}
class ObstacleManager {
constructor() {
this.all = [];
this.selected = null;
}
draw(){
for(let i = 0; i < this.all.length; ++i){
this.all[i].draw();
}
}
select(m){
this.selected = null;
let minDist = 20;
for(let i=0; i < this.all.length;i++){
//console.log('dist obs ' + i + ' : ' + this.all[i].distance(m));
if(this.all[i].distance(m) <= minDist){
minDist = this.all[i].distance(m);
this.selected = this.all[i];
}
}
}
update(){
for(let i=0; i < this.all.length; ++i){
this.all[i].update();
}
}
}