-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneSimpleAttractor.java
More file actions
51 lines (43 loc) · 1.56 KB
/
Copy pathOneSimpleAttractor.java
File metadata and controls
51 lines (43 loc) · 1.56 KB
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
/*************************************************************************
* Compilation: javac OneSimpleAttractor.java
* Execution: java OneSimpleAttractor
* Dependencies: StdDraw.java
*
* A particle is attracted to the current location of the mouse.
* Incorporates drag.
*
*
* % java OneSimpleAttractor
*
*
* Credits: Jeff Traer-Bernstein
*
*************************************************************************/
public class OneSimpleAttractor {
public static void main(String[] args) {
double rx = 0.0, ry = 0.0; // position
double vx = 0.0, vy = 0.0; // velocity
double mass = 1.0; // mass
double dt = 0.5; // time quantum
double drag = 0.1; // mess around with this a bit
double attractionStrength = 0.01;
// do the animation
while (true) {
// compute the attractive force to the mouse, accounting for drag
double dx = StdDraw.mouseX() - rx;
double dy = StdDraw.mouseY() - ry;
double fx = (dx * attractionStrength) - (drag * vx);
double fy = (dy * attractionStrength) - (drag * vy);
// Euler step: update velocity, then position
vx += fx * dt / mass;
vy += fy * dt / mass;
rx += vx * dt;
ry += vy * dt;
// draw particle
StdDraw.clear();
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.filledCircle(rx, ry, .02);
StdDraw.show(10);
}
}
}