We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I know there is a code for chain simulation in processing but I want to create my own code using my logic but somehow it not working as expected. Can anyone of you help me?
thanks in advance :)
Point P1, P2;
int len = 50;
float sx=0, sy=0;
void setup() {
size(400, 400);
P1 = new Point();
P2 = new Point();
}
void draw() {
background(-1);
P1.move(mouseX, mouseY);
P1.display(#F20055);
float theta = atan2(mouseY-sy, mouseX-sx);
sx = mouseX + len*cos(theta);
sy = mouseY + len*sin(theta);
P2.move(sx, sy);
P2.display(#C5F200);
stroke(0);
line(P1.x, P1.y, P2.x, P2.y);
}
class Point {
float x, y;
void display(color c) {
fill(c);
noStroke();
ellipse(x, y, 10, 10);
}
void move(float x, float y) {
this.x = x;
this.y = y;
}
}
Answers
Because its not working "as expected" its very hard to fix as I don't know what you expected. What do you want your chain to be like?
@DCRaven I wanted it like to one ellipse follow other and I would like to expand it to more than two.
Also it would be great if you can explain me the problem.
Thanks again :)
This is an adaptation to an example to have an unlimited amount of links in the chain and structured it a bit closer to how I would've. :)
The problem with your code (or at least my problem with it) is I would've structured it completely differently. First off you will need an array or arraylist (I personally would go for an arraylist) and store your class in it. Another problem you would have found with this structure is that you will need to put your physics inside the class rather than having a shell of a class that you control from a draw loop. Last thing that I can think of at the moment is that the mouseX & mouseY are like the fist link in the chain. Instead of having it centered (like you did) you should provide each like with the previous link's positions to join to (mouseX & mouseY being for the first link as there is no other to attach to)
Oh wow it posted twice. Anyway I hope that works for you :D
@DCRaven ..... Thanks a lot :) for your code and explanation. :D But sorry I wanted to simulate this code [illustrated below] using my logic [illustrated above]. It is an example code from processing which works as expected and I wanted to do the same using class but I am not able to get my head around.
I had tried removing y directional force in your code and it worked but the only problem was it gets collapsed which I don't want. I have also teased stiffness and damping but then it started behaving weird.
So can you help me to implement it ? and thank you so much for time ....
And a bit of physics later this appears!
Is this what you want? its 12:03 am so i'm gonna sleep now I'll make changes tommorow if need be :D
Here is the my attempt. You can find code in action here
@Blyk Thanks for the code. This is what exactly what I wanted :) @DCRaven : Thanks for your effort and time .... highly appreciated :) :)
Oh I thought you wanted it physicsified :/ Still my one is cooler... good luck on whatever project you have planned with a chain!
@DCRaven: I am really sorry if I was rude. I just wanted to use my logic (class implementation) to create the same effect that comes with processing.
Certainly your code was awesome :) because it has physics but I was trying some basics first :)
Thanks.