简体   繁体   English

检测与圆边的碰撞

[英]Detect a collision with the edge of a circle

I am using a script to generate random particles within a circle based on it's radius. 我正在使用脚本根据其半径在圆内生成随机粒子。 What I would like to do next is detect when the particle collides with the circles edge. 我接下来要做的是检测粒子何时与圆边缘碰撞。

I'm guessing I need to use a for loop to store the coordinates of the circles circumference in an array but I'm unsure what math is needed to do this. 我猜我需要使用一个for循环将圆的圆周坐标存储在一个数组中,但是我不确定要执行什么数学运算。

Here's what I've got down from the answer below. 这就是我从下面的答案中得出的结论。 It doesn't seem to be working though: 它似乎不起作用:

Variable par is a particles moving with the circle, emitters contains x,y, positions of the centre of the circle while the prop height contains the radius. 变量par是随圆运动的粒子,发射器包含x,y,即圆心的位置,而道具高度包含半径。

var fromC = Math.sqrt( (par.y-(emitters[i].y ) )^2 + (par.x- (emitters[i].x))^2); 

if(fromC >= emitters[i].height){
    par.vx *= -1;
    par.vy *= -1;
}

Thanks in advance. 提前致谢。

The issue is with your square operation, ^ is NOT power operator in javascript. 问题在于您的square运算, ^不是javascript中的幂运算符。

Use this: 用这个:

var fromC = Math.sqrt( Math.pow((par.y - emitters[i].y), 2) + Math.pow((par.x - emitters[i].x), 2) ); 

if(fromC >= emitters[i].height){
    par.vx *= -1;
    par.vy *= -1;
}

只需计算点和圆心之间的距离(平方根((y2-y1)^ 2 +(x2-x1)^ 2)并与半径比较

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

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