Created
October 14, 2014 06:16
-
-
Save crawsible/a6cd98fcf97d67d2e1d9 to your computer and use it in GitHub Desktop.
Our velocity update function for the sweet new game, "Sassteroids"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sassteroid.prototype.updateVelocity = function (that, maxDimensions) { | |
var dotProd = Sassteroids.Utils.dotProduct; | |
var perpVec = Array(2); | |
var uSpeed = Math.sqrt(dotProd(this.vel, this.vel)); | |
var vSpeed = Math.sqrt(dotProd(that.vel, that.vel)); | |
var posVector = Array(2); | |
for (var i = 0; i < 2; i++) { | |
posVector[i] = that.pos[i] - this.pos[i]; | |
} | |
var posVectorMagnitude = Math.sqrt(dotProd(posVector, posVector)); | |
var perpVec = Array(2); | |
for (var i = 0; i < 2; i++) { | |
perpVec[i] = posVector[i] / posVectorMagnitude; | |
} | |
var ui = dotProd(this.vel, perpVec); | |
var vi = dotProd(that.vel, perpVec); | |
var mu = this.mass(); | |
var mv = that.mass(); | |
var bounceVectoru = []; | |
var bounceVectorv = []; | |
for (var i = 0; i < 2; i++) { | |
var uf = ((2 * mv * vi) + (mu - mv) * ui) / (mu + mv) | |
bounceVectoru.push(uf * perpVec[i]); | |
var vf = ((2 * mu * ui) + (mv - mu) * vi) / (mu + mv) | |
bounceVectorv.push(vf * perpVec[i]) | |
} | |
var newVelu = Array(2); | |
var newVelv = Array(2); | |
for (var i = 0; i < 2; i++) { | |
newVelu[i] = this.vel[i] - (ui * perpVec[i]) + bounceVectoru[i]; | |
newVelv[i] = that.vel[i] - (vi * perpVec[i]) + bounceVectorv[i]; | |
} | |
this.vel = newVelu; | |
that.vel = newVelv; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment