1
1
//Settings:
2
2
var backgroundColor = "#000000" ;
3
3
var elementColor = "#FFFFFF" ;
4
- var initialBallSpeed = 20 ; //How many px should we move per frame?
5
- var randomV = Math . random ( ) * ( Math . PI * 2 ) ;
4
+ var initialBallSpeed = 10 ; //How many px should we move per frame?
5
+ var randomV = Math . random ( ) * ( Math . PI / 2 ) - Math . PI / 4 ;
6
6
7
7
var initialBallK = Math . tan ( randomV ) ;
8
8
@@ -188,72 +188,102 @@ Platform.prototype.move = function(){
188
188
}
189
189
Platform . prototype . checkHitWithBall = function ( ball ) {
190
190
var x = ball . x ;
191
- var to = x + ball . width ;
192
-
193
191
var oldX = ball . oldX ;
194
- var oldTo = oldX + ball . width ;
195
192
196
- var smallestX = Math . min ( x , oldX ) ;
197
- var largestX = Math . max ( x , oldX ) ;
193
+ var line1 = this . x - ball . width ;
194
+ var line2 = this . x + this . width ;
198
195
199
- // Has it passed one of the lines?
200
- if ( smallestX < )
196
+ var smallest = Math . min ( x , oldX ) ;
197
+ var largest = Math . max ( x , oldX ) ;
201
198
}
202
199
203
200
var start = null ;
204
201
var ball = new Ball ( 10 , 10 , width / 2 - 5 , height / 2 - 5 , canvas . width , canvas . height ) ;
205
202
206
- var platform1 = new Platform (
203
+ var platformR = new Platform (
207
204
100 , // Height
208
205
10 , // Width
209
- 30 , // StartX: 30px from right edge of canvas
210
- height / 2 - 50 , // Start in the middle on y-axis
206
+ canvas . width - 40 , // StartX: 30px from right edge of canvas
207
+ height / 2 - 50 , // Start in the middle on y-axis
211
208
canvas . height // Only let it move so that we don't go outside of canvas
212
209
) ;
213
210
214
- var platform2 = new Platform (
215
- 30 , // Height
211
+ var platformL = new Platform (
212
+ 100 , // Height
216
213
10 , // Width
217
- canvas . width - 40 , // StartX: 30px (becomes 40px, because we need to count in the width of the platform) from left edge of canvas
218
- height / 2 - 15 , // Start in the middle on y-axis
214
+ 30 , // StartX: 30px (becomes 40px, because we need to count in the width of the platform) from left edge of canvas
215
+ height / 2 - 50 , // Start in the middle on y-axis
219
216
canvas . height // Only let it move so that we don't go outside of canvas)
220
217
) ;
221
218
222
- var platforms = [ platform1 , platform2 ] ;
223
- platform1 . setVelocity ( 0 ) ;
224
- platform2 . setVelocity ( 0 ) ;
219
+ var platforms = [ platformR , platformL ] ;
220
+ platformR . setVelocity ( 0 ) ;
221
+ platformL . setVelocity ( 0 ) ;
225
222
var running = true ;
226
223
var step = function ( timestamp ) {
227
- ball . draw ( ctx ) ;
228
- ball . move ( ) ;
224
+ for ( var i = 0 ; i < platforms . length ; i ++ ) {
225
+ platforms [ i ] . move ( ) ;
226
+ platforms [ i ] . draw ( ctx ) ;
227
+ }
229
228
230
- // Move platforms and check if ball has hit platform
229
+ var bouncyOnPlatform = function ( platform , ball ) {
230
+ var gradperl = Math . PI / 2 / platform . height ;
231
+
232
+ var l = - ( platform . y + platform . height / 2 - ball . centerY ) ;
233
+
234
+ var grad = gradperl * l ;
235
+
236
+ var k = Math . tan ( grad ) ;
237
+
238
+ var changeDir = 0 ;
239
+ if ( ball . dX > 0 ) {
240
+ changeDir = - 1
241
+ }
242
+ else {
243
+ changeDir = 1 ;
244
+ }
245
+
246
+ ball . setK ( k ) ;
247
+ ball . dX = ball . dX * changeDir ;
248
+ }
231
249
232
- /*platform1.draw(ctx);
233
- platform1.move();
250
+ // Check for hits with ball
251
+ var rightBound = platformR . x ;
252
+ var leftBound = platformL . x + platformL . width ;
234
253
235
- platform2.draw(ctx);
236
- platform2.move();*/
237
- for ( var i = 0 ; i < platforms . length ; i ++ ) {
238
- platforms [ i ] . draw ( ctx ) ;
239
- platforms [ i ] . move ( )
254
+ // Hit with left platform
255
+ if ( ball . x < leftBound && ball . dX < 0 ) {
256
+ // change direction
240
257
241
- platforms [ i ] . checkHitWithBall ( ball ) ;
258
+ if ( ball . y + ball . height > platformL . y && ball . y < platformL . y + platformL . height ) {
259
+ bouncyOnPlatform ( platformL , ball ) ;
260
+ }
242
261
}
243
262
244
- if ( running ) window . requestAnimationFrame ( step ) ;
263
+ // Hit with right platform
264
+ if ( ( ball . x + ball . width ) > rightBound && ball . dX > 0 ) {
265
+ if ( ball . y + ball . height > platformR . y && ball . y < platformR . y + platformR . height ) {
266
+ bouncyOnPlatform ( platformR , ball ) ;
267
+ }
268
+ }
269
+
270
+
271
+ ball . move ( ) ;
272
+ ball . draw ( ctx ) ;
273
+
245
274
246
275
//Very advanced system for detection of winner. Please be careful when you
247
276
//uncomment this. It could lead to a broken toe, or worse, a
248
277
//pinple on your elbow. Please uncomment with cuation.
249
- /*
278
+
250
279
if ( ball . centerX <= 0 ) {
251
280
alert ( "alliansen vann!" )
252
281
running = false ;
253
282
}
254
283
if ( ball . centerX >= ball . boundX ) {
255
284
alert ( "Lefty vann!" ) ;
256
285
running = false ;
257
- }*/
286
+ }
287
+ if ( running ) window . requestAnimationFrame ( step ) ;
258
288
} ;
259
289
window . requestAnimationFrame ( step ) ;
0 commit comments