Reputation: 346
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.
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.
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.
Upvotes: 0
Views: 271
Reputation: 35950
The issue is with your square
operation, ^
is NOT power operator in 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;
}
Upvotes: 1
Reputation: 146449
Just calculate the distance between the point and the center of the circle ( square root( (y2-y1)^2 + (x2-x1)^2) and compare with the radius
Upvotes: 0