Notion - Update docs

This commit is contained in:
shiffman 2024-02-26 22:00:00 +00:00 committed by GitHub
parent 87496fee12
commit 8a98df860a
8 changed files with 7 additions and 7 deletions

View file

@ -804,7 +804,7 @@ mover.applyForce(force);</code></pre>
<pre class="codesplit" data-code-language="javascript">// Made-up force
let force = createVector(0, 0.1);
mover.applyForce(force);</pre>
<p>I now have the following.</p>
<p>I now have this:</p>
<pre class="codesplit" data-code-language="javascript">//{!1} Attraction force between two objects
<strong>let force = attractor.attract(mover);</strong>
mover.applyForce(force);</pre>

View file

@ -659,7 +659,7 @@ let part2 = Bodies.circle(x + offset, y, r);</pre>
<p>Before moving on, I want to stress that what you draw in your canvas window doesnt magically experience perfect physics just by the mere act of creating Matter.js bodies. The chapters examples have worked because Ive been carefully matching the way Ive drawn each p5.js body with the way Ive defined the geometry of each Matter.js body. If you accidentally draw a shape differently, you wont get an error—not from p5.js or from Matter.js. However, your sketch will look odd, and the physics wont work correctly because the world youre seeing wont be aligned with the world as Matter.js understands it.</p>
<p>To illustrate, let me return to Example 6.5. A lollipop is a compound body consisting of two parts, a rectangle (<code>this.part1</code>) and a circle (<code>this.part2</code>). Ive been drawing each lollipop by getting the positions for the two parts separately: <code>this.part1.position</code> and <code>this.part2.position</code>. However, the overall compound body also has a position, <code>this.body.position</code>. It would be tempting to use that as the position for drawing the rectangle, and to figure out the circles position manually using an offset. After all, thats how I conceived of the compound shape to begin with (look back at Figure 6.8):</p>
<pre class="codesplit" data-code-language="javascript"> show() {
//{!1} Getting the body position rather than the parts.
//{!1} Getting the body position rather than the parts
let position = this.body.position;
let angle = this.body.angle;
push();
@ -983,7 +983,7 @@ function mousePressed() {
<p>The global <code>mousePressed()</code> function in p5.js is executed whenever the mouse is clicked. This is known as a <strong>callback</strong>, a function thats called back at a later time when an event occurs. Matter.js collision events operate in a similar fashion. Instead of p5.js just knowing to look for a function called <code>mousePressed()</code> when a mouse event occurs, however, you have to explicitly define the name for a Matter.js collision callback:</p>
<pre class="codesplit" data-code-language="javascript">Matter.Events.on(engine, 'collisionStart', handleCollisions);</pre>
<p>This code specifies that a function named <code>handleCollisions</code> should be executed whenever a collision between two bodies starts. Matter.js also has events for <code>'collisionActive'</code> (executed over and over for the duration of an ongoing collision) and <code>'collisionEnd'</code> (executed when two bodies stop colliding), but for a basic demonstration, knowing when the collision begins is more than adequate.</p>
<p>Just as <code>mousePressed()</code> is triggered when the mouse is clicked, <code>handleCollisions()</code> (or whatever you choose to name the callback function) is triggered when two shapes collide. It can be written as follows.</p>
<p>Just as <code>mousePressed()</code> is triggered when the mouse is clicked, <code>handleCollisions()</code> (or whatever you choose to name the callback function) is triggered when two shapes collide. It can be written as follows:</p>
<div class="avoid-break">
<pre class="codesplit" data-code-language="javascript">function handleCollisions(event) {
@ -1051,7 +1051,7 @@ function mousePressed() {
<p>Ive managed to get most of the way through this material related to physics simulation without really needing to dive into calculus. As I wrap up the first half of this book, however, its worth taking a moment to examine the calculus behind what Ive been demonstrating and how it relates to the methodology in certain physics libraries (like Box2D, Matter.js, and the upcoming Toxiclibs.js). This way, youll know what to say at the next cocktail party when someone asks you about integration.</p>
<p>Ill begin with a question: “What does integration have to do with position, velocity, and acceleration?” To answer, I should first define <strong>differentiation</strong>, the process of finding a derivative. The <strong>derivative</strong> of a function is a measure of how a function changes over time. Consider position and its derivative. Position is a point in space, while velocity is the change in position over time. Therefore, velocity can be described as the derivative of position. And whats acceleration? The change in velocity over time. Acceleration is the derivative of velocity.</p>
<p><strong>Integration</strong>, the process of finding an integral, is the inverse of differentiation. For example, the <strong>integral</strong> of an objects velocity over time tells us the objects new position when that time period ends. Position is the integral of velocity, and velocity is the integral of acceleration.</p>
<p>Since the physics simulations in this book are founded on the notion of calculating acceleration based on forces, integration is needed to figure out the objects location after a certain period of time (like one cycle of the <code>draw()</code> loop). In other words, youve been doing integration all along! It looks like the following.</p>
<p>Since the physics simulations in this book are founded on the notion of calculating acceleration based on forces, integration is needed to figure out the objects location after a certain period of time (like one cycle of the <code>draw()</code> loop). In other words, youve been doing integration all along! It looks like the following:</p>
<pre class="codesplit" data-code-language="javascript">velocity.add(acceleration);
position.add(velocity);</pre>
<p>This methodology is known as <strong>Euler integration</strong>, or the Euler method (named for the mathematician Leonhard Euler, pronounced <em>Oiler</em>). Its essentially the simplest form of integration and is very easy to implement in code—just two lines! However, while its computationally simple, its by no means the most accurate or stable choice for certain types of simulations.</p>
@ -1540,14 +1540,14 @@ function setup() {
physics = new VerletPhysics2D();
physics.setWorldBounds(new Rect(0, 0, width, height));
physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.5)));
// Particles at vertices of character.
// Particles at vertices of character
particles.push(new Particle(200, 25));
particles.push(new Particle(400, 25));
particles.push(new Particle(350, 125));
particles.push(new Particle(400, 225));
particles.push(new Particle(200, 225));
particles.push(new Particle(250, 125));
// Springs connecting vertices of character.
// Springs connecting vertices of character
springs.push(new Spring(particles[0], particles[1]));
springs.push(new Spring(particles[1], particles[2]));
springs.push(new Spring(particles[2], particles[3]));

View file

@ -427,7 +427,7 @@ if (y > yline) {
<p>I can then make an input array to go with the <code>desired</code> output.</p>
<pre class="codesplit" data-code-language="javascript">// Dont forget to include the bias!
let trainingInputs = [x, y, 1];</pre>
<p>Assuming that I have a <code>perceptron</code> variable, I can train it by providing the inputs along with the desired answer:</p>
<p>Assuming that I have a <code>perceptron</code> variable, I can train it by providing the inputs along with the desired answer.</p>
<pre class="codesplit" data-code-language="javascript">perceptron.train(trainingInputs, desired);</pre>
<p>If I train the perceptron on a new random point (and its answer) for each cycle through <code>draw()</code>, it will gradually get better at classifying the points as above or below the line.</p>
<div data-type="example">

Binary file not shown.

Before

Width:  |  Height:  |  Size: 588 KiB

After

Width:  |  Height:  |  Size: 578 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 375 KiB

After

Width:  |  Height:  |  Size: 321 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 592 KiB

After

Width:  |  Height:  |  Size: 572 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 404 KiB

After

Width:  |  Height:  |  Size: 458 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 828 KiB

After

Width:  |  Height:  |  Size: 855 KiB