Hi guys, I'm new to processing and I need help

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
image

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);
  }
}

Hello,

First thoughts…

:)

Hi @nhghi

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:

On this sketch, you know:

  • 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:

You can then apply basic trig functions:

   sin(deltaAngle / 2) = (Dia/2 + Gap/2) / Radius
=> deltaAngle = 2 * asin( (Dia + Gap) / (2*Radius))

The only thing left to do is to implement it with code but I will leave that as an exercice for you.
Here is my result:

image

3 Likes

Hello,

I like that response! And do enjoy the math.

What did you prepare the diagrams with?

A related discussion here but not a direct solution to this:

:)

Hello,

I use geogebra to quickly produce diagrams like these. They have different apps depending what you want to do : geometry, 3D, equations…

2 Likes

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");}

}```