My code for this newton's cradle isn't working. How do I fix this?

CoolKid :
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.

Rabbid76 :

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I fix a vulnerable npm package in my package-lock.json that isn't listed in the package.json?

Why Isn't my Swift code working?

Why is this code not working, How do i fix it?

How do I add an HTML favicon (mine isn't working)?

discord,js confusion, I want to add a status to my discord bot but my code isn't working properly, how can I make a status for my bot?

Why isn't my pagination code working?

My mac's screen isn't showing. How do I get it to use an external monitor as primary?

How can I best display this nested collection? (Can you tell me why my code isn't working?)

My code isn't giving the user's the correct feedback. Any ideas how to fix this?

Why isn't utorrent working? How do I remedy this problem?

How do I get the count of loans? Why isn't this working?

Timer isn't working correctly in my code

Only dropbox.com isn't resolving on my laptop. How do I fix it?

strtoupper isn't working in my code

How do i fix my code's vulnerability?

How do I fix an issue with my HTML code?. The screen displays text I haven't written

How do I fix it so that my text label isn't cut off at the top when programmatically adding a text label?

The button command in my code isn't working

How do I fix my code about a script that stopped working?

Connect 4 check for win isn't working - how do I fix?

My script isn't working correctly, but I believe that the code is right

How to fix my string concatenation that isn't working?

What's causing segmentation fault in my code and how do I fix it?

Why isn't my function save_users(): working? How do I save these passwords to file?

When I use fetch my .then code isn't working

How do i fix mutating operator isn't mutable error?

My code isn't printing any of the while loop when I give it valid input. How do I fix?

How can I fix my Binary Search code not working as expected?

My Java code isn't executing print statements when the user inputs an option they want to choose. Why is it not working and how do I fix it?