{"id":238,"date":"2012-06-20T12:37:46","date_gmt":"2012-06-20T00:37:46","guid":{"rendered":"http:\/\/wp.davidbrebner.com\/?p=238"},"modified":"2013-04-28T20:49:28","modified_gmt":"2013-04-28T08:49:28","slug":"string-physics","status":"publish","type":"post","link":"https:\/\/www.davidbrebner.com\/?p=238","title":{"rendered":"String Physics"},"content":{"rendered":"<p>These videos show simulated string with different properties.<\/p>\n<p>\n\t\t\t\t\t\t<iframe width=\"420\" height=\"315\" src=\"http:\/\/www.youtube.com\/embed\/fpD78xIZls0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>\n\t\t\t\t\t\t\tStiff string physics.<br \/>\n\t\t\t\t\t\t\t<P><br \/>\n\t\t\t\t\t\t\t<iframe width=\"420\" height=\"315\" src=\"http:\/\/www.youtube.com\/embed\/VF_YXGiDcxo\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>\n\t\t\t\t\t\t\tStretchy string physics.<\/p>\n<p>\n\t\t\t\t\t\t\t\t\tThis is created using a 2d Verlet integrator.  The string is composed of a line of points connected by springs. By applying different values to the springyness of the joints between the points and by altering the gravity value the behaviour of the string changes.  This approach was first written about in 1791 by Delambre, but most recently rediscovered by Loup Verlet in 1967.  It&#8217;s really very simple to implement.<\/p>\n<p><pre>\r\nmethod verlet()\r\n\tint a=0\r\n\t\r\n\tloop a<.points\r\n\t\treal oldx = .node_x[a]\r\n\t\treal oldy = .node_y[a]\r\n\t\t\r\n\t\t\/\/apply verlet\r\n\t\t.node_x[a] = (.node_x[a]*2 - .node_oldx[a])\r\n\t\t.node_y[a] = (.node_y[a]*2 - .node_oldy[a]) + .gravity\r\n\t\t\r\n\t\t.node_oldx[a] = oldx\r\n\t\t.node_oldy[a] = oldy\r\n\t\ta++\r\n\tend\r\nend<\/pre>\n<p>This is the core function.  This goes through the list of nodes or points in the string individually and essentially inserts a force between the last known position and the new position we are calculating.  No forces between nodes are considered at this point.<\/p>\n<pre>\r\nmethod constraints()\r\n\tint a=0\r\n\treal line_len_sq = 1.0*1.0\r\n\t\/\/now scale the points towards the correct length\r\n\tloop a<.points\r\n\t\t\/\/distance from this to the next point\r\n\t\treal deltax = .node_x[a] - .node_x[a+1]\r\n\t\treal deltay = .node_y[a] - .node_y[a+1]\r\n\t\t\/\/generate a scale of how far off the correct length\r\n\t\treal vscale = line_len_sq \/ (deltax*deltax + deltay*deltay + line_len_sq) - 0.5\r\n\t\t\/\/now move both points a fraction towards each other\r\n\t\t.node_x[a] += deltax * vscale\r\n\t\t.node_y[a] += deltay * vscale\r\n\t\t.node_x[a+1] -= deltax * vscale\r\n\t\t.node_y[a+1] -= deltay * vscale\r\n\t\ta++\r\n\tend\r\nend\r\n<\/pre>\n<p>The constraints compare connected points.  In this case we know it's a string so all the points are in order, so we go through each pair in order trying to adjust the position of the nodes so that they are the correct length apart.<\/p>\n<pre>\r\nmethod update()\r\n\t\/\/first apply gravity inside verlet \r\n\t.verlet()\r\n\t\r\n\t\/\/then restitution, \r\n\t\/\/try and remake points the same distance apart\r\n\t.constraints()\r\n\t\t\r\n\t\/\/then render\r\n\t.render()\r\nend\r\n<\/pre>\n<p>This is the heartbeat, it essentially applies the gravity during the verlet pass, applies the constraints and then renders. Magic!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>These videos show simulated string with different properties. Stiff string physics. Stretchy string physics. This is created using a 2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[3,24,25],"class_list":["post-238","post","type-post","status-publish","format-standard","hentry","category-umaijn","tag-umajin","tag-physics","tag-verlet"],"_links":{"self":[{"href":"https:\/\/www.davidbrebner.com\/index.php?rest_route=\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.davidbrebner.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.davidbrebner.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.davidbrebner.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.davidbrebner.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=238"}],"version-history":[{"count":6,"href":"https:\/\/www.davidbrebner.com\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":491,"href":"https:\/\/www.davidbrebner.com\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions\/491"}],"wp:attachment":[{"href":"https:\/\/www.davidbrebner.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.davidbrebner.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.davidbrebner.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}