MCPcopy
hub / github.com/KilledByAPixel/LittleJS / collideWithObject

Method collideWithObject

examples/breakoutTutorial/game.js:57–84  ·  view source on GitHub ↗
(o)

Source from the content-addressed store, hash-verified

55 this.restitution = 1; // make object bounce
56 }
57 collideWithObject(o)
58 {
59 // prevent colliding with paddle if moving upwards
60 if (o == paddle && this.velocity.y > 0)
61 return false;
62
63 // speed up
64 const speed = LJS.min(1.04*this.velocity.length(), .5);
65 this.velocity = this.velocity.normalize(speed);
66
67 // play bounce sound with pitch scaled by speed
68 sound_bounce.play(this.pos, 1, speed);
69
70 if (o == paddle)
71 {
72 // control bounce angle when ball collides with paddle
73 const deltaX = o.pos.x - this.pos.x;
74 this.velocity = this.velocity.rotate(.3 * deltaX);
75
76 // make sure ball is moving upwards with a minimum speed
77 this.velocity.y = LJS.max(-this.velocity.y, .2);
78
79 // prevent default collision code
80 return false;
81 }
82
83 return true; // allow object to collide
84 }
85}
86
87class Wall extends LJS.EngineObject

Callers

nothing calls this directly

Calls 4

lengthMethod · 0.80
normalizeMethod · 0.80
rotateMethod · 0.80
playMethod · 0.45

Tested by

no test coverage detected