forked from yoland68/Pinball
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
72 lines (60 loc) · 1.77 KB
/
objects.js
File metadata and controls
72 lines (60 loc) · 1.77 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
//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){
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 (point[0] >= minX && point[0] <= maxX && point[1] >= minY && point[1] <= maxY)
return true;
return false;
}
//Objects
function Line(pointA, pointB, fiction, bp) {
this.a = pointA;
this.b = pointB;
this.fiction = fiction;
this.bouncePower = bp;
this.collision = function(ballInstance){
var result1 = slopeIntercept(ballInstance.lastCord, ballInstance.cord),
//result1 contains the slope and intercept value for ball curve
m1 = result1[0],
b1 = result1[1];
var result2 = slopeIntercept(this.a, this.b),
//result1 contains the slope and intercept value for surface curve
m2 = result2[0],
b2 = result2[1];
//x and y are the intercept point for both curves
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) ){
//if the intercepting point is in the range of both curve, then if collided
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
}
}