简体   繁体   English

圆形和多边形之间的Javascript碰撞检测?

[英]Javascript Collision Detection between circle and polygon?

I'm unsure how to go about defining the area for a collision detection function when I have a polygon that looks like: 当我有一个看起来像这样的多边形时,我不确定如何去定义碰撞检测功能的区域:

    ______
   / _____|
  / /
 / /
---

I draw the polygon using lineTo() a few times before fill/stroke call so I know the x,y co-ords of all the points. 在填充/描边调用之前,我使用lineTo()绘制了多边形,所以我知道所有点的x,y坐标。

Currently I just check if the ball is beyond certain areas of points: 目前,我只检查球是否超出某些点区域:

    if(tmpParticle.y <= platformBottom) {
        if(tmpParticle.x < leftPipe_middleX || tmpParticle.x > rightPipe_middleX) {
            tmpParticle = particleCollision(tmpParticle, platformBottom);
        }
    }

    if(tmpParticle.y <= pipeBottom && tmpParticle.y >= pipeBottom - 30) {
        if(tmpParticle.x < leftPipe_bottomRightX && tmpParticle.x > leftPipe_bottomLeftX) {
            tmpParticle = particleCollision(tmpParticle, pipeBottom);
        } else if (tmpParticle.x < rightPipe_bottomRightX && tmpParticle.x > rightPipe_bottomLeftX) {
            tmpParticle = particleCollision(tmpParticle, pipeBottom);
        }
    }

platformHeight would be the Y value for the 'top horizontal line' platformBottom would be the Y value for the 'horizontal line just below platformHeight' rightPipe* is for the example shown. platformHeight将是“顶部水平线” platform的Y值,bottomHeight是“位于platformHeight下方水平线”的Y值,例如rightPipe *。 leftPipe* is for the same polygon except in the other direction (to form a pipe, where you must shoot the balls through without colliding). leftPipe *用于同一多边形,但在另一个方向上除外(要形成管道,您必须在其中射出球而不会发生碰撞)。

My particleCollision() function just takes the tmpParticle and inverses the direction based on the Y value (2nd parameter, ie pipeBottom). 我的particleCollision()函数仅使用tmpParticle并根据Y值(第二个参数,即pipeBottom)反转方向。 This works fine for now though I may need to improve it later on. 尽管目前我可能需要稍后对其进行改进,但目前效果良好。

I just need to figure out a better way to define the area for collisions. 我只需要找出一种更好的方法来定义碰撞区域。

You may consider splitting your pipe into triangles and then finding triangle - circle intersection area. 您可以考虑将管道分成三角形,然后找到triangle - circle相交区域。 If they do intersect the intersection will always be a convex polygon (area is easy to calculate by splitting into triangles again) and a segment (area is easy to calculate too - http://en.wikipedia.org/wiki/Circular_segment ). 如果它们相交,则相交将始终是convex polygon (易于通过再次拆分为三角形来计算面积)和segment (也易于计算面积-http: //en.wikipedia.org/wiki/Circular_segment )。 The other case is the triangle itself, in case it lies inside the circle completely (a simple case again). 另一种情况是三角形本身,以防它完全位于圆内(再次是简单的情况)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM