# Problem with springs motion

**URL:** <https://discourse.processing.org/t/problem-with-springs-motion/3394>\
**Category:** Processing.py\
**Created:** [September 9, 2018, 10:00pm UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394 "2018-09-09T22:00:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [September 9, 2018, 10:00pm UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/1 "2018-09-09T22:00:48Z")

</div>

Hi,

I’m working on a sketch where I’m moving specific particles on a grid. Each particle is connected to its neighbour by a spring.

**Goal** : I would like the moving particle to drag/pull its neighbours as it is moving up and down.

**Problem** : I thought putting springs between the particles would do the trick but for some reasons this doesn’t work at all.

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b34794bf57c901fc28c7eda6d4cef8a152b7cc7c.gif)

_gif is glitchy, see video format_ [here](https://streamable.com/lix2g)

Here, the springs on both sides of the moving particle (in red) should normally pull its neighbours up and down, like in the following picture

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0a42688d1d9fe4113cae1f10f80de93ac3575765.png)

**Question** : Why do I get this strange behaviour ? How can I fix it ?

I made a very simple example sketch using the Toxiclibs library. I also added some step-by-step explanations to be as clear as possible.

Any suggestions are more than welcomed !

```auto
add_library('verletphysics')
add_library('toxiclibscore')
from toxi.physics2d import VerletParticle2D
from toxi.physics2d import VerletPhysics2D
from toxi.physics2d.behaviors import GravityBehavior 

pointlist = []
t = 0

def setup():
    global physics
    size(800, 800, P2D)
    smooth(8)
    
    physics = VerletPhysics2D()
    
    #Adding gravity
    physics.addBehavior(GravityBehavior(Vec2D(0, 1)))
    
    
    #Creating particles and adding them to physics engine
    for e in range(31):
        p = VerletParticle2D(width/2 - 15 * 20 + e*20, height/2)
        pointlist.append(p)
        physics.addParticle(p)
        
        
    #Connecting particles with springs
    for i in range(len(pointlist)-1):
        a = pointlist[i]
        b = pointlist[i+1]
        s = VerletSpring2D(a, b, 15, .1)
        physics.addSpring(s)
        
        
    #Locking particles on the edges (far left and far right)
    pointlist[0].lock()
    pointlist[-1].lock()

def draw():
    global t, poitlist
    background(255)
    
    #Running the physics engine
    physics.update()
    
    
    #drawing points
    strokeWeight(8)
    for i, p in enumerate(pointlist):
        
        #if middle point -> change height and add it to pointlist
        if i == 15:
            y = height/2 + sin(t*.05) * 380 
            pointlist[i] = VerletParticle2D(p.x(), y)
        else: 
            y = p.y()
            
        stroke(255, 30, 30) if i == 15 else stroke(0)
        point(p.x(), y)
        
        
    #drawing springs
    strokeWeight(.6)
    for i in range(len(pointlist)-1):
        line(pointlist[i].x(), pointlist[i].y(), pointlist[i+1].x(), pointlist[i+1].y())
        
        
    t += 1

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 10, 2018, 5:23am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/2 "2018-09-10T05:23:49Z")

</div>

Hi solub,

I can’t run your code right now and you video link is broken so I can’t help much.

I just noticed 2 typos in your code:

```auto
#Locking particles on the edges (far left and far right)
    pointlist[0].lock()
    pointlist[-1].lock() // Here with the -1

```

```auto
def draw():
    global t, poitlist // shoudnlt it be pointlist?

```

I’ve never used Python but is this normal that you declare `pointlist` and `t` at the very beginning and once again at the beginning of `draw`?

Are you sure that when you set the positions of your points in draw, it also update the position in the physics engine?

What is the purpose of your `t` variable?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 10, 2018, 5:51am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/3 "2018-09-10T05:51:26Z")

</div>

> [@jb4x](#):
>
> What is the purpose of your `t` variable?

Given `global` _t_ merely increases by `1` at the end of each **draw()**, it can easily be replaced by the PApplet::_frameCount_ field: 😜

> **[frameCount \\ Language (API)](https://py.processing.org/reference/frameCount.html)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

> [@jb4x](#):
>
> I’ve never used Python but, is this normal that you declare _pointlist_ and _t_ at the very beginning and once again at the beginning of **draw()**?

When directly assigning some value to a variable (via the `=` operator or 1 of its composite forms) inside a function:

> **[= (assign) \\ Language (API)](https://py.processing.org/reference/assign.html)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

> **[+= (add assign) \\ Language (API)](https://py.processing.org/reference/addassign.html)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

Python always assumes it’s a local variable! 😮  
Unless we declare it as `global` before using the assign operator: 🌐

> **[Globals \\ Language (API)](https://py.processing.org/reference/globals.html)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

However, b/c variable _pointlist_ isn’t being directly reassigned, but merely its indices:  
`pointlist[i] = VerletParticle2D(p.x(), y)`

There’s no need to declare _pointlist_ as `global` inside **draw()** there. 🤭

And as I had already tipped, if _t_ is replaced by _frameCount_, the `global` statement can be completely removed from **draw()**. 😹

BtW, at `global t, poitlist`, it shoulda been `global t, pointlist`. 🤪

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 10, 2018, 8:20am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/4 "2018-09-10T08:20:04Z")

</div>

> [@jb4x](#):
>
> Are you sure that when you set the positions of your points in draw, it also update the position in the physics engine?

Nice catch! At `pointlist[i] = VerletParticle2D(p.x(), y)`, new VerletParticle2D objects are replacing old 1s. 💱

While in **setup()**, _pointlist_ points to the same VerletParticle2D objects as _physics_’ own internal ArrayList called _particles_. 👉

However in **draw()**, the original VerletParticle2D shared objects are lost! 😱

Therefore, _pointlist_ doesn’t mirror VerletPhysics2D _physics_ anymore! 😞

Maybe rather than instantiating more VerletParticle2D objects, mutate them instead. 💡

Or directly modify VerletPhysics2D _physics_’ own internal ArrayList _particles_, like this p5.js +  
toxiclibs.js sketch does: 🕶

> <https://gist.github.com/GoSubRoutine/f4e383cfc4de8b063df3ee1c3fc66419>
>
> There are more than three files. show original

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 10, 2018, 8:50am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/5 "2018-09-10T08:50:51Z")

</div>

> [@jb4x](#):
>
> I just noticed 2 typos in your code:

> [@jb4x](#):
>
> `pointlist[-1].lock() // Here with the -1`

