Skip to content

Instantly share code, notes, and snippets.

@snodgrass23
Created April 18, 2014 13:42
Show Gist options
  • Save snodgrass23/11044849 to your computer and use it in GitHub Desktop.
Save snodgrass23/11044849 to your computer and use it in GitHub Desktop.
newton notes
  • docs don't mention the html needs (example needs canvas element)
  • order matters for some methods, but beginning of docs doesn't make it clear until looking at a full demo
  • first example doesn't work with viewport method which I added earlier by following the docs in order (looks like none of examples use viewport after first mentioning it).
  • explain linear force/gravity example better. maybe define vars first with names explaining what those numbers mean or just use inline comment like in many other examples
  • better code docs for inline autocompletion?
  • spring example doesn't work (SpringConstraint not defined)
  • many numbers used (x,y, etc) don't work with one another well when working through the docs in order
@snodgrass23
Copy link
Author

when taking your movement example and playing with a numbers, I ended up with a rope that goes crazy for a few seconds and then calms down into a realistic motion.

<html>
  <head>
    <title>Newton Demo</title>
    <link rel="stylesheet" type="text/css" href="main.css">
    <script src="newton.js"></script>
  </head>
  <body>
    <canvas id="display" width="640" height="480"></canvas>
    <script src="main.js"></script>
  </body>
</html>
var sim = Newton.Simulator();
var display = document.getElementById('display');
var renderer = Newton.GLRenderer(display);
var string = Newton.Body();
var prev, current;

// Build a string to dangle
for (var i = 0; i < 70; i++) {
  current = string.add(Newton.Particle(500 + i * 7, 100));

  // A PinConstraint pins a Particle in place
  if (i === 0) string.add(Newton.PinConstraint(current));

  // A RopeConstraint attaches this particle to the previous particle
  if (prev) string.add(Newton.RopeConstraint(prev, current));
  prev = current;
}

sim.add(string);
renderer.render(sim);

// A LinearForce simulates gravity
sim.add(Newton.LinearForce(3, Math.PI * 1.5));

// A BoxConstraint keeps our string within the viewport
sim.add(Newton.BoxConstraint(0, 0, 640, 480));
sim.start();

@snodgrass23
Copy link
Author

looks like the craziness is when any of the particles of the rope start outside of the constraint. Once they all find there way back in is when it calms down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment