/
balls-tests.js
159 lines (147 loc) · 6.06 KB
/
balls-tests.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
var assert = require('assert');
var Vector2D = require('../src/js/vector2d.js');
var Balls = require('../src/js/balls.js');
describe('Balls', function() {
var localDimensions = {
width: 100,
height: 100 * (2/3)
};
var ballRadius = 1;
// Machine epsilon (an upper bound on the relative error due to rounding in floating point arithmetic)
var epsilon = 0.00000001;
function nearEqual(a, b) {
return Math.abs(a - b) < epsilon;
}
describe('no collision', function() {
it('should have the same position before and after method', function() {
// arrange
var oldPositionA = new Vector2D(50, 50);
var ballA = new Balls.VerticalBall(
oldPositionA.clone(),
new Vector2D(10, 0),
ballRadius,
localDimensions
);
var oldPositionB = new Vector2D(60, 50);
var ballB = new Balls.VerticalBall(
oldPositionB.clone(),
new Vector2D(10, 0),
ballRadius,
localDimensions
);
// act
ballA.collision(ballB)
// assert
assert.equal(ballA.position.X, oldPositionA.X);
assert.equal(ballA.position.Y, oldPositionA.Y);
assert.equal(ballB.position.X, oldPositionB.X);
assert.equal(ballB.position.Y, oldPositionB.Y);
});
});
describe('1 moving ball collision', function() {
it('should move the stationary ball and stop the moving ball', function() {
// arrange
var oldPositionA = new Vector2D(50, 50);
var ballA = new Balls.VerticalBall(
oldPositionA.clone(),
new Vector2D(10, 0),
ballRadius,
localDimensions
);
var oldVelocityA = ballA.velocity.clone();
var oldPositionB = new Vector2D(50.5, 50);
var ballB = new Balls.VerticalBall(
oldPositionB.clone(),
new Vector2D(0, 0),
ballRadius,
localDimensions
);
var oldVelocityB = ballB.velocity.clone();
// act
ballA.collision(ballB)
// assert
assert.equal(nearEqual(ballA.position.X, oldPositionA.X), true);
assert.equal(nearEqual(ballA.position.Y, oldPositionA.Y), true);
assert.equal(nearEqual(ballA.velocity.X, oldVelocityB.X), true);
assert.equal(nearEqual(ballA.velocity.Y, oldVelocityB.Y), true);
assert.equal(nearEqual(ballB.position.X, oldPositionB.X), false);
assert.equal(nearEqual(ballB.position.Y, oldPositionB.Y), true);
assert.equal(nearEqual(ballB.velocity.X, oldVelocityA.X), true);
assert.equal(nearEqual(ballB.velocity.Y, oldVelocityA.Y), true);
});
});
describe('2 moving balls collision', function() {
it('should change the velocity vectors of both balls', function() {
// arrange
var oldPositionA = new Vector2D(50, 50);
var ballA = new Balls.VerticalBall(
oldPositionA.clone(),
new Vector2D(10, 0),
ballRadius,
localDimensions
);
var oldVelocityA = ballA.velocity.clone();
var oldPositionB = new Vector2D(50.5, 50);
var ballB = new Balls.VerticalBall(
oldPositionB.clone(),
new Vector2D(-7, 0),
ballRadius,
localDimensions
);
var oldVelocityB = ballB.velocity.clone();
// act
ballA.collision(ballB)
// assert
assert.equal(nearEqual(ballA.position.X, oldPositionA.X), false);
assert.equal(nearEqual(ballA.position.Y, oldPositionA.Y), true);
assert.equal(nearEqual(ballA.velocity.X, oldVelocityB.X), true);
assert.equal(nearEqual(ballA.velocity.Y, oldVelocityB.Y), true);
assert.equal(nearEqual(ballB.position.X, oldPositionB.X), false);
assert.equal(nearEqual(ballB.position.Y, oldPositionB.Y), true);
assert.equal(nearEqual(ballB.velocity.X, oldVelocityA.X), true);
assert.equal(nearEqual(ballB.velocity.Y, oldVelocityA.Y), true);
});
});
describe('horizontal ball moving', function() {
it('should move the ball in direction', function() {
// arrange
var oldPosition = new Vector2D(50, 50);
var oldVelocity = new Vector2D(10, 0);
var ball = new Balls.HorizontalBall(
oldPosition.clone(),
oldVelocity.clone(),
ballRadius,
localDimensions
);
// act
ball.move();
// assert
var direction = oldPosition.direction(ball.position).tryNormalize();
var velocity = oldVelocity.tryNormalize();
assert.equal(nearEqual(direction.X, velocity.X), true);
assert.equal(nearEqual(direction.Y, velocity.Y), true);
});
});
describe('horizontal ball wall collision', function() {
it('should reflect the angle of moving and position inside borders', function() {
// arrange
var oldPosition = new Vector2D(99.5, 50);
var oldVelocity = new Vector2D(10, 0);
var ball = new Balls.HorizontalBall(
oldPosition.clone(),
oldVelocity.clone(),
ballRadius,
localDimensions
);
// act
ball.move();
// assert
var newVelocity = ball.velocity.tryNormalize();
oldVelocity = oldVelocity.tryNormalize();
assert.equal(nearEqual(oldVelocity.X, -newVelocity.X), true);
assert.equal(nearEqual(oldVelocity.Y, newVelocity.Y), true);
assert.notEqual(oldPosition.X, ball.position.X);
assert.equal(ball.position.X, localDimensions.width - ballRadius);
});
});
});