-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticle.js
137 lines (114 loc) · 4.59 KB
/
Particle.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
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
126
127
128
129
130
131
132
133
134
135
136
137
//Creates a new particle at a point with a velocity and acceleration.
function Particle(point, velocity, acceleration) {
this.position = point || new Vector(0, 0);
this.velocity = velocity || new Vector(0, 0);
this.acceleration = acceleration || new Vector(0, 0);
}
//Moves the particle depending on its acceleration and velocity.
Particle.prototype.move = function() {
//Adds the acceleration to the velocity
this.velocity.add(this.acceleration);
//Adds the velocity to the position.
this.position.add(this.velocity);
};
/* Creates a Wind object.
* Magnitude: Strength of the wind.
* Direction: Direction of the wind.
* Spread: Variability in the wind direction.
*/
function Wind(magnitude, direction, spread) {
this.magnitude = magnitude;
this.direction = direction;
this.spread = spread;
}
/*
* This function does the math for the wind.
* The wind needs to ramp up and down over a bunch of frames.
* Before the particles position changes in each frame, this calculates the wind for that frame.
*/
var x = -2;
var increase = .01;// 4 iterations
var strengthFactor;
function calculateWind() {
if(x >= windiness) {
x = -2;
}
else {
strengthFactor = Math.pow(Math.E,- Math.pow(x, 2));
}
x += increase;
}
// This changes all of the particles velocity depending on the wind speed.
Particle.prototype.blowWind = function(wind) {
var variability = getRandomArbitrary(-wind.spread, wind.spread);
var windStrengthX = wind.magnitude * strengthFactor * Math.cos(wind.direction + variability);
var windStrengthY = wind.magnitude * strengthFactor * Math.sin(wind.direction + variability);
var windVector = new Vector(windStrengthX, windStrengthY);
this.velocity.add(windVector);
};
//Creates a point where particles are emitted.
// Point: Location of the Emitter.
// Velocity: Velocity of the emitted particles.
// Spread: Defines the possible angles that the particles can be emitted.
function Emitter(point, velocity, spread) {
this.position = point;
this.velocity = velocity;
this.spread = spread || 0;
this.drawColor = "#999"; // Color of the emitter;
}
//Spawns a particle from an emitter.
Emitter.prototype.emitParticle = function() {
//Randomized angle to spread.
//var angle = this.velocity.getAngle() + this.spread - (Math.random() * this.spread * 2);
var angle = this.velocity.getAngle() + getRandomArbitrary(-this.spread, this.spread);
// The magnitude of the emitter's velocity
var magnitude = this.velocity.getMagnitude();
// The new particle's position is set to the emitter's position
//ADDS A RANDOM X or Y VALUE TO THE POSITION TO MAKE THEM APPEAR IN A LINE ACROSS THE TOP or SIDE
var position;
if(emitFromTop) {
position = new Vector(getRandomArbitrary(0, canvasWidth), this.position.y);
}
else {
position = new Vector(this.position.x, getRandomArbitrary(0, canvasHeight));
}
var velocity = Vector.fromAngle(angle, magnitude * getRandomArbitrary(minimumSpeed, maximumSpeed));
// Default the particle's acceleration to gravity.
// Creates an acceleration with an X-component of 0 and Y-component of whatever 'gravity' is set to.
// This creates a vertical acceleration of 'gravity'
var acceleration = gravity || new Vector(0,0);
return new Particle(position, velocity, acceleration);
};
// Returns a random number between min (inclusive) and max (exclusive)
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
/*
// Adds an acceleration to the particle to simulate a gust of wind.
Particle.prototype.windEffect = function(wind) {
this.acceleration = new Vector(this.acceleration.x + wind.x/randomFactor,this.acceleration.y + wind.y/randomFactor);
}
*/
// No longer needed.
/*
//Adjust the particles' accelerations due to fields.
//This was added to simulate gravity.
Particle.prototype.submitToFields = function() {
var totalAccelerationX = this.acceleration.x;
var totalAccelerationY = this.acceleration.y;
//for each field
for(var i = 0; i < fields.length; i++) {
var field = fields[i];
//Find the distance between the particle and the field.
var vectorX = field.position.x - this.position.x;
var vectorY = field.position.y - this.position.y;
//Calculate the force acting on the particle.
var force = field.mass / Math.pow(vectorX*vectorX+vectorY*vectorY,1.5);
// Add force to the totalAccelerationX scaled by distance.
totalAccelerationX += vectorX * force;
totalAccelerationY += vectorY * force;
}
// update our particle's acceleration
this.acceleration = new Vector(totalAccelerationX, totalAccelerationY);
};
*/