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

Commit 46ca712

Browse files
duskvirkusshiffman
andauthored
Adding page for coding challenge 159 (#3064)
* spelling fix * Adding page for CC159 * adding lint exception * Update 159-simple-pendulum-simulation.md Co-authored-by: Daniel Shiffman <[email protected]>
1 parent a68a039 commit 46ca712

File tree

8 files changed

+182
-0
lines changed

8 files changed

+182
-0
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CodingChallenges/CC_100.1_NeuroEvolution_FlappyBird/P5/neuralnetwork/
66
CodingChallenges/CC_100.5_NeuroEvolution_FlappyBird/P5/neuralnetwork/
77
CodingChallenges/CC_137_4D_Noise_Loop/P5/OpenSimplexNoise.js
88
CodingChallenges/CC_156_Pi_Digits/p5-multi/node
9+
CodingChallenges/CC_159_simple_pendulum_simulation/P5
910
Tutorials/p5.js/10/10.02_p5.js_what_is_JSON_pt1/libraries/p5.serialport.js
1011
Tutorials/p5.js/10/10.03_p5.js_what_is_JSON_pt2/libraries/p5.serialport.js
1112
Tutorials/P5JS/p5.js_video/11.8_p5.js_seriously/libraries/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.min.js"></script>
6+
7+
<meta charset="utf-8" />
8+
</head>
9+
10+
<body>
11+
<script src="sketch.js"></script>
12+
</body>
13+
14+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Simple Pendulum Simulation
2+
// The Coding Train / Daniel Shiffman
3+
// https://thecodingtrain.com/CodingChallenges/159-simple-pendulum-simulation.html
4+
// https://youtu.be/NBWMtlbbOag
5+
// https://editor.p5js.org/codingtrain/sketches/SN-39sHAC
6+
7+
let angle;
8+
9+
let angleV = 0;
10+
let angleA = 0;
11+
12+
let bob;
13+
let len;
14+
let origin;
15+
16+
let gravity = 1;
17+
18+
function setup() {
19+
createCanvas(600, 800);
20+
origin = createVector(300, 0);
21+
angle = PI / 4;
22+
bob = createVector();
23+
len = 200;
24+
}
25+
26+
function draw() {
27+
background(0);
28+
29+
let force = gravity * sin(angle);
30+
angleA = (-1 * force) / len;
31+
angleV += angleA;
32+
angle += angleV;
33+
34+
// angleV *= 0.99;
35+
36+
bob.x = len * sin(angle) + origin.x;
37+
bob.y = len * cos(angle) + origin.y;
38+
39+
stroke(255);
40+
strokeWeight(8);
41+
fill(127);
42+
line(origin.x, origin.y, bob.x, bob.y);
43+
circle(bob.x, bob.y, 64);
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Simple Pendulum Simulation
2+
// The Coding Train / Daniel Shiffman
3+
// https://thecodingtrain.com/CodingChallenges/159-simple-pendulum-simulation.html
4+
// https://youtu.be/NBWMtlbbOag
5+
// https://editor.p5js.org/codingtrain/sketches/SN-39sHAC
6+
7+
float angle;
8+
9+
float angleV = 0;
10+
float angleA = 0;
11+
12+
PVector bob;
13+
float len;
14+
PVector origin;
15+
16+
float gravity = 1;
17+
18+
void setup() {
19+
size(600, 800);
20+
origin = new PVector(300, 0);
21+
angle = PI/4;
22+
bob = new PVector();
23+
len = 200;
24+
}
25+
26+
void draw() {
27+
background(0);
28+
29+
float force = gravity * sin(angle);
30+
angleA = (-1 * force) / len;
31+
angleV += angleA;
32+
angle += angleV;
33+
34+
// angleV *= 0.99
35+
36+
bob.x = len * sin(angle) + origin.x;
37+
bob.y = len * cos(angle) + origin.y;
38+
39+
stroke(255);
40+
strokeWeight(8);
41+
fill(127);
42+
line(origin.x, origin.y, bob.x, bob.y);
43+
circle(bob.x, bob.y, 64);
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: "Simple Pendulum Simulation"
3+
video_number: 159
4+
date: 2021-02-16
5+
video_id: NBWMtlbbOag
6+
web_editor: SN-39sHAC
7+
repository: CC_159_simple_pendulum_simulation
8+
9+
links:
10+
- title: "Nature of Code Playlist"
11+
url: /learning/nature-of-code/index
12+
- title: "Applications of Differential Equations - The Simple Pendulum"
13+
author: "San Joaquin Delta College"
14+
url: http://calculuslab.deltacollege.edu/ODE/7-A-2/7-A-2-h.html
15+
- title: "Simple Pendulum (myPhysicsLab.com)"
16+
url: https://www.myphysicslab.com/pendulum/pendulum-en.html
17+
- title: "Object Oriented Simple Pendulum (Nature of Code Book)"
18+
url: https://github.com/nature-of-code/noc-examples-p5.js/tree/master/chp03_oscillation/NOC_3_10_PendulumExampleSimplified
19+
20+
videos:
21+
- title: "Polar Coordinates - Nature of Code"
22+
author: "The Coding Train"
23+
url: /learning/nature-of-code/3.4-polar-coordinates
24+
- title: "3.2 Angular Motion - Nature of Code"
25+
author: "The Coding Train"
26+
url: /learning/nature-of-code/3.2-angular-motion
27+
- title: "Double Pendulum - Coding Challenge #93"
28+
author: "The Coding Train"
29+
url: /CodingChallenges/093-double-pendulum
30+
- title: "Coding Train Live! (February 6th 2021)"
31+
author: "The Coding Train"
32+
video_id: dpqNqyQCcbY
33+
34+
topics:
35+
- title: "Choo choo!! 2021 Coding Challenge!"
36+
time: "0:00"
37+
- title: "Code! Drawing a bob and an arm."
38+
time: "0:43"
39+
- title: "Explain! How are we going to think about this?"
40+
time: "1:08"
41+
- title: "Code! Add our main variables."
42+
time: "2:55"
43+
- title: "Explain! How do we figure out where the bob is? Trigonometry is the answer!"
44+
time: "3:20"
45+
- title: "Code! Use the polar coordinates formulas we just worked out."
46+
time: "4:39"
47+
- title: "Code! Let's use angular motion!"
48+
time: "6:30"
49+
- title: "Explain! What is the force of the pendulum? Trigonometry is the answer!"
50+
time: "7:55"
51+
- title: "Code! Add the pendulum force."
52+
time: "10:46"
53+
- title: "Whoops! Correction on why we multiply by -1."
54+
time: "12:04"
55+
- title: "Code! Add -1 to the formula."
56+
time: "13:34"
57+
- title: "Whoops! I figured out some things that I never really understood."
58+
time: "13:57"
59+
- title: "Code! Correct the 3 step process."
60+
time: "14:24"
61+
- title: "Something doesn't feel quite right."
62+
time: "15:32"
63+
- title: "Explain! Angular acceleration relates to the arm length!"
64+
time: "16:59"
65+
- title: "Code! Let's divide by length."
66+
time: "18:58"
67+
- title: "Code! You could add some damping."
68+
time: "19:54"
69+
- title: "Ideas! What could you do next?"
70+
time: "20:21"
71+
---
72+
73+
Choo choo! In this challenge, I build on chapter 3 (Oscillating Motion) of the Nature of Code series and simulate a simple pendulum in p5.js via angular acceleration.

_CodingChallenges/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Coding Challenges
33
layout: series-index
44
reverse: true
5+
playlist_id: PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH
56
---
67

78
Watch me take on some viewer submitted Coding Challenges in p5.js and Processing!

_learning/nature-of-code/3.7-additive-waves.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ repository: nature-of-code/3.7-additive-waves
77
can_contribute: true
88
web_editor: qcRsZ_O5a
99

10+
videos:
11+
- title: "Simple Pendulum Simulation"
12+
author: "The Coding Train"
13+
url: /CodingChallenges/159-simple-pendulum-simulation
14+
1015
topics:
1116
- title: "Welcome back!"
1217
time: "0:00"

0 commit comments

Comments
 (0)