Skip to content

Commit

Permalink
added vec2.reflect
Browse files Browse the repository at this point in the history
  • Loading branch information
schteppe committed Jun 19, 2015
1 parent dfe572a commit ea9b6d5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
8 changes: 1 addition & 7 deletions examples/canvas/rayreflect.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,6 @@
ctx.stroke();
};

function reflect(out, vector, normal){
var dot = vector[0]*normal[0] + vector[1]*normal[1];
out[0] = vector[0] - 2 * normal[0] * dot;
out[1] = vector[1] - 2 * normal[1] * dot;
};

function drawRays(){

for (var i = 0; i < 10; i++) {
Expand All @@ -200,7 +194,7 @@
p2.vec2.copy(start, result.hitPointWorld);

// reflect the direction
reflect(direction, direction, result.hitNormalWorld);
p2.vec2.reflect(direction, direction, result.hitNormalWorld);

// move out a bit
start[0] += direction[0] * 0.001;
Expand Down
23 changes: 23 additions & 0 deletions src/math/vec2.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,33 @@ vec2.str = function (a) {
return 'vec2(' + a[0] + ', ' + a[1] + ')';
};

/**
* Linearly interpolate/mix two vectors.
* @static
* @method lerp
* @param {Array} out
* @param {Array} a First vector
* @param {Array} b Second vector
* @param {number} t Lerp factor
*/
vec2.lerp = function (out, a, b, t) {
var ax = a[0],
ay = a[1];
out[0] = ax + t * (b[0] - ax);
out[1] = ay + t * (b[1] - ay);
return out;
};

/**
* Reflect a vector along a normal.
* @static
* @method reflect
* @param {Array} out
* @param {Array} vector
* @param {Array} normal
*/
vec2.reflect = function(out, vector, normal){
var dot = vector[0] * normal[0] + vector[1] * normal[1];
out[0] = vector[0] - 2 * normal[0] * dot;
out[1] = vector[1] - 2 * normal[1] * dot;
};

0 comments on commit ea9b6d5

Please sign in to comment.