forked from ryancan/special-relativity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
117 lines (83 loc) · 3.28 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
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
var train; //this will be the moving rectangle across screen
var blip; //this will be light/laser particle(s) bouncing between moving train
function setup(){
createCanvas(720,400);
train = new reg; //create new "regular" train where everything appear normal
blip = new blip;//creates new light particle. I would like to be able to generate new ones on click or something
noLoop(); //prevents the draw function from continously looping until otherwise prompted
} //the train rectangle will be drawn in setup tho
function draw(){
background(230);
fill(0);
textSize(12);
text('Hold mouse button to move train, and shoot and trace laser light',20,20);
//class functions for train and light particle
train.move();
train.display();
blip.move();
blip.display();
blip.trace();
}
function mousePressed(){ //when mouse is pressed draw function will loop creating animation
loop();
}
function mouseReleased(){ //when mouse is released after click the animation will cease but objects are still visible where they had stopped
noLoop();
}//the click/release can be continously used and the animation will start/stop from any position
function reg(){
//sets up cooridnates of train and traveling speed
this.x = 100;
this.y = 100;
this.xspeed = 2;
this.yspeed = 0
this.move = function(){
if (this.x > 500){ //stops train after certain point, before off canvas
this.xspeed = 0;
}else{
this.x = this.x + this.xspeed; //moves train at constant speed to right
}
};
this.display = function(){
fill(100,100,100);
rect(this.x,this.y,100,50); //larger rectangle (train)
fill(50,50,50);
rect(this.x+25,this.y+40,10,10); //smaller box on train (light source)
};
}
function blip(){
//sets up coordinates of light partcile and speed based off of train.speeds/positions
this.x = train.x + 30;
this.y = train.y + 40;
this.xspeed = train.xspeed; //laser light shot directly upwards on train will move with train's horizontal velocity
this.yspeed = -1; //as well as the lights vertical speed
this.history = []; //an array used to store the history of (x,y) cooridinates of the laser light "blip"
this.move = function(){
this.x = this.x + this.xspeed;
this.y = this.y + this.yspeed;
if (this.y < 100 || this.y > 145){ //keeps light partcile bouncing in train
this.yspeed = this.yspeed*-1
}else{
}
if (this.x > 530){ //stops laser light after certain point, before off canvas
this.xspeed = 0;
this.yspeed = 0;
}else{
}
var v = createVector(this.x, this.y); //updates history array after every draw itertion
this.history.push(v);
};
this.display = function(){
fill(100,10,10);
rect(this.x,this.y,2,5); //draws tiny red laser rectngle to represent the light
}
this.trace = function(){ //traces the laser light in a specific section to form pretty triangle
for (var i = 87; i < this.history.length; i++) {
var pos = this.history[i];
if (i<=181){ //i = 87, 181 are the initial and terminal indicies of the history array to form pretty triangle
noStroke();
fill(150,50,50);
ellipse(pos.x, pos.y+2.5, 2, 2); //draws a small red ellipse to "trace" the laser light
} else{};
};
}
}