javascript - //Ball to ball elastic collision: ball bounces in an unequal proportion -
this first post :/
anyway, i've been working on game requires ball ball collision , can't seem find solution problem, collision balls going correct directions messes velocity.
once balls hits each other, horizontal(x) bounces off hard while vertical(y) bounces off softly. have no idea cause or causes of this, tried reduce x-velocity dividing means won't in proportion y value.
this code collision:
function resolvecollision(b1, b2){ var dot = function(v1, v2) { return v1.x * v2.x + v1.y * v2.y; }; v1x = b1.vx/2 v2x = b2.vx/2 v1y = b1.vy v2y = b2.vy var length = function(v) { return math.sqrt(v.x * v.x + v.y * v.y); }; var delta = {x: b1.x - b2.x, y: b1.y - b2.y}; var d = math.sqrt((delta.x * delta.x) + (delta.y * delta.y)); var dn = {x: delta.x / d, y: delta.y / d} var dt = {x: dn.y, y: -dn.x}; if (d === 0){ b2.x += 0.01; } var m1 = b1.m; var m2 = b2.m; var m = m1 + m2; var mt = {x: dn.x * (b1.r + b2.r - d), y: dn.y * (b1.r + b2.r - d)}; b1.x = b1.x + mt.x * m2 / m b1.y = b1.y + mt.y * m2 / m b2.x = b2.x + mt.x * m1 / m b2.y = b2.y + mt.y * m1 / m var v1 = {x: v1x, y: v1y}; var v2 = {x: v2x, y: v2y}; var v1n = { x: dn.x * (v1.x * v1.x + dn.y * dn.y), y: dn.y * (v1.x * v1.x + dn.y * dn.y) }; var v1t = { x: dt.x * (v1.x * v1.x + dt.y * dt.y), y: dt.y * (v1.x * v1.x + dt.y * dt.y) }; var v2n = { x: dn.x * (v2.x * v2.x + dn.y * dn.y), y: dn.y * (v2.x * v2.x + dn.y * dn.y) }; var v2t = { x: dt.x * (v2.x * v2.x + dt.y * dt.y), y: dt.y * (v2.x * v2.x + dt.y * dt.y) }; b1.velocity = { x: v1t.x + dn.x * ((m1 - m2) / m * length(v1n) + 2 * m2 / m * length(v2n)), y: v1t.y + dn.y * ((m1 - m2) / m * length(v1n) + 2 * m2 / m * length(v2n)) }; b2.velocity = { x: v2t.x - dn.x * ((m2 - m1) / m * length(v2n) + 2 * m1 / m * length(v1n)), y: v2t.y - dn.y * ((m2 - m1) / m * length(v2n) + 2 * m1 / m * length(v1n)) }; b1.vx = b1.velocity.x; b1.vy = b1.velocity.y; b2.vx = b2.velocity.x; b2.vy = b2.velocity.y; }
also if can tell me how make player(circle) pushes ball further without bouncing off great.
this files if wants have look, in app.js: https://www.mediafire.com/?d6dpqn1cmwzp756
just couldn't understand trying achieve, i'm trying replicate: http://www.haxball.com
i'm not english native speaker apologise grammar mistakes. me fix please, it's driving me crazy! :/
looking forward responses :d
1) should clarify code, stands bit complex, in bug source.
2) you're doing strange things v1n/t v2n/t : projecting + squaring speed on x ...??? didn't try understand b1.velocity code. ouch ! :-)
so here code simplifications : untested, solve issue :
function resolvecollision(b1, b2) { var v1x = b1.vx, v2x = b2.vx, v1y = b1.vy, v2y = b2.vy; // collision vector var delta = { x: b2.x - b1.x, y: b2.y - b1.y }; var d = length(delta); // normalized collision vector var dn = { x: delta.x / d, y: delta.y / d } // normalized tangent collision vector var dt = { x: dn.y, y: -dn.x }; var m1 = b1.m; var m2 = b2.m; var m = m1 + m2; /* // test separation tricks in second time if (d === 0) { b2.x += 0.01; } var mt = { x: dn.x * (b1.r + b2.r - d), y: dn.y * (b1.r + b2.r - d) }; b1.x = b1.x + mt.x * m2 / m b1.y = b1.y + mt.y * m2 / m b2.x = b2.x + mt.x * m1 / m b2.y = b2.y + mt.y * m1 / m */ // projection of v1 on collision vector var v1proj = { n: dn.x * v1x + dn.y * v1y, t: dt.x * v1x + dt.y * v1y }; // projection of v2 on collision vector var v2proj = { n: dn.x * v2x + dn.y * v1y, t: dt.y * v2x + dt.y * v2y }; // solving collision on normal var newv1projn = ((m1 - m2) * v1proj.n + 2 * m2 * v2proj.n) / m; var newv2projn = ((m2 - m1)* v2proj.n + 2 * m1 * v1proj.n) / m; // re-building speed vector out of projected vectors b1.vx = newv1projn * dn.x + v1proj.t * dt.x; b1.vy = newv1projn * dn.y + v1proj.t * dt.y; b2.vx = newv2projn * dn.x + v2proj.t * dt.x; b2.vy = newv2projn * dn.y + v2proj.t * dt.y; } function dot(v1, v2) { return v1.x * v2.x + v1.y * v2.y; }; function length(v) { return math.sqrt(v.x * v.x + v.y * v.y); };
Comments
Post a Comment