Jas*_*ske 2 javascript algorithm collision-detection polygons computational-geometry
如果给出一个n
多边形,一条长度线k
(at x,y
和angle a
),是否有一个算法来检测我碰撞过的多边形的哪一边(如果有的话)?到目前为止,我已经尝试测试是否x,y
在多边形之外,然后遍历多边形的每个边缘,计算到每个端点的距离.这是一个JS小提琴,展示了我创造的世界.
这是JavaScript(HTML和CSS不值得复制):
var eventLoop,
maxVelocity = 10,
agility = 5,
baseLength = 5,
degree = ((2*Math.PI)/360),
world = document.getElementById('world'),
context = world.getContext("2d"),
boundry = [[180, 120],[240, 60],[360, 40],[420, 120],[360, 220],[350, 240],[360, 265],[470,360],[450,480],[360,540],[240,550],[140,480],[120,470],[100,360],[120,300],[220,240],[240,220]],
camera = {
location: {
x:300,
y:90
},
angle: 0,
velocity: 0
},
engine = {
drawWorld: function(shape, context) {
var point,
index,
size = shape.length;
context.clearRect(0, 0, world.width, world.height);
context.beginPath();
for(index = 0; index < size; index++) {
point = shape[index];
if(index == 0) {
context.moveTo(point[0], point[1]);
} else {
context.lineTo(point[0], point[1]);
}
}
context.closePath();
context.stroke();
},
drawCamera: function(camera, context) {
var a = camera.location,
b = this.calcNextPoint(camera, 1);
context.beginPath();
context.moveTo(a.x, a.y);
context.lineTo(b.x, b.y);
context.stroke();
context.beginPath();
context.arc(a.x, a.y, baseLength, 0, Math.PI*2, true);
context.closePath();
context.stroke();
},
calcNextPoint: function(camera, moment) {
return {
x: camera.location.x + ((camera.velocity*(1/moment))*Math.sin(camera.angle)),
y: camera.location.y + ((camera.velocity*(1/moment))*(Math.cos(camera.angle)))
};
},
isInside: function(point, shape) {
var i, j, c = 0;
for (i = 0, j = shape.length - 1; i < shape.length; j = i++) {
if (((shape[i][1] > point.y) != (shape[j][1] > point.y)) && (point.x < (shape[j][0] - shape[i][0]) * (point.y - shape[i][1]) / (shape[j][1] - shape[i][1]) + shape[i][0])) {
c = !c;
}
}
return c;
}
};
document.onkeydown = function(e) {
e = e || window.event;
if (e.keyCode == '37') {
// left arrow
camera.angle += degree*agility;
}
else if (e.keyCode == '39') {
// right arrow
camera.angle -= degree*agility;
}
else if (e.keyCode == '38') {
// up arrow
camera.velocity += 1;
if(camera.velocity > maxVelocity) {
camera.velocity = maxVelocity;
}
}
else if (e.keyCode == '40') {
// down arrow
camera.velocity -= 1;
if(camera.velocity < 0) {
camera.velocity = 0;
}
}
}
engine.drawWorld(boundry, context);
engine.drawCamera(camera, context);
eventLoop = setInterval(function() {
engine.drawWorld(boundry, context);
engine.drawCamera(camera, context);
if(engine.isInside(camera.location, boundry)) {
camera.location = engine.calcNextPoint(camera, 1);
}
}, 100);
Run Code Online (Sandbox Code Playgroud)
我一直在玩一些JavaScript,它们模拟游戏Flower by ThatGameComapny 的2-Dementional版本,最终我想尝试实现Oculus Rift版本.我要解决的下一个问题是,当玩家与边缘碰撞时,将玩家变回多边形.
所以你基本上想知道多边形的哪些边与给定的线相交,对吧?
这仅仅是非常简单的几个处理线性方程表示的边缘(不等式,更精确).您已经有了很好的inside
操作实现,也可以这样做.所有这些算法的共同点是比较线的哪一侧[x1, y1]
- [x2, y2]
重点[x, y]
在于:
compare = function (a, b) { // classical compare function, returns -1, 0, 1
return a < b ? -1 : (a == b ? 0 : 1);
}
...
_lineSide: function (x, y, x1, y1, x2, y2) {
return compare(x - x1, (x2 - x1) * (y - y1) / (y2 - y1));
}
Run Code Online (Sandbox Code Playgroud)
如果[x, y]
位于线的一侧,则此函数将返回-1 [x1, y1]
- [x2, y2]
对于另一侧,该函数将返回1,如果它恰好位于该线上,则返回0.这一点并不重要,哪一方是哪一方,只是将它们分开.然而,当y2 - y1
零或接近零时,这将不起作用.在这种情况下,你必须xy翻转情况:
lineSide: function (x, y, x1, y1, x2, y2) {
var eps = 1e-20; // some very small number
if (Math.abs(y2 - y1) > eps) // this way we avoid division by small number
return _lineSide(x, y, x1, y1, x2, y2);
else if (Math.abs(x2 - x1) > eps) // flip the situation for horizontal lines
return _lineSide(y, x, y1, x1, y2, x2);
else // edge has close-to-zero length!
throw new this.zeroLengthLineException()
},
zeroLengthLineException: function () {},
Run Code Online (Sandbox Code Playgroud)
现在来测试两条线[x1, y1]
- [x2, y2]
和[x3, y3]
- [x4, y4]
相交是否非常容易.只要看看是否[x1, y1]
并且[x2, y2]
位于[x3, y3]
- 的另一侧- [x4, y4]
如果[x3, y3]
并且[x4, y4]
位于[x1, y1]
- 的另一侧- [x2, y2]
.如果是,则线条相交!
// proper: true/false (default false) - if we want proper intersection, i.e. just
// touching doesn't count
linesIntersect: function (x1, y1, x2, y2, x3, y3, x4, y4, proper) {
var min_diff = proper ? 2 : 1;
return Math.abs(this.lineSide(x1, y1, x3, y3, x4, y4) -
this.lineSide(x2, y2, x3, y3, x4, y4)) >= min_diff
&& Math.abs(this.lineSide(x3, y3, x1, y1, x2, y2) -
this.lineSide(x4, y4, x1, y1, x2, y2)) >= min_diff;
},
Run Code Online (Sandbox Code Playgroud)
现在,最终解决方案很简单:只需通过调用linesIntersect
循环检查给定行与所有多边形边的交集: