# Forwarding and reversing time in physics sim

**URL:** <https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597>\
**Category:** Coding Questions\
**Created:** [November 17, 2019, 9:03pm UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597 "2019-11-17T21:03:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [November 17, 2019, 9:03pm UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/1 "2019-11-17T21:03:23Z")

</div>

```auto
void update(){
    steps = pos.size()-1;
    text (counter, 150,20);
    fill(255);
      text(index,200,10);
      fill(col);
    if(forward){
      
      if(index==0){
        if(pos.size()>0){
          counter = pos.size()-1;
        }
    //if(lim>0){
    // vx += Limit(vx,lim);
    // vy += Limit(vy,lim);
    //}else{
    
    if(vx>4){
      vx = 4;
    }
    
    if(vx<-4){
      vx = -4;
    }
    if(vy>4){
      vx = 4;
    }
    if(vy<-4){
      vx = -4;
    }
    x += vx;
    y += vy;
    
      pos.add(new PVector(x,y));
      vel.add(new PVector(vx,vy));
    }
    else if(index ==1){
      
      x = pos.get(counter).x;
      y = pos.get(counter).y;
      counter++;
      if(counter == pos.size()-1){
        index = 0;
      }
    }}
    else if(backward){
      if(counter>0){
        index = 1;
      vx = vel.get(counter).x;
      vy = vel.get(counter).y;
      x -= vx;
      y -= vy;
      //x = pos.get(counter).x;
      //y = pos.get(counter).y;
      counter --;
      if(counter<pos.size()-1){
        pos.remove(pos.size()-1);
      }
      if(counter == 0){
        index = 0;
      }
      //steps = counter;
    }}
    else if(pause){
      
    }
  };

```

This is the code I have for a physics sim i am writing, I want it to be able to go forward and backwards, it works fine providing that I go back completely but otherwise the pattern changes. Not sure what to do on this one.

full code

> **[paulgoux/project](https://github.com/paulgoux/project/tree/master/programming%20environment)**
>
> Contribute to paulgoux/project development by creating an account on GitHub.

![com-video-to-gif](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fb50811ae4f518309506540844d5a7ad1e42a8c4.gif)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 18, 2019, 6:51am UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/2 "2019-11-18T06:51:27Z")

</div>

There are some Nice menus!

Chrisir

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [November 18, 2019, 9:06am UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/3 "2019-11-18T09:06:55Z")

</div>

Thanks Chrisir, they are fully customisable, im going to add a hashmap to make click logic more intuitive. The only thing im struggling with currently is the drag function.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [November 18, 2019, 10:48am UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/4 "2019-11-18T10:48:53Z")

</div>

Nice! (So the Big Crunch is the right theory?)

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [November 18, 2019, 12:43pm UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/5 "2019-11-18T12:43:22Z")

</div>

Well if you right click its the big bang

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [November 18, 2019, 1:07pm UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/6 "2019-11-18T13:07:36Z")

</div>

Yes, the infinite loop theory. Could be. Recently was said by the community, that the universe after all is sphere like, not flat!

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [November 18, 2019, 1:16pm UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/7 "2019-11-18T13:16:45Z")

</div>

Oh those crazy sphere universe theorists, it all sounds like tin foil hat stuff to me!

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [November 18, 2019, 2:54pm UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/8 "2019-11-18T14:54:06Z")

</div>

I don’t know what’s more ‘thin foiled’. Big Bounce or M-theory. Any how, I thought your art above was some thought expression.  
P.S B.T.W, I think artists are thin foiled as well.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [November 18, 2019, 3:15pm UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/9 "2019-11-18T15:15:00Z")

</div>

Lol i was making a joke

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [November 22, 2019, 10:33pm UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/10 "2019-11-22T22:33:43Z")

</div>

> [@paulgoux](#):
>
> I want it to be able to go forward and backwards, it works fine providing that I go back completely but otherwise the pattern changes

Your problem might be float precision –  
**edit** sorry, didn’t see that you had a github link, haven’t looked at the full source.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [November 22, 2019, 11:14pm UTC](https://discourse.processing.org/t/forwarding-and-reversing-time-in-physics-sim/15597/11 "2019-11-22T23:14:43Z")

</div>

I partially solved by implementing a counter and not clearing the array if I go backwards. The only issue with this, is that if I added some attractors during the initial forward phase, not deleting the array means when I go backwards means that I cannot add anymore, or rather anything I add wont affect the particles until the forward counter position has reached the end of the array.