Index `list[-1]` in Python is the same as last index. That is, `list[len(list) - 1]`. 🐍

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 10, 2018, 8:55am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/6 "2018-09-10T08:55:45Z")

</div>

I knew Python was a really short language for plenty of reasons.  
That’s one more 🙂

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [September 10, 2018, 9:00am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/7 "2018-09-10T09:00:43Z")

</div>

Hi @jb4x,

Thank you for the reply. Indeed, there’s a typo and as @GoToLoop rightly pointed out, there is no need to declare it here as a `global` variable.

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [September 10, 2018, 9:17am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/8 "2018-09-10T09:17:47Z")

</div>

Hi @GoToLoop, thank you for all the suggestions.

> [@GoToLoop](#):
>
> However in **draw()** , the original VerletParticle2D shared objects are lost! 😱
> 
> Therefore, _pointlist_ doesn’t mirror VerletPhysics2D _physics_ anymore! 😞

… and it seems I’m facing again that [old issue](https://forum.processing.org/two/discussion/26484/how-to-give-individual-particles-a-repulsion-force-using-toxiclibs#latest) I couldn’t fix some time ago

> [@GoToLoop](#):
>
> Maybe rather than instantiating more VerletParticle2D objects, mutate them instead. 💡

Could you please tell me what you mean by “mutation” ?

At this point, I’d probably be better off trying to implement my own springs forces… hmm

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 10, 2018, 9:22am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/9 "2018-09-10T09:22:05Z")

</div>

> [@solub](#):
>
> Could you please tell me what you mean by “mutation”?

Reassign an object’s internal variables rather than instantiating another 1. 🤓

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [September 10, 2018, 9:52am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/10 "2018-09-10T09:52:28Z")

</div>

I’m sorry, isn’t that what I’m doing already ?  
How would you do that ?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 10, 2018, 9:58am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/11 "2018-09-10T09:58:16Z")

</div>

> [@solub](#):
>
> I’m sorry, isn’t that what I’m doing already?

You’re creating `new` VerletParticle2D, not mutating existing 1s!

> [@solub](#):
>
> How would you do that?

Check whether a VerletParticle2D got some setting method called **set()** or something like that.  
Or… given you’re only interested in its _y_ field, you may attempt to change it directly.

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [September 10, 2018, 11:02am UTC](https://discourse.processing.org/t/problem-with-springs-motion/3394/12 "2018-09-10T11:02:12Z")

</div>

As @GoToLoop suggested, applying the `addVelocity()` method to the moving point was the way to go.

Thank you !

```auto
add_library('verletphysics')
add_library('toxiclibscore')
from toxi.physics2d import VerletParticle2D
from toxi.physics2d import VerletPhysics2D
from toxi.physics2d.behaviors import GravityBehavior 

pointlist = []
t = 0

def setup():
    global physics
    size(800, 800, P2D)
    smooth(8)
    
    physics = VerletPhysics2D()
    
    #Adding gravity
    physics.addBehavior(GravityBehavior(Vec2D(0, 1)))

    
    #Creating particles and adding them to physics engine
    for e in range(31):
        p = VerletParticle2D(Vec2D(width/2 - 15 * 20 + e*20, height/2))
        pointlist.append(p)
        physics.addParticle(p)
        
        
    #Connecting particles with springs
    for i in range(len(pointlist)-1):
        a = pointlist[i]
        b = pointlist[i+1]
        s = VerletSpring2D(a, b, 15, .1)
        physics.addSpring(s)
        
        
    #Locking particles on the edges (far left and far right)
    pointlist[0].lock()
    pointlist[-1].lock()

def draw():
    global t
    background(255)
    
    #Running the physics engine
    physics.update()
    
    
    #drawing points
    strokeWeight(8)
    for i, p in enumerate(pointlist):

        
        #if middle point -> change height and add it to pointlist
        if i == 15:
            y = height/2 + sin(t * .1) * 10
            p.addVelocity(Vec2D(0, y - height/2))
        stroke(255, 30, 30) if i == 15 else stroke(0)
        point(p.x(), p.y())
        
        
    #drawing springs
    strokeWeight(.6)
    for i in range(len(pointlist)-1):
        line(pointlist[i].x(), pointlist[i].y(), pointlist[i+1].x(), pointlist[i+1].y())
        
        
    t += 1

```

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/46a4594fa6502408dc29a84c0b1500281e550d32.png)
