PVector start1;
PVector end1;
PVector start2;
PVector end2;
float e = 0; //angle
float l = 350; //length
float add = 0.05;//add(to angle)
float e2 = 0;//other angle
float add2 = 0; //add to second angle
void setup() { //setup the canvas
size(1600, 1000);
smooth();
strokeWeight(10);
start1=new PVector(950,300);
end1=new PVector();
start2=new PVector(650,300);
end2=new PVector();
}
void draw() {
background(0);
stroke(57,255,20);
fill(57,255,20);
ellipse(850,650,100,100);
ellipse(750,650,100,100);
line(750,300,750,600);
line(850,300,850,600);
end1.x=start1.x+l*sin(e);
end1.y=start1.y+l*cos(e);
end2.x=start2.x+l*sin(e2);
end2.y=start2.y+l*cos(e2);
ellipse(end1.x,end1.y,100,100);
ellipse(end2.x,end2.y,100,100);
line(start1.x,start1.y,end1.x,end1.y);
line(start2.x,start2.y,end2.x,end2.y);
e+=add;
e2-=add2;
if(e<0){
add=0;
add2=0.05;
}
if(e>1){
add*=-1;
}
if(e2<-1){
}
}
I know that the problem is within the last few lines but I don't know how to get around the problem. The problem is that each time e2
gets to negative, specifically less than -1
, the code says that now add2
must multiply by -1
to go the other way. But, as soon as it is more than -1, the code is also telling it that when e is less than 0(which it is), add2=0.05
. So what's happening is that it is fluctuating between >-1
and <-1
. How do I get around this problem? P.S. This explanation is only what I think the problem is but it could be completely different.
Add integral state variables actPendle
, which indicate the moving pendle. add
and add2
have to be initialized by 0.05:
int actPendle = 1;
float add = 0.05;//add(to angle)
float add2 = 0.05; //add to second angle
float e = 0; //angle
float e2 = 0;//other angle
If actPendle == 1
, then move the 1st pendle. If it has finished its move, the switch to the other pendle. Do the same for the 2nd pendle:
void draw() {
// [...]
if (actPendle == 1) {
e += add;
if(e > 1){
add *= -1;
} else if (e < 0) {
add *= -1;
actPendle = 2;
}
}
if (actPendle == 2) {
e2 -= add2;
if(e2 < -1){
add2 *= -1;
} else if (e2 > 0) {
add2 *= -1;
actPendle = 1;
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments