Skip to content

Commit

Permalink
fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
llafuente committed May 21, 2014
1 parent 958140c commit 63e3815
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
5 changes: 3 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"generate_debug": true,

"valid_arguments": {
"out": "Vec2",
"v2_n": "Vec2",
"x": "Number",
"y": "Number",
"length": "Number",
Expand Down Expand Up @@ -370,8 +370,9 @@
"out_a_velocity": "Vec2",
"out_b_velocity": "Vec2",
"normal": "Vec2",
"mtv": "Vec2",
"vector": "Vec2",
"penetration": "Number",
"penetration_depth": "Number",
"a_restitution": "Number",
"b_restitution": "Number",
"a_imass": "Number",
Expand Down
4 changes: 2 additions & 2 deletions dist/js-2dmath-browser.min.js

Large diffs are not rendered by default.

29 changes: 17 additions & 12 deletions lib/collision/resolve.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
var Vec2 = require("../vec2.js"),
vec2_scale = Vec2.scale,
vec2_dot = Vec2.dot,
vec2_sub = Vec2.sub,
abs = Math.abs,
max = Math.max,
sqrt = Math.sqrt,
Expand All @@ -7,17 +10,18 @@ var Vec2 = require("../vec2.js"),
sin = Math.sin,
HALF_PI = Math.HALF_PI;


function outside(out_position, out_velocity, penetration, normal, vector) {
var mtv_v = [0, 0];
function outside(out_position, out_velocity, penetration_depth, mtv) {
vec2_scale(mtv_v, mtv, penetration_depth);
// left-right penetration
if (vector[0] !== 0) {
out_position[0] = vector[0];
if (mtv_v[0] !== 0) {
out_position[0] = mtv_v[0];
out_velocity[0] = 0;
}

// up-down penetration
if (vector[1] !== 0) {
out_position[1] = vector[1];
if (mtv_v[1] !== 0) {
out_position[1] = mtv_v[1];
out_velocity[1] = max(out_velocity[1], 0);
}
}
Expand All @@ -26,17 +30,18 @@ var aux_vec2 = [0, 0],
col_impulse = [0, 0],
fric_impulse = [0, 0],
impulse = [0, 0],
tangent_vel = [0, 0];
tangent_vel = [0, 0],
rv = [0, 0];
/**
*
* @return {Boolean} is the velocity modified ?
*/
function linear(out_a_velocity, a_restitution, a_imass, a_point, out_b_velocity, b_restitution, b_imass, b_point, normal) {
// Calculate relative velocity
var rv = Vec2.sub([0, 0], out_b_velocity, out_a_velocity);
vec2_sub(rv, out_b_velocity, out_a_velocity);

// Calculate relative velocity in terms of the normal direction
var normal_vel = Vec2.dot(rv, normal);
var normal_vel = vec2_dot(rv, normal);

// Do not resolve if velocities are separating
if (normal_vel > 0) {
Expand All @@ -54,11 +59,11 @@ function linear(out_a_velocity, a_restitution, a_imass, a_point, out_b_velocity,
Vec2.scale(col_impulse, normal, j);

//rv - Dot( rv, normal ) * normal
Vec2.sub(tangent_vel, rv, Vec2.scale(tangent_vel, normal, normal_vel));
vec2_sub(tangent_vel, rv, Vec2.scale(tangent_vel, normal, normal_vel));
Vec2.normalize(tangent_vel, tangent_vel);

// Solve for magnitude to apply along the friction vector
var jt = -Vec2.dot(rv, tangent_vel);
var jt = -vec2_dot(rv, tangent_vel);
jt /= a_imass + b_imass;

var a_sfriction = 0.2;
Expand All @@ -82,7 +87,7 @@ function linear(out_a_velocity, a_restitution, a_imass, a_point, out_b_velocity,

Vec2.add(impulse, col_impulse, fric_impulse);

Vec2.sub(out_a_velocity, out_a_velocity, Vec2.scale(aux_vec2, impulse, a_imass));
vec2_sub(out_a_velocity, out_a_velocity, Vec2.scale(aux_vec2, impulse, a_imass));
Vec2.add(out_b_velocity, out_b_velocity, Vec2.scale(aux_vec2, impulse, b_imass));


Expand Down
19 changes: 12 additions & 7 deletions lib/vec2.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,15 +778,20 @@ function crossZV(out_vec2, factor, vec2) {
var tp_left = [0, 0],
tp_right = [0, 0];
/**
* (A x B) x C = B(C · A) - A(C · B)
* (A x B) x C = B(A.dot(C)) - A(B.dot(C))
* (A x B) x C = B(C · A) - A(C · B)
* (A x B) x C = B(A.dot(C)) - A(B.dot(C))
*
* @param out_vec2 {Array|Vec2}
* @param v1 {Array|Vec2}
* @param v2 {Array|Vec2}
* @param v3 {Array|Vec2}
*/
function tripleProduct(out, v1, v2, v3) {
function tripleProduct(out_vec2, v1, v2, v3) {
scale(tp_left, v2, dot(v1, v3));

scale(tp_right, v1, dot(v2, v3));

return subtract(out, tp_left, tp_right);
return subtract(out_vec2, tp_left, tp_right);
}

// **********************************************************
Expand Down Expand Up @@ -1038,8 +1043,8 @@ Vec2 = {
compare: compare,
dot: dot,
cross: cross,
cossVZ: cossVZ,
cossZV: cossZV,
crossVZ: crossVZ,
crossZV: crossZV,
toAngle: toAngle,
angle: toAngle,
angleTo: angleTo,
Expand Down Expand Up @@ -1069,4 +1074,4 @@ Vec2 = {
toString: toString
};

module.exports = Vec2;
module.exports = Vec2;

0 comments on commit 63e3815

Please sign in to comment.