SHARE
TWEET

Untitled

a guest Aug 20th, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <div id='simulationBox1'></div>
  2. <div id='simulationInfo1'></div>
  3. <div id='simulationInfo2'></div>
  4. <script src="/matter.min.js" type="text/javascript"></script>
  5. <script type="text/javascript">
  6. var particleEngine = Matter.Engine.create(document.body, {render: {element:document.getElementById("simulationBox1"), options: {wireframes: true, showAngleIndicator: false}}, constraintIterations:300, positionIterations:20, timing:{timeScale: 0.1}});
  7. var N = 100; // Number of pearls
  8. var S = 5; // Pearl radius
  9. var L = 10; // Link length
  10. var M = 730; // Number of filling particles (try 730)
  11. document.getElementById('simulationInfo1').innerHTML='<b>Simulation 1</b><br />Number of pearls: N = '+N+'<br />Pearl radius: S = '+S+'<br /> Link length: L = '+L+'<br /> Filling particle count: M = '+ M;
  12. particleEngine.world.gravity.y = 0.04; // try 0.04
  13. var stack = Matter.Composite.create();
  14. for (var i = 0; i < M; i++) {
  15.   Matter.Composite.add(stack, Matter.Bodies.circle(400+Matter.Common.random(-N*L/Math.PI/3, N*L/Math.PI/3), 15+N*L/Math.PI/2+Matter.Common.random(-N*L/Math.PI/3, N*L/Math.PI/3), Matter.Common.random(2, 8), { friction: 0, restitution: 0, density: 1, collisionFilter: {category: 1}}));
  16. };
  17. var bridge = Matter.Composite.create();
  18. for (var i = 0; i < N; i++) {
  19.   Matter.Composite.add(bridge, Matter.Bodies.circle(400 + N*L/(Math.PI*2)*Math.sin(2*Math.PI*i/N), 15+N*L/(Math.PI*2) - N*L/(Math.PI*2)*Math.cos(2*Math.PI*i/N), S, {friction: 0, restitution: 0, density:0.5, collisionFilter: {category: 2, mask: 1}, isStatic: i == 0}));
  20. }
  21. for (var i = 0; i < N; i++) {
  22.   Matter.World.add(particleEngine.world, Matter.Constraint.create({bodyA: bridge.bodies[i], pointA: { x: 0, y: 0 }, bodyB: bridge.bodies[(i+1)%N], pointB: { x: 0, y: 0 }, stiffness:7, length:L,render:{strokeStyle:'#00ffff'}}));
  23. }
  24. Matter.World.add(particleEngine.world, [
  25.   stack,
  26.   bridge,
  27.   Matter.Bodies.rectangle(400, -40, 800, 10, { isStatic: true })
  28. ]);
  29. Matter.Events.on(particleEngine.render, "afterRender", function(a){
  30.   var str = 'Circumference:';
  31.   var checksum = 0;
  32.   var len = 0;
  33.   for (var i = 0; i < N; i++) {
  34.     len += Math.sqrt(Math.pow(bridge.bodies[i].position.x-bridge.bodies[(i+1)%N].position.x, 2) + Math.pow(bridge.bodies[i].position.y-bridge.bodies[(i+1)%N].position.y, 2));
  35.   }
  36.   var x0 = bridge.bodies[0].position.x-bridge.bodies[1].position.x;
  37.   var y0 = bridge.bodies[0].position.y-bridge.bodies[1].position.y;
  38.   var x1 = bridge.bodies[0].position.x-bridge.bodies[N-1].position.x;
  39.   var y1 = bridge.bodies[0].position.y-bridge.bodies[N-1].position.y;
  40.   str += ' '+len.toString()+'<br /> Tail tip angle (degrees): '+(((Math.atan2(y1, x1) - Math.atan2(y0, x0))*180/Math.PI + 360)%360).toString()+'<p />';
  41.   document.getElementById('simulationInfo2').innerHTML=str;
  42. });
  43. var lemniscate = Matter.Composite.create()
  44. var trix = Matter.Composite.create()
  45. for (var i = 0; i < N; i++) {
  46.   var s = 1.9150080481545374814*(2.0*i/(N)-1);
  47.   Matter.Composite.add(trix, Matter.Bodies.circle(400 + L*N*(s - 2*Math.tanh(s))/3.8295479369488985, 15 + L*N*(2/Math.cosh(s) - 0.57683981788998291629)/3.8295479369488985, 1, {isStatic: true}));
  48. }
  49. for (var i = 0; i < N; i++) {
  50.   Matter.World.add(particleEngine.world, Matter.Constraint.create({bodyA: trix.bodies[i], bodyB: trix.bodies[(i+1)%N],render:{strokeStyle:'#ff0000'}}));
  51. }
  52. Matter.Engine.run(particleEngine);
  53. </script>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top