-
Notifications
You must be signed in to change notification settings - Fork 1
/
objects_old.js
149 lines (121 loc) · 3.77 KB
/
objects_old.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
//For Unit testing purpose
// function Ball(xy, speed, radius) {
// this.originalxy = xy.slice(0);
// this.originalspeed = speed.slice(0);
// this.lastCord = xy.slice(0);
// this.cord = xy;
// this.speed = speed;
// this.r = radius;
// this.lineCollision = function(){
// //STUB
// }
// this.sphereCollision = function(){
// //STUB
// }
// this.flipperCollision = function(){
// }
// this.move = function() {
// this.gravity();
// this.lastCord = this.cord.slice(0);
// this.cord[0] = this.speed[0]+this.cord[0];
// this.cord[1] = this.speed[1]+this.cord[1];
// }
// this.gravity = function() { //This is both gravity and fiction
// this.speed[0] = this.speed[0]*.999;
// this.speed[1] = this.speed[1]-0.001;
// }
// this.reset = function() {
// this.cord[0] = this.originalxy[0];
// this.cord[1] = this.originalxy[1];
// this.speed[0] = this.originalspeed[0];
// this.speed[1] = this.originalspeed[1];
// }
// }
//help functions
function slopeIntercept(pointA, pointB) {
var m = (pointB[1]-pointA[1])/(pointB[0]-pointA[0]);
var b = pointA[1]-m*pointA[0];
return [m, b];
}
function withinRange(point, limitA, limitB, printit){
var minX = Math.min(limitA[0], limitB[0]),
maxX = Math.max(limitA[0], limitB[0]),
minY = Math.min(limitA[1], limitB[1]),
maxY = Math.max(limitA[1], limitB[1]);
if (printit){
console.log("minX: " + minX + " maxX: " + maxX + " minY: " + minY + " maxY: " + maxY);
console.log(point);
}
if (point[0] >= minX && point[0] <= maxX && point[1] >= minY && point[1] <= maxY)
return true;
return false;
}
function calcDist(point, m, i){
var a = m,
b = -1,
c = i;
var x = (b*(b*point[0]-a*point[1])-a*c)/(a*a+b*b),
y = (a*(-b*point[0]+a*point[1])-b*c)/(a*a+b*b);
return [y-point[1], x-point[0]];
}
//Objects
function Line(pointA, pointB, fiction, bouncingPower) {
this.a = pointA;
this.b = pointB;
this.fiction = fiction;
this.bp = bouncingPower;
this.collision = function(ballInstance){
var result1 = slopeIntercept(ballInstance.lastCord, ballInstance.cord),
m1 = result1[0],
b1 = result1[1];
var result2 = slopeIntercept(this.a, this.b),
m2 = result2[0],
b2 = result2[1];
var x = (b2-b1)/(m1-m2),
y = x*m1+b1;
y_test = x*m2+b2; //this value is for testing purposes
if ( withinRange([x,y], ballInstance.cord, ballInstance.lastCord) && withinRange([x,y], this.a, this.b) ){
console.log("location: " + ballInstance.cord);
console.log("speed before: " + ballInstance.speed);
// m2 = m2 == 0 ? 1000000 : m2;
var perp_m = -1/m2,
perp_b = y-perp_m*x;
var offset = calcDist(ballInstance.lastCord, perp_m, perp_b);
console.log("\noffset: "+offset+"\n")
var nextPoint = [ballInstance.lastCord[0]+offset[0]*2, ballInstance.lastCord[1]+offset[1]*2];
ballInstance.speed[0] = (nextPoint[0]-ballInstance.cord[0])*this.bp;
ballInstance.speed[1] = (nextPoint[1]-ballInstance.cord[1]);
console.log("nextPoint = "+nextPoint);
console.log("speed after: " + ballInstance.speed+"\n==========\n");
// console.log(ballInstance.speed[1])
ballInstance.move()
return true
}
return false;
}
}
function Sphere(xy, radius, bp){
this.xy = xy;
this.r = radius;
this.collision = function(ballInstance){
//STUB
}
}
function Flipper(root, tip) {
this.root = tip;
this.tip = tip;
this.speed = 0;
this.rotate = function() {
//Stub
}
this.collision = function(ballInstance){
//STUB
}
}
// Unit testing
// var line = new Line([-1,0], [1,0], 0, 0);
// var ball = new Ball([0,.5], [0.0001,0], .02);
// for (var i = 0; i < 100; i ++){
// ball.move();
// line.collision(ball);
// }