Skip to content
This repository was archived by the owner on Jul 5, 2022. It is now read-only.

Commit 5f99d94

Browse files
2.6 mutual attraction (#3597)
* initial commit 2.6 * Finishing timestamps * Add array example, fix timestamps * code examples updated * Add array example, fix timestamps * rewrite description * fix spaces in md * fix spaces in md * Fix timestamp indentation * Try to fix indentation again * adding some video references * add p5 rotate Co-authored-by: Kobe Liesenborgs <[email protected]>
1 parent 4bcdaaa commit 5f99d94

File tree

8 files changed

+1020
-0
lines changed

8 files changed

+1020
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: Mutual Attraction
3+
video_number: 2.6
4+
date: 2022-03-28
5+
video_id: GjbKsOkN1Oc
6+
repository: nature-of-code/2.6_mutual_attraction
7+
can_contribute: true
8+
9+
links:
10+
- title: "Newton's law of universal gravitation (Wikipedia)"
11+
url: https://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation
12+
- title: 'Nature of Code Book - 2.10 Everything Attracts (or Repels) Everything'
13+
url: https://natureofcode.com/book/chapter-2-forces/#210-everything-attracts-or-repels-everything
14+
- title: 'p5.Vector rotate()'
15+
url: https://p5js.org/reference/#/p5.Vector/rotate
16+
- title: 'N-Body Choreographies research paper by Montaldi and Steckles'
17+
url: 'https://personalpages.manchester.ac.uk/staff/j.montaldi/Choreographies/about.html'
18+
- title: 'N-Body Choreographies animation tool by Dan Gries'
19+
url: https://personalpages.manchester.ac.uk/staff/j.montaldi/Choreographies/
20+
21+
variations:
22+
- name: 'Mutual Attraction (N-Body)'
23+
folder: 'mutual-n-body'
24+
lang: 'p5js'
25+
web_editor: bEt7eLZ6Y
26+
- name: 'Mutual Attraction (N-Body) with Barnes Hut'
27+
folder: 'mutual-barnes-hut'
28+
lang: 'p5js'
29+
web_editor: joXNoi9WL
30+
31+
videos:
32+
- title: '2.5 Gravitational Attraction - The Nature of Code'
33+
author: The Coding Train
34+
url: /learning/nature-of-code/2.5-gravitational-attraction
35+
- title: '2.1 Simulating Forces: Gravity and Wind - The Nature of Code'
36+
author: The Coding Train
37+
url: /learning/nature-of-code/2.1-simulating-forces
38+
- title: '5.1 Autonomous Steering Agents - The Nature of Code'
39+
author: The Coding Train
40+
url: /learning/nature-of-code/5.1-autonomous-agents
41+
- title: '3.1 Angles and Rotation - The Nature of Code'
42+
author: The Coding Train
43+
url: /learning/nature-of-code/3.1-angles-rotation
44+
- title: 'Coding Challenge #98.1 - Quadtree'
45+
author: The Coding Train
46+
url: /CodingChallenges/098.1-quadtree
47+
48+
topics:
49+
- title: "Introducing Today's Topic"
50+
time: '0:00'
51+
- title: 'Recap Single attractor system'
52+
time: '0:30'
53+
- title: 'The two body problem'
54+
time: '1:20'
55+
- title: 'Euler Integration'
56+
time: '2:21'
57+
- title: 'Other integration options'
58+
time: '2:53'
59+
- title: 'Coding 2-body simulation'
60+
time: '4:00'
61+
- title: 'The three body problem'
62+
time: '7:56'
63+
- title: 'Coding 3-body simulation'
64+
time: '8:15'
65+
- title: 'Refactoring for n-body'
66+
time: '9:04'
67+
- title: 'Adding a fourth mover'
68+
time: '10:48'
69+
- title: 'Looking at initial states'
70+
time: '11:38'
71+
- title: 'Shape 1: donut'
72+
time: '12:03'
73+
- title: 'The p5 Vector rotate() function'
74+
time: '13:33'
75+
- title: 'Trying out different variations'
76+
time: '16:12'
77+
- title: 'Looking at efficiency'
78+
time: '17:34'
79+
- title: 'Adding the sun'
80+
time: '19:10'
81+
- title: 'Looking at other examples'
82+
time: '20:46'
83+
- title: 'Exercise for viewers'
84+
time: '21:21'
85+
- title: 'Invite to chapter 3 and goodbye'
86+
time: '22:09'
87+
---
88+
89+
Finishing off Chapter 2 on forces, let's look at an "N-Body Simulation" where all movers experience gravitational attraction with all other movers!
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>
5+
<meta charset="utf-8" />
6+
</head>
7+
<body>
8+
<script src="quadtree.js"></script>
9+
<script src="mover.js"></script>
10+
<script src="sketch.js"></script>
11+
</body>
12+
</html>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Mutual Attraction (N-Body Simulation)
2+
// The Nature of Code
3+
// The Coding Train / Daniel Shiffman
4+
// https://youtu.be/GjbKsOkN1Oc?list=PLRqwX-V7Uu6ZV4yEcW3uDwOgGXKUUsPOM
5+
// https://thecodingtrain.com/learning/nature-of-code/2.6-mutual-attraction.html
6+
7+
// N-Body: https://editor.p5js.org/codingtrain/sketches/bEt7eLZ6Y
8+
// N-Body w/ Barnes-Hut: https://editor.p5js.org/codingtrain/sketches/joXNoi9WL
9+
10+
class Mover {
11+
constructor(x, y, vx, vy, m) {
12+
this.pos = createVector(x, y);
13+
this.vel = createVector(vx, vy);
14+
this.acc = createVector(0, 0);
15+
this.mass = m;
16+
this.r = sqrt(this.mass) * 2;
17+
}
18+
19+
applyForce(force) {
20+
let f = p5.Vector.div(force, this.mass);
21+
this.acc.add(f);
22+
}
23+
24+
attract(mover) {
25+
let force = p5.Vector.sub(this.pos, mover.pos);
26+
let distanceSq = constrain(force.magSq(), 100, 1000);
27+
let strength = (G * (this.mass * mover.mass)) / distanceSq;
28+
force.setMag(strength);
29+
mover.applyForce(force);
30+
}
31+
32+
update() {
33+
this.vel.add(this.acc);
34+
this.pos.add(this.vel);
35+
this.vel.limit(15);
36+
this.acc.set(0, 0);
37+
}
38+
39+
show() {
40+
noStroke();
41+
strokeWeight(8);
42+
//fill(255, 200);
43+
//ellipse(this.pos.x, this.pos.y, this.r);
44+
stroke(255);
45+
point(this.pos.x, this.pos.y);
46+
}
47+
}

0 commit comments

Comments
 (0)