-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
executable file
·368 lines (276 loc) · 8.47 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Calculating force vectors
Whenever a new force vector is applied to a physics object, the new vector should be added to any existing vector, so that the physics object
would only have one force vector at any given time. Gravity and any accelrating forces should be calculated to get a vector that would be
applied to the physics object.
Collisions
*/
Constants = {
Jump_Force : -130
}
class Point {
constructor(x,y) {
this.x =x;
this.y =y;
}
}
class Utilities {
constructor(){}
genUniqueID(){
// gen a 32 char unique Alpha-numeric ID
let uniqueID = "";
while(uniqueID.length < 32) {
// decide on next character Letter or Number
let flip = Math.random() * 1;
let nextChar = "";
if(flip < .5) {
// random letter
let nextCode = Math.floor((Math.random() * 25 ) + 65);
// console.log("Next code=" + nextCode);
nextChar = String.fromCharCode(nextCode);
// console.log("next CHAR=" + nextChar);
}
else {
// random number
nextChar = Math.floor(Math.random() * 10);
// console.log("next digit=" + nextChar);
}
uniqueID += nextChar;
}
return uniqueID;
}
}
class PhysicsBody {
constructor(tl, br){
this.isStatic = false;
this.ID = util.genUniqueID();
this.size = {
width : Math.abs(tl.x - br.x),
height : Math.abs(tl.y - br.y)
};
this.bondingBox = {
topLeft : tl,
bottomRight : br
};
this.position = {
x : tl.x,
y : tl.y
};
this.velocity = {
x: 0,
y: 0
};
this.forces = [] // an array of force vectors being applied
}
setPos(newPos) {
this.position = newPos;
this.bondingBox.topLeft = new Point(this.position.x, this.position.y);
this.bondingBox.bottomRight = new Point(this.position.x + this.size.width, this.position.y + this.size.height);
if(!this.isStatic) console.log(this);
}
applyForce(fvector) {
console.log("a force has been applied to :[" + this.ID + "]" );
this.forces.push(fvector);
}
}
class Collision {
constructor() {
this.po1_id = "";
this.po1_side = ""; // l(eft), r(ight), t(op), b(ottom)
this.po2_id = "";
this.po2_side = "";
}
}
class PhysicsEngine{
constructor() {
this.gravity = 9.28;
this.objectList = []
}
isInBetween(i, l, r){
return(i >= l && i <= r);
}
objectsCollided(po1, po2) {
// determine if and what side of po1 collided. If yes, deterine which side of po2 collided
// check left vs right
let po2_right = po2.bondingBox.bottomRight.x;
let po2_left = po2.position.x;
let po2_top = po2.position.y;
let po2_bottom = po2.bondingBox.bottomRight.y;
// check for top or bottom
if(this.isInBetween(po1.position.x, po2_left, po2_right) && this.isInBetween(po1.position.y, po2_top, po2_bottom)) {
if(po1.position.y + po1.size.height > po2_bottom) return "bottom";
else return "top";
}
// check for left or right
if(this.isInBetween(po1.position.y, po2_top, po2_bottom)) {
if(this.isInBetween(po1.position.x, po2_left, po2_right)) {
if(po1.bondingBox.bottomRight.x > po2_right) return "right";
else return "left";
}
}
return "no-collision";
}
physicsBody(pos, size) {
let tl = new Point(pos.x - (size.width/2), pos.y - (size.height/2));
let br = new Point(pos.x + (size.width/2), pos.y + (size.height/2));
let pb = new PhysicsBody(tl, br);
this.objectList.push(pb);
return pb;
}
forceVector(x_force,y_force) {
return {
x: x_force,
y: y_force,
accelerationY : 0,
accelerationX : 0,
accelerationX_start : 0,
accelerationY_start : 0
}
}
applyAccelerationToForce(f) {
if(f.accelerationX || f.accelerationY) {
console.log("calculating acceleration...");
console.log("Acceleration_y=", f.accelerationY)
if(f.accelerationX) {
console.log("not doing x acceleration yet");
}
if(f.accelerationY) {
f.y += f.accelerationY * (Date.now() - f.accelerationY_start)/10000;
}
}
else {
// decrease force over time ----
f.x = (f.x/2);
f.y = (f.y/2);
}
}
calcCollisions(){
for(let i=0; i < this.objectList.length; i++) {
let po = this.objectList[i];
if(po.isStatic) continue;
for(let j=0; j < this.objectList.length; j++) {
let po2 = this.objectList[j];
if(po.ID == po2.ID) continue; // skip if same object
let collision_side = this.objectsCollided(po, po2);
if(collision_side != "no-collision")
console.log(collision_side);
}
}
}
calcForces(){
for(let i=0; i < this.objectList.length; i++) {
let po = this.objectList[i];
if(po.isStatic) continue;
for(let j=0; j < po.forces.length; j++) {
let f = po.forces[j];
this.applyAccelerationToForce(f);
po.setPos(new Point(po.position.x + f.x, po.position.y + f.y));
// remove force if it is too small
if( (Math.abs(f.x) < 0.01) && (Math.abs(f.y) < 0.01) ) {
console.log("Force has depleted and will be removed");
po.forces.splice(po.forces.indexOf(f,1));
}
}
}
}
}
class GameObject {
constructor(_pos, _width, _height) {
this.size = {
width: _width,
height: _height
};
this.color = "#f5f4f0";
this.isVisible = true;
this.physicsBody = Physics.physicsBody(_pos, this.size);
this.visual = null;
this.hasGravity = false;
}
applyGravity(){
if(this.hasGravity) return;
console.log("applying gravity to " + this.physicsBody.ID);
let gravity = Physics.forceVector(0, 0);
gravity.accelerationX = 0;
gravity.accelerationY = 9.8;
gravity.accelerationY_start = Date.now();
this.physicsBody.applyForce(gravity);
this.hasGravity = true;
}
isOutofBounds(){
return (this.physicsBody.bondingBox.bottomRight.y >= height);
}
removeAllForces(){
console.log("removing all forces");
this.physicsBody.forces.splice(0,this.physicsBody.forces.length);
this.hasGravity = false;
}
draw() {
if(!this.isVisible) return;
if(this.isOutofBounds() && !this.physicsBody.isStatic) {
// console.log("out of bounds. resetting everything");
this.removeAllForces();
this.physicsBody.setPos(new Point(this.physicsBody.position.x, height - (this.physicsBody.size.height + 1) ));
}
if(this.visual != null) {
console.log("visuals are not implemented yet");
}
else {
push();
fill(this.color);
rect(this.physicsBody.position.x, this.physicsBody.position.y, this.size.width, this.size.height);
pop();
}
}
}
let hero;
let Physics;
let floor;
function draw() {
background("#15291a");
// calculate
Physics.calcForces();
Physics.calcCollisions();
// redraw characters
hero.draw();
floor.draw();
}
function keyPressed(){
switch(keyCode) {
case LEFT_ARROW:
hero.physicsBody.applyForce(Physics.forceVector(-5,0));
break;
case RIGHT_ARROW:
hero.physicsBody.applyForce(Physics.forceVector(5,0));
break;
case 32: // space bar
hero.applyGravity();
hero.physicsBody.applyForce(Physics.forceVector(0,Constants.Jump_Force));
break;
default:
console.log(keyCode);
}
}
function setupFloor() {
floor = new GameObject(new Point(0 + width/2,height), width, 10 );
floor.color = "#fcba03";
floor.physicsBody.isStatic = true;
}
function setup() {
createCanvas(400, 400);
util = new Utilities();
Physics = new PhysicsEngine();
hero = new GameObject(new Point(width/2,0), 10,20 );
hero.applyGravity();
setupFloor();
}
/* ---------------------
Movement implementation (basic)
No physics
Right, Left, jump
Jump implementation :
activated by space bar
activated only while on ground
object will go up for a specified distance
No physics
Implemented via the Game-loop
------------------------- */