-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.pde
More file actions
223 lines (170 loc) · 5.66 KB
/
Copy pathProject.pde
File metadata and controls
223 lines (170 loc) · 5.66 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
SimpleUI ui;
TextDisplayBox gravityLabel;
TextDisplayBox ballMassLabel;
TextDisplayBox ballVelLabel;
SimpleButton resetButton;
Slider gravSlider;
Slider ballMassSlider;
Slider ballVelSlider;
SimSurfaceMesh tableTop;
SimSphereMover ball;
SimSurfaceMesh target; //<>//
SimCamera myCam;
float gravMag = 0.7;
PVector gravity;
float ballMass = 0.5;
float ballForce = 0.2;
boolean moving = false; //<>//
float tableTopX = 300;
float tableTopY = 450;
float tableTopZ = -130;
int targetLeftX = 300;
int targetRightX = 600;
int targetTopY = 300;
int targetBottomY = 450;
int targetZ = -100;
boolean ballExists = false;
boolean forwards;
boolean initial = false;
void setup(){
size(950, 700, P3D);
myCam = new SimCamera();
tableTop = new SimSurfaceMesh(20, 35, 17);
ui = new SimpleUI();
gravityLabel = ui.addLabel("Gravity", 140, 20, str(gravMag));
ballMassLabel = ui.addLabel("Ball Mass", 140, 40, str(ballMass));
ballVelLabel = ui.addLabel("Ball Force", 140, 60, str(ballForce));
gravSlider = ui.addSlider("Gravity", 20, 20);
ballMassSlider = ui.addSlider("Ball Mass", 20, 50);
ballVelSlider = ui.addSlider("Ball Force", 20, 80);
resetButton = ui.addSimpleButton("Start/Reset", 20, 200);
PVector tableTopVec = new PVector(tableTopX, tableTopY, tableTopZ);
tableTop.setTransformAbs( 1, 0, 0, 0, tableTopVec);
forwards = true;
String p = sketchPath();
println(p);
int targetPolyX = 40;
int targetPolyY = 20;
target = new SimSurfaceMesh(targetPolyX, targetPolyY, 1);
PVector targetVec = new PVector(targetLeftX, targetTopY, targetZ); //<>//
target.setTransformAbs(8.5, 80, 0, 0, targetVec); //<>//
}
void setBallAtStartPos(){
float ballX = 500;
float ballY = 300;
float ballZ = 300;
int ballRad = 5;
PVector ballVec = new PVector(ballX, ballY, ballZ);
ball = new SimSphereMover(ballVec, ballRad);
gravity = new PVector(0,gravMag * 10000,0);
ball.physics.setMass(ballMass * 10);
moving = true;
}
void draw(){
background(0);
lights();
myCam.update();
myCam.isMoving = false;
pushStyle();
noStroke();
fill(255, 0, 0);
target.drawMe();
popStyle();
pushStyle();
fill(70,127, 70 );
noStroke();
tableTop.drawMe();
popStyle();
PVector ballMove;
if (forwards) {
ballMove = new PVector(0, 0, -(ballForce * 1000000));
} else {
ballMove = new PVector(0, 0, ballForce * 1000000);
}
if (initial == true) {
//reset();
ball.physics.addForce(ballMove);
initial = false;
}
updateBallBounce();
if (ballExists == true) {
pushStyle();
fill(255,150,0);
//stroke(255);
noStroke();
ball.drawMe();
popStyle();
}
myCam.startDrawHUD();
gravityLabel.setText(str(gravMag));
ballMassLabel.setText(str(ballMass));
ballVelLabel.setText(str(ballForce));
ui.update();
myCam.endDrawHUD();
}
void updateBallBounce(){
if (ballExists == true) {
moving = true;
// move the ball in the current ball direction
if(moving) ball.physics.addForce(gravity);
if(ball.physics.location.y >= tableTopY && (ball.physics.location.z <= tableTopZ || ball.physics.location.z >= tableTopZ + 15) && (ball.physics.location.x <= tableTopX || ball.physics.location.x >= tableTopX + 20)) {
//if (ball.collidesWith(tableTop)) {
float mag = ball.physics.velocity.mag();
PVector bounceVector = reflectOffSurface(ball.physics.velocity, new PVector(0,-1,0));
ball.physics.velocity = bounceVector.mult(mag);
}
if (ball.physics.location.z <= targetZ && (ball.physics.location.x >= targetLeftX && ball.physics.location.x <= targetRightX) && (ball.physics.location.y >= targetTopY && ball.physics.location.y <= targetBottomY)) {
forwards = false;
float mag = ball.physics.velocity.mag();
PVector bounceVector = reflectOffSurface(ball.physics.velocity, new PVector(0,0,-1));
ball.physics.velocity = bounceVector.mult(mag);
}
if (ball.physics.location.z >= 500) {
forwards = true;
float mag = ball.physics.velocity.mag();
PVector bounceVector = reflectOffSurface(ball.physics.velocity, new PVector(0,0,1));
ball.physics.velocity = bounceVector.mult(mag);
}
}
}
PVector reflectOffSurface(PVector incident, PVector surfaceNormal){
// Using Fermat's Principle: the formula R = 2(N.I)N-I, where N is the surface normal,
// I is the vector of the incident ray, and R is the resultant reflection.
// First make sure you are working on copies only so changes are not propagated outside the method,
// and that the incident and surfaceNormal vectors are normalized
PVector i = incident.copy();
i.normalize();
PVector n = surfaceNormal.copy();
n.normalize();
// do the vector maths
float n_dot_i_x2 = n.dot(i)*2;
PVector n_dot_i_x2_xn = PVector.mult(n, n_dot_i_x2);
PVector reflection = PVector.sub(n_dot_i_x2_xn,i);
// need to do this to create a reflection "this side" of the surface, not the ray on the other side
reflection.mult(-1);
//println("reflect I,SN,R ", i, n, reflection);
return reflection;
}
void handleUIEvent(UIEventData uied){
if (uied.eventIsFromWidget("Start/Reset")) {
reset();
}
if (uied.eventIsFromWidget("Gravity")) {
gravMag = gravSlider.getSliderValue();
}
if (uied.eventIsFromWidget("Ball Mass")) {
ballMass = ballMassSlider.getSliderValue();
}
if (uied.eventIsFromWidget("Ball Force")) {
ballForce = ballVelSlider.getSliderValue();
}
// here we just get the event to print its self
// with "verbosity" set to 1, (1 = low, 3 = high, 0 = do not print anything)
uied.print(2);
}
void reset () {
setBallAtStartPos();
initial = true;
forwards = true;
ballExists = true;
}