forked from nature-of-code/noc-examples-p5.js-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
55 lines (40 loc) · 1.01 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Mover object
let bob;
// Spring object
let spring;
function setup() {
createCanvas(640, 360);
setFrameRate(60);
// Create objects at starting position
// Note third argument in Spring constructor is "rest length"
spring = new Spring(width / 2, 10, 100);
bob = new Bob(width / 2, 100);
}
function draw() {
background(51);
// Apply a gravity force to the bob
let gravity = createVector(0, 2);
bob.applyForce(gravity);
// Connect the bob to the spring (this calculates the force)
spring.connect(bob);
// Constrain spring distance between min and max
spring.constrainLength(bob, 30, 200);
// Update bob
bob.update();
// Draw everything
spring.displayLine(bob); // Draw a line between spring and bob
bob.display();
spring.display();
}
function mousePressed() {
bob.handleClick(mouseX, mouseY);
}
function mouseDragged() {
bob.handleDrag(mouseX, mouseY);
}
function mouseReleased() {
bob.stopDragging();
}