-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4classData.js
1778 lines (1669 loc) · 53.9 KB
/
4classData.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//empty as of yet
//not anymore its not
//copied from Chase a little dot 2-1
//at 8/29/2022 2:53 PM CST
//wisdom teeth removed today at like 8 idk and now school starts
//next thursday... that will be fun...
//player
//black hole
//target
//anything else?
//white hole - pushes you away, if you manage to get into it you get a point lol (remember to only allow one point) <- who ever typed "this" is weird <- lol nobody typed the word there except you in this script oh wait i just did
//ctx.line(2,3,4,5,6);
//the "this" keyword is actually very nice what do you mean; those who DONT type it are weird. CoolMan here obviously not kitty didn't know about it in either C++ or Javascript so he was sad and his code was dog solid and gas and plasma but not liquid his constructores were so able to be taired and bruh input r/ihadastroke
//allow references without crash
//WHAT DO YOU MEAN m is not a function I JUST MADE IT A FUNCTION
//function m(){console.log("WHAT");}
//three();
//what, is it some chance thing?
//for(var i=0;i<90;i++){
// console.log("what");
// }
//physics class, such that every object should have these methods
//-8:39 AM CST 11/28/2022 Monday
class PhysicsObject{
attractSingle(obj){
//object minus this for object to this vector
var vector=[
obj.pos[0]-this.pos[0],
obj.pos[1]-this.pos[1]
];
var vectorMagnitude=Math.sqrt(vector[0]**2+vector[1]**2);
var normalizedVector=[
vector[0]/vectorMagnitude,
vector[1]/vectorMagnitude
];
//change the speeds
obj.vel[0]+=normalizedVector[0]*this.force; //here was the
obj.vel[0]+=normalizedVector[1]*this.force; //solution all along!
}
//both way attraction by mass
attractBoth(obj,mult){
if(this==obj){
//changed from console.log to error.log...
//whoops not that its meant to be console.error...
//at 8/30/2022 10:20 AM CST Tueday
console.error("Same object interacting with self in attractBoth error");
}
//console.log(obj);
//object minus this for object to this vector
//check the numbers to make sure they are not undefined
//console.log(obj.pos,this.pos);
//console.log(obj.pos[0],obj.pos[1],this.pos[0],this.pos[1]);
if(
obj.pos[0]==undefined
||obj.pos[1]==undefined
||this.pos[0]==undefined
||this.pos[1]==undefined
||obj.pos[0]=="NaN"
||obj.pos[1]=="NaN"
||this.pos[0]=="NaN"
||this.pos[1]=="NaN"
||isNaN(obj.pos[0])
||isNaN(obj.pos[1])
||isNaN(this.pos[0])
||isNaN(this.pos[1])
){
console.error("obj or this pos undefined what is this thing");
console.log(
"obj: "+obj.pos[0]+" "+obj.pos[1]+"\n"+
"this: "+this.pos[0]+" "+this.pos[1]
);
}
var vector=[
obj.pos[0]-this.pos[0],
obj.pos[1]-this.pos[1]
];
//console.log(vector);
//console.log(vector);
//console.log(vector);
var vectorMagnitude=Math.sqrt(vector[0]**2+vector[1]**2);
var normalizedVector=[
vector[0]/vectorMagnitude,
vector[1]/vectorMagnitude
];
//console.log(normalizedVector);
//forces between the two
//var multiplier=1;
//stops things that push away from attracting to each other
//not needed for correct use
//if(this.force<0&&obj.force<0){
// multiplier=-1;
//}
//stop dividing by zero!!!
//stop dividing all together!!!
//var toThisForce=this.force/obj.force*multiplier;
//too bad.
//if(this.force==undefined)
// console.error("This object force not defined!");
this.force=this.size?this.size:1;
var toThisForce=this.force;
//WOW THIS WAS THE ERROR THE WHOLE TIME? OR ELSE SOMETHING ELSE HERE??
//WOW... MULTIPLE HOURS SPENT ON SOMETHING HERE THAT I DON'T EVEN
//KNOW...
//console.log(toThisForce);
//var toObjForce=obj.force/this.force*multiplier;
//yay simple thing works!!! checks if its undefined or not
//and if it doesnt have a force then it uses the size instead
var toObjForce=
obj.force?obj.force:
obj.size?obj.size:
obj.radius?obj.radius:
1;
if(toObjForce==undefined)
console.error("For some reason the object force is undefined");
//console.log(toObjForce);
//change the speeds
obj.vel[0]-=normalizedVector[0]*(toThisForce)/(settings.fps**2)*mult;
obj.vel[1]-=normalizedVector[1]*(toThisForce)/(settings.fps**2)*mult;
this.vel[0]+=normalizedVector[0]*(toObjForce)/(settings.fps**2)*mult;
this.vel[1]+=normalizedVector[1]*(toObjForce)/(settings.fps**2)*mult;
//console.log(obj.vel[0],obj.vel[1],this.vel[0],this.vel[1]);
}
/*attractBoth(obj,mult){
//console.log("attractBoth used 1idao");
if(this==obj){
//changed from console.log to error.log...
//whoops not that its meant to be console.error...
//at 8/30/2022 10:20 AM CST Tueday
console.error("Same object interacting with self in attractBoth error");
}
//console.log(obj);
//object minus this for object to this vector
var vector=[
obj.pos[0]-this.pos[0],
obj.pos[1]-this.pos[1]
];
//console.log(obj,this);
//console.log(vector);
//console.log(vector);
var vectorMagnitude=Math.sqrt(vector[0]**2+vector[1]**2);
var normalizedVector=[
vector[0]/vectorMagnitude,
vector[1]/vectorMagnitude
];
//forces between the two
//var multiplier=1;
//stops things that push away from attracting to each other
//not needed for correct use
//if(this.force<0&&obj.force<0){
// multiplier=-1;
//}
//stop dividing by zero!!!
//stop dividing all together!!!
//var toThisForce=this.force/obj.force*multiplier;
//a
//thats too bad
//if(this.force==undefined)
// console.error("1 Object force not defined!");
this.force=this.size?this.size:1;
var toThisForce=this.force;
//var toObjForce=obj.force/this.force*multiplier;
//yay simple thing works!!! checks if its undefined or not
//and if it doesnt have a force then it uses the size instead
var toObjForce=obj.force?obj.force:obj.size;
//change the speeds
obj.vel[0]-=normalizedVector[0]*(toThisForce)/(settings.fps**2)*mult;
obj.vel[1]-=normalizedVector[1]*(toThisForce)/(settings.fps**2)*mult;
this.vel[0]+=normalizedVector[0]*(toObjForce)/(settings.fps**2)*mult;
this.vel[1]+=normalizedVector[1]*(toObjForce)/(settings.fps**2)*mult;
}*/
updatePositionByVelocity(){
//if(
// this.xv==NaN||this.yv==NaN||this.x==NaN||this.y==NaN||
// this.xv==undefined||this.yv==undefined||
// this.x==undefined||this.y==undefined
//){
// //no its console.error
// console.error(
// "NaN found in updatePositionByVelocity:"
// +" xv:"+this.xv
// +" yv:"+this.yv
// +" x:"+this.x
// +" y:"+this.y
// );
//}
//this.x+=this.xv;
//this.y+=this.yv;
if(
//(
// this.xv==NaN||this.yv==NaN||
// this.x==NaN||this.y==NaN||
// this.xv==undefined||this.yv==undefined||
// this.x==undefined||this.y==undefined
//)
//&&
(
this.vel[0]==NaN||this.vel[1]==NaN||
this.pos[0]==NaN||this.pos[1]==NaN||
this.vel[0]==undefined||this.vel[1]==undefined||
this.pos[0]==undefined||this.pos[1]==undefined
)
){
console.error(
"NaN found in updatePositionByVelocity:"
+" xv:"+this.xv
+" yv:"+this.yv
+" x:"+this.x
+" y:"+this.y
+" vel[0]:"+this.vel[0]
+" vel[1]:"+this.vel[1]
+" pos[0]:"+this.pos[0]
+" pos[1]:"+this.pos[1]
);
}
this.pos[0]+=this.vel[0];
this.pos[1]+=this.vel[1];
}
wallBounce(rect){
//get the corner coordinates
var sides=rect.getSideCoordinates();
//var topX=rect.pos[0]+rect.width/2;
//var bottomX=rect.pos[0]-rect.width/2;
//var topY=rect.pos[1]+rect.height/2;
//var bottomY=rect.pos[1]-rect.height/2;
//reverse the x velocity after compensating for the change
var trueRight=sides.right-this.radius;
var trueLeft=sides.left+this.radius;
var trueTop=sides.top+this.radius;
var trueBottom=sides.bottom-this.radius;
//i gues implementation for the box moving is nonexistant?
//not really sure how it even COULD be implemented, but still...
//oh ok sure
//console.log(this.radius);
if(this.pos[0]>trueRight){
this.pos[0]=(this.pos[0]-trueRight)*-1+trueRight;
//still an incorrect way to add that...
//in that the velocity should not be multiplied by negative 1
//by itself, it should maybe have the rectangle's velocity
//subtracted from ti before that... oh ok
this.vel[0]=Math.abs(this.vel[0]-rect.vel[0])*-1+rect.vel[0];
}
if(this.pos[0]<trueLeft){
this.pos[0]=(this.pos[0]-trueLeft)*-1+trueLeft;
this.vel[0]=Math.abs(this.vel[0]-rect.vel[0])+rect.vel[0];
}
//remember y is reversed so top is a shorter coordinate than bottom
if(this.pos[1]>sides.bottom-this.radius){
this.pos[1]=(this.pos[1]-trueBottom)*-1+trueBottom;
this.vel[1]=Math.abs(this.vel[1]-rect.vel[1])*-1+rect.vel[1];
}
if(this.pos[1]<trueTop){
this.pos[1]=(this.pos[1]-trueTop)*-1+trueTop;
this.vel[1]=Math.abs(this.vel[1]-rect.vel[1])+rect.vel[1];
}
}
render(){
//if the first argument exists use that
//otherwise try to get the data camera
//otherwise IDK
//console.log(arguments[0]);
var cameraObject=
arguments[0]?arguments[0]:(data.camera?data.camera:{});
//data.camera.getCoordsAndSize(this.x,this.y,this.size);
//console.log(cameraObject);
//console.log();
var pos=
cameraObject.getCoordsAndSize(this.pos[0],this.pos[1],this.size);
//this.parent.parent;
//console.log(pos);
ctx.beginPath();
ctx.moveTo(pos.x+pos.size,pos.y);
ctx.arc(pos.x,pos.y,pos.size,0,Math.PI*2,false);
ctx.closePath();
if(this.filled){
//HERE i got the Style not Color RIGHT...
//He got the Style yooo
ctx.fillStyle=this.color;
ctx.fill();
}else{
ctx.strokeStyle=this.color;
ctx.stroke(); //had a stroke
}
}
draw(){
this.render();
}
//gets the distance between "this" and the inputted object
//assumes the inputted object has an x and y...
//renamed from getDistance to distanceTo before its use
distanceTo(object){
return(
Math.sqrt(
(object.pos[0]-this.pos[0])**2+(object.pos[1]-this.pos[1])**2
)
);
}
//have this thing be a thing of its own to maybe even allow
//computer things in the future
getControlsFromKeyboard(){
//check keys
this.up=
keyPressed('w')||keyPressed('W')||keyPressed("ArrowUp");
this.down=
keyPressed('s')||keyPressed('S')||keyPressed("ArrowDown");
this.right=
keyPressed('d')||keyPressed('D')||keyPressed("ArrowRight");
this.left=
keyPressed('a')||keyPressed('A')||keyPressed("ArrowLeft");
//console.log(this.left);
}
move(){
//if velocity is used
//change the velocity
if(this.up){this.vel[1]-=(settings.speed/settings.fps)/settings.fps;}
if(this.down){this.vel[1]+=(settings.speed/settings.fps)/settings.fps;}
if(this.right){this.vel[0]+=(settings.speed/settings.fps)/settings.fps;}
if(this.left){this.vel[0]-=(settings.speed/settings.fps)/settings.fps;}
//console.log(this.left);
}
//move
moveToCoordinates(mx,my){
//vector this to mouse (what it was originally made for)
//the word "target" here in this context would be kind of
//confusing so maybe the word "mouse" is better since that is
//one instance this is made for
//so anyways the vector of this to mouse:
var vector=[mx-this.pos[0],my-this.pos[1]];
var magnitude=Math.sqrt(vector[0]**2+vector[1]**2);
//unit vector
var normalized=[vector[0]/magnitude,vector[1]/magnitude];
//change the velocity
this.vel[0]+=normalized[0]*settings.speed/(settings.fps*settings.fps);
this.vel[1]+=normalized[1]*settings.speed/(settings.fps*settings.fps);
}
//move with the mouse
moveWithMouse(){
this.moveToCoordinates(mouse.x,mouse.y);
}
decay(){
//decay the velocity over time
//if(settings.useDampeners){
plr.vel[0]*=settings.decay;
plr.vel[1]*=settings.decay;
//}
}
}
//THE camera!!!
class Camera extends PhysicsObject{
//scale is the order of magnitude of which
//everything is zoomed in to
//as in default^n where n is scale idk
//oh yeah also maybe i could get an automatic scale setter thing
//as well!
//although if there are only 3 things and they just fluctuate
//then the camera would repeatedly go in and out and in and out
//and that would be quite annoying... eh idk maybe maybe not idk
//scale is the power of 2 not 10? idk also i just looked at
//3d-10-8 and its code was kind of helpful but not really
//it used 1.1 weirdly expressed as 1+(0.1)
//IDK
//but sure well ok then
//anyways scale being normal should be 0
//not 1
//cuz something raised to 0 is 1 and then that gets multiplied
//by the number
constructor(x,y,xv,yv,scale){
super();
//x,y,xv,yv=x?x:0,y?y:0,xv?xv:0,yv?yv:0;
//this.x=x?x:0;
//this.y=y?y:0;
//this.xv=xv?xv:0;
//this.yv=yv?yv:0;
this.scale=scale?scale:0; //SCALE?????????????????? yes.
this.pos=[x,y];
this.vel=[xv,yv];
this.controls={
up:false,
down:false,
right:false,
left:false
}
} //idk lol scale?^
//method to get the number to scale or multiply by or whatever
getScaleNum(){
return(Math.pow(2,this.scale));
}
//inputs world space coordinates and size
//outputs camera space coordinates and size
getCoordsAndSize(x,y,size){
var scaleNum=this.getScaleNum();
//console.log(scaleNum);
var newSize=size*scaleNum
var newX=x-this.pos[0];
var newY=y-this.pos[1];
newX*=scaleNum;
newY*=scaleNum;
//console.log(x==newX);
return({x:newX,y:newY,size:newSize});
}
getCoords(x,y){
var scaleNum=this.getScaleNum();
var newX=x-this.pos[0];
var newY=y-this.pos[1];
newX*=scaleNum;
newY*=scaleNum;
return({x:newX,y:newY});
}
getSize(size){
return(size*this.getScaleNum());
}
updatePosition(){
//prefers list
try{
if(this.vel[0]!=0||this.vel[1]!=0){
//console.error(
// "xv or yv variables defined for camera, "+
// "please instead use this.pos and this.vel"
//);
//something something camera caused the unused velocity
//variables to be undefined instead of 0
//this.vel[0]=0;
//this.vel[1]=0;
}
}catch(e){}
this.pos[0]+=this.vel[0];
this.pos[1]+=this.vel[1];
}
accelerate(acceleration){
acceleration=acceleration?acceleration:settings.acceleration;
this.vel[0]+=acceleration[0];
this.vel[1]+=acceleration[1];
}
//have this thing be a thing of its own to maybe even allow
//computer things in the future
getControlsFromKeyboard(){
//check keys
this.controls.up=
keyPressed('w')||keyPressed('W')||keyPressed("ArrowUp");
this.controls.down=
keyPressed('s')||keyPressed('S')||keyPressed("ArrowDown");
this.controls.right=
keyPressed('d')||keyPressed('D')||keyPressed("ArrowRight");
this.controls.left=
keyPressed('a')||keyPressed('A')||keyPressed("ArrowLeft");
//console.log(this.left);
}
move(){
//if velocity is used
//change the velocity
if(this.controls.up){this.vel[1]-=(settings.speed/settings.fps)/settings.fps;}
if(this.controls.down){this.vel[1]+=(settings.speed/settings.fps)/settings.fps;}
if(this.controls.right){this.vel[0]+=(settings.speed/settings.fps)/settings.fps;}
if(this.controls.left){this.vel[0]-=(settings.speed/settings.fps)/settings.fps;
//console.log("yes");
//console.log(this.xv);
}
//console.log(this.left);
}
//move
moveToCoordinates(mx,my){
//vector this to mouse (what it was originally made for)
//the word "target" here in this context would be kind of
//confusing so maybe the word "mouse" is better since that is
//one instance this is made for
//so anyways the vector of this to mouse:
var vector=[mx-this.pos[0],my-this.pos[1]];
var magnitude=Math.sqrt(vector[0]**2+vector[1]**2);
//unit vector
var normalized=[vector[0]/magnitude,vector[1]/magnitude];
//change the velocity
this.vel[0]+=normalized[0]*settings.speed/(settings.fps*settings.fps);
this.vel[1]+=normalized[1]*settings.speed/(settings.fps*settings.fps);
}
//move with the mouse
moveWithMouse(){
this.moveToCoordinates(mouse.x,mouse.y);
}
decay(){
//decay the velocity over time
//if(settings.useDampeners){
plr.vel[0]*=settings.decay;
plr.vel[1]*=settings.decay;
//}
}
}
var data={camera:new Camera()};
class Ball extends PhysicsObject{
constructor(x,y,size,color,filled){
super();
//console.log(x,y);
//this.x=x;
//this.y=y;
//this.xv=0;
//this.yv=0;
this.size=size;
this.radius=size;
this.color=color;
this.filled=filled;
this.force=size;
this.pos=[x,y];
this.vel=[0,0];
}
//constructor(){
// this.x=0;
// this.y=0;
// this.xv=0;
// this.yv=0;
//}
/*
render(){
//if the first argument exists use that
//otherwise try to get the data camera
//otherwise IDK
//console.log(arguments[0]);
var cameraObject=
arguments[0]?arguments[0]:(data.camera?data.camera:{});
//data.camera.getCoordsAndSize(this.x,this.y,this.size);
//console.log(cameraObject);
//console.log();
var pos=
cameraObject.getCoordsAndSize(this.pos[0],this.pos[1],this.size);
//this.parent.parent;
//console.log(pos);
ctx.beginPath();
ctx.moveTo(pos.x+pos.size,pos.y);
ctx.arc(pos.x,pos.y,pos.size,0,Math.PI*2,false);
ctx.closePath();
if(this.filled){
//HERE i got the Style not Color RIGHT...
//He got the Style yooo
ctx.fillStyle=this.color;
ctx.fill();
}else{
ctx.strokeStyle=this.color;
ctx.stroke(); //had a stroke
}
}
draw(){
this.render();
}
*/
updatePositionByVelocity(){
if(
//(
// this.xv==NaN||this.yv==NaN||
// this.x==NaN||this.y==NaN||
// this.xv==undefined||this.yv==undefined||
// this.x==undefined||this.y==undefined
//)
//&&
(
this.vel[0]==NaN||this.vel[1]==NaN||
this.pos[0]==NaN||this.pos[1]==NaN||
this.vel[0]==undefined||this.vel[1]==undefined||
this.pos[0]==undefined||this.pos[1]==undefined
)
){
error.log(
"NaN found in updatePositionByVelocity:"
+" xv:"+this.xv
+" yv:"+this.yv
+" x:"+this.x
+" y:"+this.y
+" vel[0]:"+this.vel[0]
+" vel[1]:"+this.vel[1]
+" pos[0]:"+this.pos[0]
+" pos[1]:"+this.pos[1]
);
}
this.pos[0]+=this.vel[0];
this.pos[1]+=this.vel[1];
}
}
//methods for things like calculating wall bounces and stuffs
class BallPhysics extends Ball{
constructor(x,y,xv,yv,size,color,filled){
super(x,y,size,color,filled);
this.vel[0]=xv;
this.vel[1]=yv;
}
//have this thing be a thing of its own to maybe even allow
//computer things in the future
getControlsFromKeyboard(){
//check keys
this.up=
keyPressed('w')||keyPressed('W')||keyPressed("ArrowUp");
this.down=
keyPressed('s')||keyPressed('S')||keyPressed("ArrowDown");
this.right=
keyPressed('d')||keyPressed('D')||keyPressed("ArrowRight");
this.left=
keyPressed('a')||keyPressed('A')||keyPressed("ArrowLeft");
//console.log(this.left);
}
move(){
//if velocity is used
//change the velocity
if(this.up){this.vel[1]-=(settings.speed/settings.fps)/settings.fps;}
if(this.down){this.vel[1]+=(settings.speed/settings.fps)/settings.fps;}
if(this.right){this.vel[0]+=(settings.speed/settings.fps)/settings.fps;}
if(this.left){this.vel[0]-=(settings.speed/settings.fps)/settings.fps;}
//console.log(this.left);
}
//move
moveToCoordinates(mx,my){
//vector this to mouse (what it was originally made for)
//the word "target" here in this context would be kind of
//confusing so maybe the word "mouse" is better since that is
//one instance this is made for
//so anyways the vector of this to mouse:
var vector=[mx-this.pos[0],my-this.pos[1]];
var magnitude=Math.sqrt(vector[0]**2+vector[1]**2);
//unit vector
var normalized=[vector[0]/magnitude,vector[1]/magnitude];
//change the velocity
this.vel[0]+=normalized[0]*settings.speed/(settings.fps*settings.fps);
this.vel[1]+=normalized[1]*settings.speed/(settings.fps*settings.fps);
}
//move with the mouse
moveWithMouse(){
this.moveToCoordinates(mouse.x,mouse.y);
}
decay(){
//decay the velocity over time
//if(settings.useDampeners){
plr.vel[0]*=settings.decay;
plr.vel[1]*=settings.decay;
//}
}
/*
wallBounce(){
if(this.pos[0]<=-canvasWidth/2+this.size){
var newWidth=canvasWidth/2-this.size;
this.pos[0]=((this.x+newWidth)*-1)-newWidth;
//reverse velocity
this.vel[0]*=-1;
}
if(this.pos[0]>=canvasWidth/2-this.size){
var newWidth=canvasWidth/2-this.size;
this.pos[0]=((this.x-newWidth)*-1)+newWidth;
//reverse velocity
this.vel[0]*=-1;
}
if(this.pos[1]<=-canvasHeight/2+this.size){
var newHeight=canvasHeight/2-this.size;
this.pos[1]=((this.y+newHeight)*-1)-newHeight;
//reverse velocity
this.vel[1]*=-1;
}
if(this.pos[1]>=canvasHeight/2-this.size){
var newHeight=canvasHeight/2-this.size;
this.pos[1]=((this.y-newHeight)*-1)+newHeight;
//reverse velocity
this.vel[1]*=-1;
}
}
*/
//wall wrap; hit one wall, come out the other wall
wallWrap(){
//this.x=this.x<=-canvasWidth/2?this.x+canvas.Width:this.x;
if(this.pos[0]<=-canvasWidth/2)this.x=this.x+canvas.width;
//this.x=this.x>=canvasWidth/2?this.x-canvas.Width:this.x;
if(this.pos[0]>=canvasWidth/2)this.x=this.x-canvas.width;
//this.y=this.y<=-canvasHeight/2?this.y+canvas.Height:this.y;
if(this.pos[1]<=-canvasHeight/2)this.y=this.y+canvas.height;
//this.y=this.y>=canvasHeight/2?this.y-canvas.Height:this.y;
if(this.pos[1]>=canvasHeight/2)this.y=this.y-canvas.height;
// this is code ^ but its a 50/50 sometimes
}
updatePosition(){
this.pos[0]+=this.vel[0];
this.pos[1]+=this.vel[1];
//console.log(this.xv);
//console.log(this.x);
//CoolManTheCool was here 7/2/2022 and the guy under me isn't cool
//i know
}
drawTrajectoryLine(lineColor){
var pos=data.camera.getCoordsAndSize(this.pos[0],this.pos[1],1);
//console.log(pos);
ctx.beginPath();
var color=lineColor?lineColor:"red";
ctx.strokeStyle=color;
ctx.moveTo(pos.pos[0],pos.pos[1]);
ctx.lineTo(
pos.pos[0]+(this.vel[0]*pos.size)*settings.fps,
pos.pos[1]+(this.vel[1]*pos.size)*settings.fps
);
//its Style not Color!!!!!!!!!
//this fix should apply to the text too...
ctx.stroke();
//ctx.closePath();
}
drawRelativeTrajectoryLine(obj,lineColor){
var pos0=data.camera.getCoordsAndSize(this.pos[0],this.pos[1],1);
var pos1=data.camera.getCoordsAndSize(obj.pos[0],obj.pos[1],1);
//size here in this context is just a scalar variable really
//actually yes cuz it is a vector so
//mathematically defined yes it is still a scaler nice
//get the colors
ctx.beginPath();
var color=lineColor?lineColor:"lime";
ctx.strokeStyle=color;
ctx.moveTo(pos0.pos[0],pos0.pos[1]);
//STUFF
//it just works now idk
//i used logic to make it
//and then added camera compatibility and now it just doesnt
//look like it makes any sense anymore
//but hey at least it still works so that nice
ctx.lineTo(
pos0.pos[0]+((this.vel[0]*pos0.size)-(obj.vel[0]*pos1.size))*settings.fps,
pos0.pos[1]+((this.vel[1]*pos0.size)-(obj.vel[1]*pos1.size))*settings.fps
);
ctx.stroke();
}
}
/*class player extends ball{
constructor(x,y,xv,yv){
this.x=x;
this.y=y;
this.xv=xv;
this.yv=yv;
}
}*/ //your microphone is not a war king
//fix your microphone
//hit the micrphone
//make microphone great again
//the black hole like ball that can also maybe work as a white hole like ball??? sure
class ForceBall extends BallPhysics{
constructor(x,y,xv,yv,force,size,color,filled){
super(x,y,xv,yv,size,color,filled);
//this.xv=xv;
//this.yv=yv;
this.vel[0]=xv;
this.vel[1]=yv;
this.force=force;
this.render();
}
//singular one way attraction
//from object to this
//object is the one being affected
//it is being affected by force
attractSingle(obj){
//object minus this for object to this vector
var vector=[
obj.pos[0]-this.pos[0],
obj.pos[1]-this.pos[1]
];
var vectorMagnitude=Math.sqrt(vector[0]**2+vector[1]**2);
var normalizedVector=[
vector[0]/vectorMagnitude,
vector[1]/vectorMagnitude
];
//change the speeds
obj.vel[0]+=normalizedVector[0]*this.force; //here was the
obj.vel[1]+=normalizedVector[1]*this.force; //solution all along!
}
//both way attraction by mass
//force using physicObject's method
/*
attractBoth(obj,mult){
if(this==obj){
//changed from console.log to error.log...
//whoops not that its meant to be console.error...
//at 8/30/2022 10:20 AM CST Tueday
console.error("Same object interacting with self in attractBoth error");
}
//console.log(obj);
//object minus this for object to this vector
//check the numbers to make sure they are not undefined
//console.log(obj.pos,this.pos);
//console.log(obj.pos[0],obj.pos[1],this.pos[0],this.pos[1]);
if(
obj.pos[0]==undefined
||obj.pos[1]==undefined
||this.pos[0]==undefined
||this.pos[1]==undefined
||obj.pos[0]=="NaN"
||obj.pos[1]=="NaN"
||this.pos[0]=="NaN"
||this.pos[1]=="NaN"
||isNaN(obj.pos[0])
||isNaN(obj.pos[1])
||isNaN(this.pos[0])
||isNaN(this.pos[1])
){
console.error("obj or this pos undefined what is this thing");
console.log(
"obj: "+obj.pos[0]+" "+obj.pos[1]+"\n"+
"this: "+this.pos[0]+" "+this.pos[1]
);
}
var vector=[
obj.pos[0]-this.pos[0],
obj.pos[1]-this.pos[1]
];
//console.log(vector);
//console.log(vector);
//console.log(vector);
var vectorMagnitude=Math.sqrt(vector[0]**2+vector[1]**2);
var normalizedVector=[
vector[0]/vectorMagnitude,
vector[1]/vectorMagnitude
];
//console.log(normalizedVector);
//forces between the two
//var multiplier=1;
//stops things that push away from attracting to each other
//not needed for correct use
//if(this.force<0&&obj.force<0){
// multiplier=-1;
//}
//stop dividing by zero!!!
//stop dividing all together!!!
//var toThisForce=this.force/obj.force*multiplier;
if(this.force==undefined)
console.error("Object force not defined!");
var toThisForce=this.force;
//console.log(toThisForce);
//var toObjForce=obj.force/this.force*multiplier;
//yay simple thing works!!! checks if its undefined or not
//and if it doesnt have a force then it uses the size instead
var toObjForce=
obj.force?obj.force:
obj.size?obj.size:
obj.radius?obj.radius:
1;
if(toObjForce==undefined)
console.error("For some reason the object force is undefined");
//console.log(toObjForce);
//change the speeds
obj.vel[0]-=normalizedVector[0]*(toThisForce)/(settings.fps**2)*mult;
obj.vel[1]-=normalizedVector[1]*(toThisForce)/(settings.fps**2)*mult;
this.vel[0]+=normalizedVector[0]*(toObjForce)/(settings.fps**2)*mult;
this.vel[1]+=normalizedVector[1]*(toObjForce)/(settings.fps**2)*mult;
//console.log(obj.vel[0],obj.vel[1],this.vel[0],this.vel[1]);
}
*/
//gets the distance between "this" and the inputted object
//assumes the inputted object has an x and y...
//renamed from getDistance to distanceTo before its use
distanceTo(object){
return(Math.sqrt((object.pos[0]-this.pos[0])**2+(object.pos[1]-this.pos[1])**2));
}
}
//funny joke with nowhere else to write it:
//got 2 short jokes and a long one
//joke joke jooooke
//thanks casual conversation between teachers its a good joke
//-a bit before 2:45 PM CST 9/2/2022 Friday
//the target ball
class TargetBall extends BallPhysics{
constructor(x,y,xv,yv,size,color,filled){
super(x,y,xv,yv,size,color,filled);
//this.xv=xv;
//this.yv=yv;
this.vel[0]=xv;
this.vel[1]=yv;
this.render();
}
//sotp misspelling please and thanks
newPositionAndVelocity(){
this.newPosition();
this.newVelocity();
}
newPosition(){
//this.x=(Math.random()-0.5)*canvas.width;
//this.y=(Math.random()-0.5)*canvas.height;
//console.log(((Math.random()*2)-1)*(canvas.width/2-this.size/2));
//why is hte x and y nan here
this.pos[0]=((Math.random()*2)-1)*(canvas.width/2-this.size/2);
this.pos[1]=((Math.random()*2)-1)*(canvas.height/2-this.size/2);
}
//interesting when just velocity change and not position change
newVelocity(speed){
//HOW IS THIS ERRORING
//"speed" is undefined...
//thanks javascript for maybe or maybe not showing the error..
//-7/2/2022 5:26 PM CST Saturday
var newAngle=Math.random()*Math.PI*2;
//console.log(newAngle);
//console.log(speed);
this.vel[0]=Math.cos(newAngle)*settings.targetSpeed;
this.vel[1]=Math.sin(newAngle)*settings.targetSpeed;
}
//returns true or false
checkCollision(obj){
var dist=Math.sqrt(
Math.pow(this.pos[0]-obj.pos[0],2)+Math.pow(this.pos[1]-obj.pos[1],2)
);
return(dist<=this.size+obj.size);
}
//newPosition(){
// this.x=((Math.random()*2)-1)*(canvasWidth/2-target.size/2);
// this.y=((Math.random()*2)-1)*(canvasHeight/2-target.size/2);
//set the target speed
//if(settings.targetHasVelocity){
//this.xv=((Math.random()*2)-1)*settings.targetSpeed;
//this.yv=((Math.random()*2)-1)*settings.targetSpeed;
//}
//increment the score
//score++;
//}
}
//the player ball
class PlayerBall extends ForceBall{
constructor(x,y,xv,yv,force,size,color,filled){
super(x,y,xv,yv,force,size,color,filled);
this.up=false;
this.down=false;
this.right=false;
this.left=false;
this.render();
}
//have this thing be a thing of its own to maybe even allow
//computer things in the future
getControlsFromKeyboard(){
//check keys
this.up=
keyPressed('w')||keyPressed('W')||keyPressed("ArrowUp");
this.down=
keyPressed('s')||keyPressed('S')||keyPressed("ArrowDown");
this.right=
keyPressed('d')||keyPressed('D')||keyPressed("ArrowRight");
this.left=
keyPressed('a')||keyPressed('A')||keyPressed("ArrowLeft");
//console.log(this.left);
}
move(){
//if velocity is used
//change the velocity
if(this.up){this.vel[1]-=(settings.speed/settings.fps)/settings.fps;}
if(this.down){this.vel[1]+=(settings.speed/settings.fps)/settings.fps;}
if(this.right){this.vel[0]+=(settings.speed/settings.fps)/settings.fps;}
if(this.left){this.vel[0]-=(settings.speed/settings.fps)/settings.fps;}
//console.log(this.left);
}
//move
moveToCoordinates(mx,my){
//vector this to mouse (what it was originally made for)
//the word "target" here in this context would be kind of
//confusing so maybe the word "mouse" is better since that is
//one instance this is made for
//so anyways the vector of this to mouse: