-
Notifications
You must be signed in to change notification settings - Fork 13
/
collision.js
79 lines (76 loc) · 1.66 KB
/
collision.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
/**
* A hit map class for dynamically
* checking whether an x/y coordinate
* with an appropriate hit image
*
* @param {Image} img The hit map image
*/
function HitMap(img){
var self = this;
this.img = img;
// only do the drawing once the
// image has downloaded
if (img.complete){
this.draw();
} else {
img.onload = function(){
self.draw();
};
}
}
HitMap.prototype = {
draw: function(){
// first create the canvas
this.canvas = document.createElement('canvas');
this.canvas.width = this.img.width;
this.canvas.height = this.img.height;
this.context = this.canvas.getContext('2d');
// draw the image on it
this.context.drawImage(this.img, 0, 0);
},
isHit: function(x, y){
if (this.context){
// get the pixel RGBA values
var pixel = this.context.getImageData(x, y, 1, 1);
if (pixel){
// we consider a hit if the Red
// value is 0
return pixel.data[0] === 0;
} else {
return false;
}
} else {
return false;
}
}
};
function CollisionPoint (car, rotation, distance) {
this.car = car;
this.rotation = rotation;
this.distance = distance || this.distance;
}
CollisionPoint.prototype = {
car: null,
rotation: 0,
distance: 20,
getXY: function(){
return rotatePoint(
this.car.getCenter(),
this.car.rotation + this.rotation,
this.distance
);
},
isHit: function(hitMap){
var xy = this.getXY();
return hitMap.isHit(xy.x, xy.y);
}
};
function CollisionRadius () {
}
CollisionRadius.prototype = {
x: 0,
y: 0,
radius: 10,
check: function(coords){
}
};