Skip to content

Instantly share code, notes, and snippets.

@crawsible
Created October 14, 2014 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crawsible/a6cd98fcf97d67d2e1d9 to your computer and use it in GitHub Desktop.
Save crawsible/a6cd98fcf97d67d2e1d9 to your computer and use it in GitHub Desktop.
Our velocity update function for the sweet new game, "Sassteroids"
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