-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayCastTest.html
More file actions
210 lines (158 loc) · 4 KB
/
Copy pathRayCastTest.html
File metadata and controls
210 lines (158 loc) · 4 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Game</title>
<style>
* { padding: 0; margin: 0; }
canvas { background: #eee; display: block; margin: 0 auto; }
</style>
</head>
<body>
<canvas id ="myCanvas" width="480" height="320"></canvas>
<script>
// Javascript code
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var BASECOLOR = [255, 255, 255, 255];
var WALLCOLOR = [0,0,0,255];
var personalityTypes = {NEUTRAL: 0, AGGRESSIVE: 1, SHY: 3, GUIDING: 4, TERRITORIAL: 5, CURIOUS: 6 , SAD: 7, PROTECTIVE: 8};
var sightPoint = [240, 200];
ctx.fillRect(100,100,10,10);
ctx.fillRect(200,100,30,30);
ctx.fillRect(400,250,25,25);
//ctx.rect(220,180,150,100);
//ctx.rect(219,179,152,102);
//ctx.rect(218,1178,154,104);
//ctx.stroke();
ctx.beginPath();
ctx.arc(sightPoint[0], sightPoint[1], 5, 0, 2*Math.PI, false)
ctx.fillStyle = 'red';
ctx.fill();
function isSameColor(color1, color2){
for(var index in color1){
if(color2[index] != color1[index]){
return false;
}
}
return true;
}
function isWallColor(color){
for(var i = 0; i < 3; i++){
if(WALLCOLOR[i] != color[i]){
return false;
}
}
if (color[3] == 0){
return false;
}
return true;
}
function rayCast(x, y, viewRange, viewDirection, n){
var angleStart = viewDirection - (viewRange/2);
var angleEnd = viewDirection + (viewRange/2);
var angleInc = viewRange/n;
var distInc = 5;
for(k = 0; k < n; k++){
console.log(k);
var angle = angleStart + angleInc * k;
pixelColor = BASECOLOR;
var i = 0;
var xAbs = x;
var yAbs = y;
var xPrev = -1;
var yPrev = -1;
var draw = true;
var xPosAngle = Math.cos(angle);
var yPosAngle = Math.sin(angle);
while (draw){
var distance = distInc * i;
var xPos = Math.round(distance*xPosAngle);
var yPos = Math.round(distance*yPosAngle);
i++;
xAbs = x + xPos;
yAbs = y - yPos; //the y axis is inverted
if (xAbs == xPrev && yAbs == yPrev){
continue;
}
/*
console.log("loop", i);
console.log(viewRange);
console.log(viewDirection);
console.log(angleStart);
console.log(angleEnd);
console.log(angleInc);
console.log(angle);
console.log(distance);
console.log(xPos);
console.log(xAbs);
*/
var pixelColor = ctx.getImageData(xAbs, yAbs, 1, 1).data;
if (xAbs >= canvas.width || yAbs >= canvas.height || xAbs <= 0 || yAbs <= 0 || isWallColor(pixelColor)){
draw = false;
break;
}
if ((pixelColor != [0,0,0,0]))
{
console.log((i-1), xAbs, yAbs, xPos, yPos, xPosAngle, yPosAngle, distance, angle*180/Math.PI, pixelColor, isWallColor(pixelColor));
}
//console.log(pixelColor);
if(k == 0){
ctx.fillStyle = "yellow";
}
else{
ctx.fillStyle = "red";
}
if(isSameColor(pixelColor, [0,0,0,0])){
ctx.fillStyle = "blue";
}
ctx.fillRect(xAbs, yAbs, 1, 1);
xPrev = xAbs;
yPrev = yAbs;
}
console.log("i:", i );
console.log("angle:", angle*180/Math.PI);
console.log(angleStart*180/Math.PI);
console.log(angleEnd*180/Math.PI);
console.log("This should happen a couple times", k);
//console.log(canvas.width);
}
}
/*
function AStarSearch(){
}
class Spider {
constructor(x, y, personality){
Spider.x = x;
Spider.y = y;
Spider.personality = personality;
}
moveStep(){
}
setDestination(target){
Spider.target = target;
}
}
*/
rayCast(sightPoint[0], sightPoint[1], (Math.PI*.5), (Math.PI*.6), 30);
//rayCast(sightPoint[0], sightPoint[1], (Math.PI*2), (Math.PI*1.10), 30);
/*
for (y = 0; y < canvas.height; y++){
for (x = 0; x < canvas.width; x++){
var pixelColor = ctx.getImageData(x, y, 1, 1).data;
if(!isSameColor(pixelColor, [0,0,0,255])){
var imgData = ctx.createImageData(1, 1);
imgData.data[0] = 0;
imgData.data[1] = 0;
imgData.data[2] = 255;
imgData.data[3] = 255;
ctx.putImageData(imgData, x, y)
console.log(x, y);
}
}
}
*/
console.log
</script>
</body>
</html>