How should I get a connected array of circles (like a worm) to move across my canvas in a straight line?









up vote
2
down vote

favorite












void setup()
size(800,800);
background(0);


void draw()
intx= 20,40,60,80,100,120;
fill (255,0,00);
for (int i = 0; i <x.length; i ++)
ellipse (i*6, 100,10,10);




I am trying to get the "worm" to move horizontally in a straight line across my canvas. Attached is the "worm." I don't know where to start with the movement because I need to move the whole array at once. Thank you!










share|improve this question



























    up vote
    2
    down vote

    favorite












    void setup()
    size(800,800);
    background(0);


    void draw()
    intx= 20,40,60,80,100,120;
    fill (255,0,00);
    for (int i = 0; i <x.length; i ++)
    ellipse (i*6, 100,10,10);




    I am trying to get the "worm" to move horizontally in a straight line across my canvas. Attached is the "worm." I don't know where to start with the movement because I need to move the whole array at once. Thank you!










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      void setup()
      size(800,800);
      background(0);


      void draw()
      intx= 20,40,60,80,100,120;
      fill (255,0,00);
      for (int i = 0; i <x.length; i ++)
      ellipse (i*6, 100,10,10);




      I am trying to get the "worm" to move horizontally in a straight line across my canvas. Attached is the "worm." I don't know where to start with the movement because I need to move the whole array at once. Thank you!










      share|improve this question















      void setup()
      size(800,800);
      background(0);


      void draw()
      intx= 20,40,60,80,100,120;
      fill (255,0,00);
      for (int i = 0; i <x.length; i ++)
      ellipse (i*6, 100,10,10);




      I am trying to get the "worm" to move horizontally in a straight line across my canvas. Attached is the "worm." I don't know where to start with the movement because I need to move the whole array at once. Thank you!







      arrays animation processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 20:36









      Kevin Workman

      33.2k53968




      33.2k53968










      asked Nov 11 at 19:56









      G. Prescot

      111




      111






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          Store the current positions of the worm in a global array, where the head is the first position.



          int x= 30,24,18,12,6,0;
          int step = 6;


          Change the position in every frame by the distance between 2 parts of the worm. Shift the current positions in the array in a loop:



          draw() 

          .....
          for (int i = x.length-1; i > 0; --i )
          x[i] = x[i-1];

          x[0] += step;
          .....



          Chang the direction if the end of the window or the begin of the window is reached, by the head of the worm:



          if ( x[0] >= width || x[0] < 0)
          step *= -1;


          Draw the worm in the reverse order:



          for (int i = x.length-1; i >= 0; --i )
          ellipse(x[i], 100, 10, 10);



          The full code may look like this:



          void setup()
          size(200,200);


          int x= 30,24,18,12,6,0;
          int step = 6;

          void draw()


          See the preview:








          share|improve this answer





























            up vote
            0
            down vote













            Moving an array of values isn't much different from moving a single value.



            Let's look at how we'd move a single value:



            float x = 0;

            void draw()
            background(0);
            ellipse(x, height/2, 20, 20);

            x++;



            To move an array of values, you'd do the same thing, but for each index of the array. Something like this:



            float xArray = 20, 40, 60;

            void draw()
            background(0);

            for(int i = 0; i < xArray.length; i++)
            ellipse(xArray[i], height/2, 20, 20);
            xArray[i]++;




            Shameless self-promotion: here is a tutorial on animation in Processing, and here is a tutorial on arrays in Processing.






            share|improve this answer




















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252638%2fhow-should-i-get-a-connected-array-of-circles-like-a-worm-to-move-across-my-ca%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              Store the current positions of the worm in a global array, where the head is the first position.



              int x= 30,24,18,12,6,0;
              int step = 6;


              Change the position in every frame by the distance between 2 parts of the worm. Shift the current positions in the array in a loop:



              draw() 

              .....
              for (int i = x.length-1; i > 0; --i )
              x[i] = x[i-1];

              x[0] += step;
              .....



              Chang the direction if the end of the window or the begin of the window is reached, by the head of the worm:



              if ( x[0] >= width || x[0] < 0)
              step *= -1;


              Draw the worm in the reverse order:



              for (int i = x.length-1; i >= 0; --i )
              ellipse(x[i], 100, 10, 10);



              The full code may look like this:



              void setup()
              size(200,200);


              int x= 30,24,18,12,6,0;
              int step = 6;

              void draw()


              See the preview:








              share|improve this answer


























                up vote
                1
                down vote













                Store the current positions of the worm in a global array, where the head is the first position.



                int x= 30,24,18,12,6,0;
                int step = 6;


                Change the position in every frame by the distance between 2 parts of the worm. Shift the current positions in the array in a loop:



                draw() 

                .....
                for (int i = x.length-1; i > 0; --i )
                x[i] = x[i-1];

                x[0] += step;
                .....



                Chang the direction if the end of the window or the begin of the window is reached, by the head of the worm:



                if ( x[0] >= width || x[0] < 0)
                step *= -1;


                Draw the worm in the reverse order:



                for (int i = x.length-1; i >= 0; --i )
                ellipse(x[i], 100, 10, 10);



                The full code may look like this:



                void setup()
                size(200,200);


                int x= 30,24,18,12,6,0;
                int step = 6;

                void draw()


                See the preview:








                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Store the current positions of the worm in a global array, where the head is the first position.



                  int x= 30,24,18,12,6,0;
                  int step = 6;


                  Change the position in every frame by the distance between 2 parts of the worm. Shift the current positions in the array in a loop:



                  draw() 

                  .....
                  for (int i = x.length-1; i > 0; --i )
                  x[i] = x[i-1];

                  x[0] += step;
                  .....



                  Chang the direction if the end of the window or the begin of the window is reached, by the head of the worm:



                  if ( x[0] >= width || x[0] < 0)
                  step *= -1;


                  Draw the worm in the reverse order:



                  for (int i = x.length-1; i >= 0; --i )
                  ellipse(x[i], 100, 10, 10);



                  The full code may look like this:



                  void setup()
                  size(200,200);


                  int x= 30,24,18,12,6,0;
                  int step = 6;

                  void draw()


                  See the preview:








                  share|improve this answer














                  Store the current positions of the worm in a global array, where the head is the first position.



                  int x= 30,24,18,12,6,0;
                  int step = 6;


                  Change the position in every frame by the distance between 2 parts of the worm. Shift the current positions in the array in a loop:



                  draw() 

                  .....
                  for (int i = x.length-1; i > 0; --i )
                  x[i] = x[i-1];

                  x[0] += step;
                  .....



                  Chang the direction if the end of the window or the begin of the window is reached, by the head of the worm:



                  if ( x[0] >= width || x[0] < 0)
                  step *= -1;


                  Draw the worm in the reverse order:



                  for (int i = x.length-1; i >= 0; --i )
                  ellipse(x[i], 100, 10, 10);



                  The full code may look like this:



                  void setup()
                  size(200,200);


                  int x= 30,24,18,12,6,0;
                  int step = 6;

                  void draw()


                  See the preview:









                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 20:33

























                  answered Nov 11 at 20:11









                  Rabbid76

                  31.5k112842




                  31.5k112842






















                      up vote
                      0
                      down vote













                      Moving an array of values isn't much different from moving a single value.



                      Let's look at how we'd move a single value:



                      float x = 0;

                      void draw()
                      background(0);
                      ellipse(x, height/2, 20, 20);

                      x++;



                      To move an array of values, you'd do the same thing, but for each index of the array. Something like this:



                      float xArray = 20, 40, 60;

                      void draw()
                      background(0);

                      for(int i = 0; i < xArray.length; i++)
                      ellipse(xArray[i], height/2, 20, 20);
                      xArray[i]++;




                      Shameless self-promotion: here is a tutorial on animation in Processing, and here is a tutorial on arrays in Processing.






                      share|improve this answer
























                        up vote
                        0
                        down vote













                        Moving an array of values isn't much different from moving a single value.



                        Let's look at how we'd move a single value:



                        float x = 0;

                        void draw()
                        background(0);
                        ellipse(x, height/2, 20, 20);

                        x++;



                        To move an array of values, you'd do the same thing, but for each index of the array. Something like this:



                        float xArray = 20, 40, 60;

                        void draw()
                        background(0);

                        for(int i = 0; i < xArray.length; i++)
                        ellipse(xArray[i], height/2, 20, 20);
                        xArray[i]++;




                        Shameless self-promotion: here is a tutorial on animation in Processing, and here is a tutorial on arrays in Processing.






                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Moving an array of values isn't much different from moving a single value.



                          Let's look at how we'd move a single value:



                          float x = 0;

                          void draw()
                          background(0);
                          ellipse(x, height/2, 20, 20);

                          x++;



                          To move an array of values, you'd do the same thing, but for each index of the array. Something like this:



                          float xArray = 20, 40, 60;

                          void draw()
                          background(0);

                          for(int i = 0; i < xArray.length; i++)
                          ellipse(xArray[i], height/2, 20, 20);
                          xArray[i]++;




                          Shameless self-promotion: here is a tutorial on animation in Processing, and here is a tutorial on arrays in Processing.






                          share|improve this answer












                          Moving an array of values isn't much different from moving a single value.



                          Let's look at how we'd move a single value:



                          float x = 0;

                          void draw()
                          background(0);
                          ellipse(x, height/2, 20, 20);

                          x++;



                          To move an array of values, you'd do the same thing, but for each index of the array. Something like this:



                          float xArray = 20, 40, 60;

                          void draw()
                          background(0);

                          for(int i = 0; i < xArray.length; i++)
                          ellipse(xArray[i], height/2, 20, 20);
                          xArray[i]++;




                          Shameless self-promotion: here is a tutorial on animation in Processing, and here is a tutorial on arrays in Processing.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 11 at 20:35









                          Kevin Workman

                          33.2k53968




                          33.2k53968



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252638%2fhow-should-i-get-a-connected-array-of-circles-like-a-worm-to-move-across-my-ca%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              政党

                              天津地下鉄3号線