How to make verlet integration collisions more stable? : <p>I'm not using any engine, but instead trying to build my own softbody dynamics for fun using verlet integeration. I made a cube defined by 4x4 points with segments keeping its shape like so:</p>

<p><a href="https://i.stack.imgur.com/3RzPH.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/3RzPH.jpg" alt="The structure of my box"></a></p>

<p>I have the points collide against the edges of the scene and it seems to work fine. Though I do get some cases where the points collapses in itself and it'll create a dent instead of maintaining its box shape. For example, if it's a high enough velocity and it lands on its corner it tends to crumble:</p>

<p><a href="https://i.stack.imgur.com/KmNiK.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/KmNiK.jpg" alt="enter image description here"></a></p>

<p>I must be doing something wrong or out of order when solving the collision.
Here's how I'm handling it. It's in Javascript, though the language doesn't matter, feel free to reply with any language:</p>

<pre><code>sim = function() {
    // Sim all points.
    for (let i = 0; i &lt; this.points.length; i++) { 
        this.points[i].sim();
    }

    // Keep in bounds.
    let border = 100;

    for (let i = 0; i &lt; this.points.length; i++) { 
        let p = this.points[i];

        let vx = p.pos.x - p.oldPos.x;
        let vy = p.pos.y - p.oldPos.y;

        if (p.pos.y &gt; height - border) {
         // Bottom screen
         p.pos.y = height - border;
         p.oldPos.y = p.pos.y + vy;
        } else if (p.pos.y &lt; 0 + border) {
         // Top screen
         p.pos.y = 0 + border;
         p.oldPos.y = p.pos.y + vy;
        }

        if (p.pos.x &lt; 0 + border) {
         // Left screen
         p.pos.x = 0 + border;
         p.oldPos.x = p.pos.x + vx;
        } else if (p.pos.x &gt; width - border) {
         // Right screen
         p.pos.x = width - border;
         p.oldPos.x = p.pos.x + vx;
        }  
    }

    // Sim its segments.
    let timesteps = 20;

    for (let ts = 0; ts &lt; timesteps; ts++) {
        for (let i = 0; i &lt; this.segments.length; i++) { 
            this.segments[i].sim();
        }
    }
}
</code></pre>

<p>Please let me know if I need to post any other details.</p>
