-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.js
More file actions
125 lines (110 loc) · 2.91 KB
/
Particle.js
File metadata and controls
125 lines (110 loc) · 2.91 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class Particle {
/*
* constructor
* Creates particle with given position.
* Default mass is 1.0 kg.
* Velocity and Acceleration vectors are both (0,0).
* Assigns default color of "blue" and radius of 10.
* @param {number} pX - starting position x-coordinate.
* @param {number} pY - starting position y-coordinate.
*/
constructor(pX, pY, mass, num) {
this.posX = pX;
this.posY = pY;
this.mass = mass;
this.num = num;
this.color = "blue";
this.radius = 10;
this.velX = 0.0;
this.velY = 0.0;
this.accX = 0.0;
this.accY = 0.0;
}
/*
* respawn
* Launch particle at given position.
* Velocity and Acceleration vectors are both (0,0).
* @param {number} pX - starting position x-coordinate.
* @param {number} pY - starting position y-coordinate.
*/
respawn(pX, pY) {
this.posX = pX;
this.posY = pY;
}
restartSpeed() {
this.velX = 0.0;
this.velY = 0.0;
this.accX = 0.0;
this.accY = 0.0;
}
flipSpeedX() {
this.velX = -1*this.velX;
this.accX = -1*this.accX;
}
flipSpeedY() {
this.velY = -1*this.velY;
this.accY = -1*this.accY;
}
/*
* setMass
* @param {number} m - mass of particle in kilograms.
*/
setMass(m) {
this.mass = m;
}
/*
* setColor
* Sets draw color of this circular particle.
* @param {string} color - an HTML color name string or hex value.
*/
setColor(color) {
this.color = color;
}
/*
* setRadius
* Sets radius of this circular particle.
* @param {number} r - radius given > 0.
*/
setRadius(r) {
this.radius = r;
}
/*
* setAcceleration
* Set current acceleration applied to this particle.
* @param {number} aX - acceleration x-component.
* @param {number} aY - acceleration y-component.
*/
setAcceleration(aX, aY) {
this.accX = aX;
this.accY = aY;
}
/*
* update
* Increments position by instantaneous velocity and
* increments velocity by instantaneous accelration.
* @param {number} deltaTimeSec - elapsed frame time in seconds.
*/
update(deltaTimeSec) {
// (1) Increment position by current velocity vector.
// separate statement for posX and posY
this.posX += this.velX*deltaTimeSec;
this.posY += this.velY*deltaTimeSec;
// (2) Increment velocity vector by instantaneous acceleration vector.
// seconds x pixels/second/second
this.velX += this.accX*deltaTimeSec;
this.velY += this.accY*deltaTimeSec;
}
/*
* paint
* Draws a circle at particle position filled with color
* and radius as from setColor and setRadius.
* @param {CanvasContext} ctx - Canvas 2D drawing context.
*/
paint(ctx) {
ctx.strokeStyle = this.color;
ctx.beginPath();
ctx.arc(this.posX, this.posY, this.radius, 0, 2*Math.PI);
ctx.stroke();
ctx.strokeText(String(this.num), this.posX, this.posY);
}
}