so I’m trying to make an effect is like “Newton’s cradle”. I have n number of circles that go around my mouse. so the first n-1 circle go around my mouse with SPEED constance and the other circle go with 2*SPEED with opposite direction from the first n-1 circle, when the alone circle touch n-1 circles it becomes a part of the n-1 circle and one of the n-1 cirles go alone, like this picture
int numberDot=5;
int dia=30;
int radius=52;
float speed=0.01;
float angle=PI/12;
float c, d;
void setup()
{
size(500, 500);
}
void draw()
{
background(0);
angle+=speed;
for (int i=1; i<=5; i+=1)
// my question is how can i move the first n-1 circle with exactly 5 pixel apart from each other
{
c= cos(angle -i/(2*PI))*radius;
d=sin(angle -i/(2*PI))*radius;
ellipse(250+ c, 250+d, dia, dia);
}
}
When you want to do something like this, a good way to start is to draw a sketch of how you want it to look like with the information that you know and the one that you want:
Radius: the radius of the big circle on wich the small circles are travelling
Dia: the diameter of the small circle
Gap: the gap you want between 2 small circles
And you want to know:
deltaAngle: the angle between two consecutive small circles in order to get the desired gap bteween them
The good thing about drawing a sketch is that you can see things that you haven’t noticed before. For example here, you can see that the triangle (A, C1, C2) is isocele in A.
It means that to ease your life, you can cut it in half and work with the top part triangle (highlighted in orange) which is a rectangular triangle:
so I did firgure a way to make n-1 circle move but now the lonely cirelce is so hard to detech because it’s float, can anyone can help me of how to do that ?
int numberDot=5;
float dia=10;
float radius=150;
float speed=0.01;
float angle=0;
float angle1;
float c, d,a,b;
void setup()
{
size(500, 500);
frameRate(10);
}
void draw()
{
background(0);
for(int i=1;i<=4;i++)
{
a= cos(angle1 +i*asin(dia/radius))*radius;
b=sin(angle1+i*asin(dia/radius))*radius;
ellipse(250+ a, 250+b, dia, dia);
}
c= cos(angle )*radius;
d=sin(angle)*radius;
ellipse(250+ c, 250-d, dia, dia);
angle+=5*speed;
angle1+=speed;
if(250+ a>=250+ c-0.001||250+ a<=250+ c+0.001&&250-d<=250+b+0.001||250-d>=250+b+0.001)
// in here I try to check if the lonely ball hit the other or not, but it's not work
{
println("hit");}
}```