简体   繁体   English

检查圆形边界内的碰撞

[英]Check collision within circle bounds

I am trying to fill circle with some line alike sprites.我正在尝试用一些类似线条的精灵填充圆圈。 I generate random position x and y within +- radius, but I olny want to draw sprite when it not intersects with circle bounds like:我在 +- 半径内生成随机位置 x 和 y,但是当精灵不与圆形边界相交时,我想绘制精灵,例如:

在此处输入图片说明

Green lines I want to draw, and the red ones - I don't.我想画绿线,红线 - 我不想。 I am wondering on some ideas that can help to detect unwanted sprites fast.我想知道一些可以帮助快速检测不需要的精灵的想法。 Is there anything I can use for this purposes?有什么我可以用于此目的的吗? I am using pixi.js and sprite`s height and width are always the same.我正在使用 pixi.js 并且精灵的高度和宽度始终相同。

A simple idea could be the following one:一个简单的想法可能是以下一个:

For segment (line?), defined by the two point P1 = (x0,y0) and P2 = (x1, y1).对于线段(线?),由两点 P1 = (x0,y0) 和 P2 = (x1, y1) 定义。 The circle is defined by center R = (xc, yc) and a radius r.圆由中心 R = (xc, yc) 和半径 r 定义。

Now check现在检查

  • distance P1 - C < r, also meaning point is IN the circle距离 P1 - C < r,也意味着点在圆内
  • distance P2 - C < r, (same)距离 P2 - C < r,(相同)

If both true, green line !如果两者都为真,绿线! Depending on the library you used, you'll probably find a method such pointIsInCircle that will do half of the job.根据您使用的库,您可能会发现像pointIsInCircle这样的方法可以完成一半的工作。

Here is a simple exemple from w3resource这是w3resource 的一个简单示例

function check_a_point(a, b, x, y, r) {
    var dist_points = (a - x) * (a - x) + (b - y) * (b - y);
    r *= r;
    if (dist_points < r) {
        return true;
    }
    return false;
}

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

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