Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

  • How to make lines of text move back and forth with sine movements?

    of course it's possible in processing

    we just have to agree how it's working

    Do you think the speed of each line of each pendulum/word is the same?

    Do you think the radius is the same?

    What do you think is the formula to bring the angle of sin in sync with previous line?

    You can only program it if you know how it works.

    new version with less random:

    ArrayList<Mover> movers = new ArrayList();
    
    void setup() {
      size(640, 360);
      String[] words={"=======frequency========", "=======heaven========", "=======success========", 
        "=======frequency========", "=======heaven========", "=======success========", 
        "=======frequency========", "=======heaven========", "=======success========", 
        "=======frequency========", "=======heaven========", "=======success========", 
        "=======frequency========", "=======heaven========", "=======success========", 
        "=======frequency========", "=======heaven========", "=======success========"
      };
      for (int i=0; i<words.length; i++)
        movers.add(new Mover( words[i], 30+ i*30, i*.1 ) );
    }
    
    void draw() {
      background(255);
    
      for (Mover m : movers) {
        m.updateBall();
        m.display();
      }
    }
    
    // ================================================================
    
    class Mover {
    
      PVector location;
    
      // used in the sin formula :
      float locationX;
      float angle; // = random(0, 5*TWO_PI);
      float radius = 99; // random(40, 390);
      float angleSpeed = .2; // random (.01, .1);
    
      String text1;
    
      //constr
      Mover(String text_, float y_, float angle_) {
        text1=text_;
        location = new PVector(-1, y_);
    
        locationX=random(-100, width-55); 
    
        angle=angle_;
    
        //if (random(100)<50) {
        //  angleSpeed*=-1;
        //}
        stroke(0);
        strokeWeight(2);
        fill(127);
        noStroke();
      } //constr
    
      void updateBall() {
        location.x = radius * sin (angle) +locationX;
        angle+=angleSpeed;
      }
    
      void display() {
        textSize(17);
        text(text1, location.x, location.y);
      }
    }//class
    //
    
  • How to tell servo to follow a pendulum movement

    It sounds like you are tracking a physical pendulum with sensors. If you are interested in classes that model pendulums, you also might find this interesting:

    https://forum.processing.org/two/discussion/comment/108565/#Comment_108565

  • How to tell servo to follow a pendulum movement

    I have a pendulum and arduino reading script. I was able to track simple movements, but I cannot make it follow pendulum curve and convert it into degrees.

    #include <Servo.h> 
    
    //load servo to control it
        Servo myservo;  
    #define servoPin 9
    
    int sangle=0;
    //information comming into servo = numbers read from processing
    int inByte=0;
    
    void setup() { 
    //attach a pin 9 to servo
      myservo.attach(servoPin); 
     Serial.begin(9600);
    } 
    
    void loop() { 
    
     if(0<Serial.available()){
      inByte=Serial.read();
     }
     sangle=inByte;
     myservo.write(sangle);
    }
    

    and processing script:

    import processing.serial.*;
    Serial port;
    
    float x= (PI/2);
    float pos=0; 
    float pos1=PI/4;
    byte sangle=0;
    float angle_degrees=0;
    float rotangle=0;
    
    void setup() {
      size(800, 800);
     port=new Serial(this,"COM3",9600);
    }
    
    void draw() {
      background(255);
    
    // float rotangle = atan2(height/4, width/2);
    //my servo is 90 degrees to each side =180
      rotangle=constrain(rotangle,0,PI);
      angle_degrees=degrees(rotangle);
      println(angle_degrees);
    //send information into arduino
      port.write(sangle);
    
      translate(width/2, height/4);
      rotate(pos1*cos(millis()/900.0));
      stroke(0);
      strokeWeight(3);
      line(0, 0, 0, 300);
      fill(#0074FF);
      noStroke();
      ellipse(0, 300, 40, 40);
      println(pos1);
      println(angle_degrees);
      }
    
  • Why doesn't my arc work?

    Somewhat related to creating a Spinner class: two different sketches with examples of Pendulum classes, one with stopping behavior.

    https://forum.processing.org/two/discussion/24743/how-to-create-a-traditional-metronome

  • How to simulate a pendulum with just using forces.

    The problem is that if you run the sketch and check the console output, the length is not constant

    When you said the length wasn't constant, do you mean it gradually increased? I tried this same approach to model a pendulum, but in my sketch, the pendulum constantly floats downwards.

    class GPendulum extends Mover{
      float anchorX;
      float anchorY;
      float len;
    
      public GPendulum(float _anchorX, float _anchorY , float m, float l){
        super(_anchorX+20,_anchorY+l,m);
        anchorX = _anchorX;
        anchorY = _anchorY;
        len = l;
    
      }
      ///*** PI - heading = theta
    
      public void show(){
        ellipse(pos.x,pos.y,30,30);
        line(pos.x,pos.y,anchorX,anchorY);
      }
    
      public void swing(){
    
        PVector gravity = new PVector(0,1); //gravity points straight down
        gravity.normalize();
        gravity.mult(mass*0.1);
        float theta = asin((pos.x-anchorX)/len);
        PVector stringForce = new PVector(0,-1); //start by pointing straight upwards
        stringForce.normalize();
        stringForce.mult(gravity.mag()*cos(theta)+ vel.magSq()/len);
        stringForce.rotate(-theta);
        applyForce(gravity);
        applyForce(stringForce);
        update();
      }
    }
    
    class Mover {
      PVector pos;
      PVector vel;
      PVector accel;
    
      float mass;
      float topSpeed = 7;
      int size = 10;
    
    
      public Mover(float x, float y, float m) {
        pos = new PVector(x, y);
        vel = new PVector(0, 0);
        accel = new PVector(0, 0);
        mass =  m;
      }
      public void show() {
        stroke(#FC6924);
        ellipseMode(CENTER);
        fill(#FC6924);
        ellipse(pos.x,pos.y,size,size);
      }
    
      public void update() {
        vel.add(accel);
        //vel.limit(topSpeed);
        pos.add(vel);
        accel.mult(0);
        //println("New Vel:"+ vel.y);
      }
    
      public void applyForce(PVector f) {
        //println("Applying");
        PVector fc = f.copy();
        accel.add(fc.div(mass));
      }
    }
    
  • How to simulate a pendulum with just using forces.

    Doubles are better, but they still accumulate error. Another (more robust) way to work around cumulative errors is to avoid accumulating processes. There are many ways using the built-in PVector methods. For one example, you could periodically reassert the length of the vector with setMag(), containing drift:

    A common pattern to avoid cumulative drift is to always apply a time step to calculating the position from the origin -- rather than calculating each position from the previous position. But since you've explicitly said you don't want to do that, you could think of this process as being like rigidity in the pendulum string/rod material.

  • How to create a traditional metronome?

    Second suggestion:

    This may be a bit advanced if you aren't yet using objects and classes, but here are some different ways of configuring a pendulum based on a shared class:

    /**
     * Pendulums -- a sine-based pendulum class
     * Jeremy Douglass 2017-04-16 Processing 3.2.2
     * based on a sketch: forum.processing.org/two/discussion/15855/is-it-possible-to-create-a-pendolum-with-sin-and-cos
     **/
    Pendulum pen, penDouble, penOffset, penSlow;
    
    void setup() {
      size(300, 300);
      pen       = new Pendulum(100, PI/100, 0.5, 0, 0);
      penDouble = new Pendulum( 40, PI/50, 0.5, 0, 0);
      penOffset = new Pendulum( 40, PI/100, 0.5, 0, HALF_PI);
      penSlow   = new Pendulum( 40, PI/100, 1.0, 0.002, 0);
    }
    void draw() {
      background(255);
      fill(0);
      text("hold mouse to recharge top: \ntap key to pause middle:", 10, 20);
    
      translate(width/2, height/2);
      penDraw(pen);
      translate(-100, 100);
      penDraw(penDouble);
      translate(200, 0);
      penDraw(penOffset);
      translate(0, -200);
      penDraw(penSlow);
    
      if (mousePressed) {
        penSlow.faster();
      } else {
        penSlow.slower();
      }
    }
    
    void keyReleased() {
      pen.paused = !pen.paused; // freeze-unfreeze pendulum
    }
    
    void penDraw(Pendulum p) {
      if (!p.paused) { 
        p.update();
      }
      pushStyle();
      noFill();
      stroke(128);
      arc(0, 0, 2*p.rod, 2*p.rod, HALF_PI*(1-p.strength), HALF_PI*(1-p.strength) + PI*p.strength);
      stroke(0);
      if (!p.paused) {
        fill(255, 0, 0, p.strength*255);
        ellipse(0, 0, p.rod/2, p.rod/2);
      }
      line(0, 0, p.pos.x, p.pos.y);
      fill(0, 0, 255);
      ellipse(p.pos.x, p.pos.y, p.rod/4, p.rod/4);
      popStyle();
    }
    
    class Pendulum {
      float rod;        // length of the rod
      float speed;      // speed of swing
      float strength;   // strength of swing
      float decay;      // decay of swing
      float time;       // time
      boolean paused;
      PVector pos = new PVector();
    
      Pendulum(float rod_, float speed_, float strength_, float decay_, float time_) {
        rod = rod_;
        speed = speed_;
        strength = strength_;
        decay = decay_;
        time = time_;
      };
      void update() {
        pos.x = sin(HALF_PI*strength*sin(time))*rod;  
        pos.y = cos(HALF_PI*strength*sin(time))*rod;
        time = time + speed;
      }
      void faster() {
        strength = min(1, strength + decay);
      }
      void slower() {
        strength = max(strength - decay, 0);
      }
    }
    
  • How to create a traditional metronome?

    First suggestion:

    There are lots of great "pendulum" discussions in the forums for simple examples:

  • How to create a traditional metronome?

    So what I have currently is basically this:

    float time = millis()/100;
    line(cx,cy,cx+cos(time)(radius0.72),cy+sin(time)(radius0.72));

    (repurposed from this clock code) https://processing.org/examples/clock.html

    Now, I'm confused on how to 'limit' the motion of the line to a certain arc (like a real metronome).

    now what I'm considering is to use an if function to reverse the motion of the 'pendulum' at a certain interval but I have no idea how to even begin to approach that. Or is there an easier function?

  • How to simulate a pendulum with just using forces.

    I know simulating a pendulum can be done by using polar coordinates and angular velocity etc. But I want to do it, by just using the Tension and Gravity forces acting on the 'bob', the resultant of which will 'supposedly' perform the swinging motion. The code I have is as follows:

    The Main Class :

    Mover bob;
    PVector anchor;
    float len;
    PVector tension;
    PVector gravity;
    
    void setup() {
      size(640, 380);
      anchor = new PVector(width/2, 0);
      bob = new Mover();
      gravity = new PVector(0, 0.4);
      tension = new PVector(0, 0);
      len = dist(bob.pos.x, bob.pos.y, anchor.x, anchor.y);
    }
    
    void applyForces() {
      tension = PVector.sub(anchor, bob.pos);
      float str = bob.vel.magSq()/len + gravity.mag()*cos(bob.getAngle(anchor));
      tension.setMag(str);
      bob.applyForce(tension);
      bob.applyForce(gravity);
    
    }
    
    void draw() {
      background(50);
      println(dist(bob.pos.x, bob.pos.y, anchor.x, anchor.y));
      applyForces();
      bob.update();
      bob.render();
    }
    

    The Mover class :

    class Mover {
    
      PVector pos;
      PVector vel;
      PVector acc;
      float mass = 1;
    
      Mover() {
        pos = new PVector(width/2.2, height/4);
        vel = new PVector();
        acc = new PVector();
      }
      float getAngle(PVector anchor) {
        PVector temp1 = PVector.sub(pos, anchor);
        PVector temp2 = PVector.sub(new PVector(anchor.x, height), anchor);
        float angle = PVector.angleBetween(temp1, temp2);
    
        return angle;
      }
      void applyForce(PVector force) {
        PVector f = PVector.div(force, mass);
        acc.add(f);
      }
    
      void update() {
        vel.add(acc);
        pos.add(vel);
    
        acc.mult(0);
      }
    
      void render() {
        fill(255);
        ellipse(pos.x, pos.y, 30, 30);
      }
    }
    
  • Amplitude of simple pendulum increasing after each cycle....

    The amplitude of the pendulum seems to increase after each cycle. I can't figure out why.....

        PVector origin;
        PVector bob;
        float len = 200;
        float angle = PI/4;
        float aVel = 0.0;
        float aAcc = 0.0;
    
        void setup()
        {
          size(640, 360);
          origin = new PVector(width/2, 0);
          bob = new PVector(); 
        }
    
        void draw()
        {
          background(255);
          bob.x = origin.x + len*sin(angle);
          bob.y = origin.y + len*cos(angle);
    
          line(origin.x, origin.y, bob.x, bob.y);
          ellipse(bob.x, bob.y, 40,40);
    
          angle+=aVel;
          aVel+=aAcc;
          aAcc = -0.001*sin(angle);
        }
    
  • How to manipulate an array of pixels wiith indivdual(yet mathematically related) factors

    Did someone say pendulum?

    void setup(){
      size(400,600);
      strokeWeight(2);
      smooth();
    }
    
    void draw(){
      background(0);
      translate(200,30);
      rotate(0.3*cos(radians(millis()/10)));
      translate(-200,-30);
      fill(200,200,50);
      rect(190,20,20,500);
      ellipse(200,520,80,80);
      fill(200,150,0);
      ellipse(200,520,40,40);
      ellipse(200,30,6,6);
    }
    

    Now what's all this about formulas?

  • How to manipulate an array of pixels wiith indivdual(yet mathematically related) factors

    First, i wanted to draw a swinging pendulum The line function didn't cooperate effectively so I tried an arc function, which worked great except it kept drawing the outer circle instead of the radius Then moved onto individual pixels and managed to use an array and a loop to draw a line Challenge is to get each to move with respective factors Starting from (x1,y1) to (x2,y2) that is; horizontal line at y = 0 and different x values for all pixels to vertical line at x = 320 and different y values for all pixels.

    e.g, for point (x = i,y=i): factor =i. so (x+i,y+i) = new point

  • Making Clock with Pendulum

    @TurtleTeaParty -- re:

    I need to use a draw function to draw a line oriented at the center of the frame

    To move the frame of reference during draw() so that (0,0) is at the center of the sketch frame:

    translate(width/2,height/2);
    

    Now, draw a line straight down:

    line(0,0,0,myLength);
    

    But wait -- what if you wanted that straight line to be straight down... in a rotated direction?

    translate(width/2,height/2);
    rotate(myRadians);
    line(0,0,0,myLength);
    

    Since you want to play with the code and learn basic concepts around how to set up line orientations, I'd recommend reading this tutorial:

    Then consider checking out some of the many ways that recent forum-goers have tackled pendulum problems:

  • Making Clock with Pendulum

    Oh, and a pendulum has a formula, one with a bit of math.

  • Making Clock with Pendulum

    The code you have posted runs without issue. It also draws something, and appears to even tell the time (almost)! What is not clear, however, is what you are trying to add/change to it. You want a pendulum? Drawn where? Behind the clock, below it? What does the pendulum look like? Does it swing back and forth? How fast? How far out does it swing?

    I would honestly sugguest you put this body of code on-hold for right now, and instead start with a blank sketch. Then add a pendulum to it - and just a pendulum. And once you have that working, and only then, try adding it to this sketch.

  • Making Clock with Pendulum

    This is for an assignment in my Computational Thinking class, and I am _not _ the best with Processing, so if you could try to just hint me which lines I will need to edit to accomplish certain things (not what to actually do) and/or other lines in the code that I can play with and take example from to perform other actions.

    Currently I am trying to make the pendulum, so I know I need to use a draw function to draw a line oriented at the center of the frame, but I'm not sure how or where. Our teacher gave us a line of code to start us out for the pendulum but I don't know what to do with it. I apologize for the commentary in the code and, again, when it comes to processing, I am not the brightest pixel in the screen.

    void setup()
    {
      size(1000, 2000);
      colorMode(HSB);
      background(0);
      frameRate(30);
      surface.setResizable(false); //I can probably take this out can't i huh
    
    }
    
    
    void draw()
    {
      float h, m, s;
      float radius;
      float cx, cy;
    
      float clockface;
      float hoursRadius, minutesRadius, secondsRadius;
      float minuteDotSize, hourDotSize;
    
      String months[] = {"XXX", "Jan", "Feb", "Mar", "Apr"};
      String days[] = {};
    
      radius = min(height, width)/2.0;
      cx = width/2.0;
      cy = height/4.0;
    
      clockface = radius*0.9;
      hoursRadius = radius*0.50;
      minutesRadius = radius*0.65;
      secondsRadius = radius*0.72;
      minuteDotSize = radius*0.015;
      hourDotSize = radius*0.03;
    
    
      s = second();
      m = minute();
      h = hour()%12; //Will handle this last because it's kiiinda difficult to know if it broke the function or not
    
    
      fill(128);
      noStroke();
      ellipseMode(RADIUS);
      ellipse(cx, cy, clockface, clockface);
    
      textSize(150);
      fill(0, 255, 255);
      textAlign(CENTER, CENTER); //now how do I get it below the clockface
      text(months[1+3], cx, cy); //HOW TO DAYs?
    
    
      for (float pos=0; pos<60; pos+=1) {
        if (pos%5==0) {
          drawTick(cx, cy, pos*6, secondsRadius, hourDotSize);
        } else {
          drawTick(cx, cy, pos*6, secondsRadius, minuteDotSize);
        }
      }
    
    
      drawHand(cx, cy, s*6, secondsRadius, 1);
      drawHand(cx, cy, m*6, minutesRadius, 2);
      drawHand(cx, cy, h*30, hoursRadius, 4);
    }
    
    
    float handPos(float angle)
    {
      return radians(angle-90);
    }
    
    
    color colorPos(float angle)
    {
      return color(map(angle, 0, 360, 0, 255), 255, 255);
    }
    
    
    void drawHand(float cx, float cy, float angle, float len, float weight)
    {
      stroke(colorPos(angle));
      strokeWeight(weight);
      line(cx, cy, cx + cos(handPos(angle))*len, cy + sin(handPos(angle))*len);
    }
    
    
    void drawTick(float cx, float cy, float angle, float len, float size)
    {
      fill(colorPos(angle));
      noStroke();
      rectMode(CENTER);
      rect(cx + cos(handPos(angle))*len, cy + sin(handPos(angle))*len, size, size);
    }
    
    // TO DO AND OR KEEP TESTING AND MESSING UP
    
    void drawNum(float cx, float cy, float angle, float len, float weight, int num)
    //should i be referring to the tick mark code i feel like I should be smart and do that
    
    {
      int maxh; //i don't even know if this is right and I already feel like a genius for trying
      int n;
    
    }
    
    void drawPend(float cx, float cy, float angle, float len, float size, float weight)
    
    {
    //clear() for the clear every frame requirement right?
    
    //pendRadius = radius*1.8;
    //pendSize = hourDotSize*8.0;
    //pendWeight = 3;
    
    }
    
    // told me I had duplicate draw hand, gotta go put more commands in the one draw hand area then?
    // why is coding giving me an existential crisis
    
  • simple pendulum simulator

    @shamsuddeen --

    While thinking about approaches to applying pendulum physics to a demo sketch such as the one by @TfGuy44 : Have you also looked at the previous discussions of a pendulum in the forum? There are a number of them.

    You may also be interested in the Rosetta Code entry for animating a pendulum. It is implemented in Java+Swing, not Processing, but of the principles are similar.