, , . . 1- . m, u1 u2 - , v1, v2 -
m u1 + m2 u2 = m v1 + m v2
1/2 m u1.u1 + 1/2 m u2.u2 = 1/2 m v1.v1 + 1/2 m v2.v2.
v1 = u2, v2 = u1. . , , . , Newton.
2D 1 , . , , , .
var u = pjmpi.clone().normalize();
var v = new THREE.Vector3(u.y,-u.x,0);
var piu = pi.velocity.dot(u);
var piv = pi.velocity.dot(v);
pi.velocity = new THREE.Vector3(
pju * u.x + piv * v.x,
pju * u.y + piv * v.y,
0);
pj.velocity = new THREE.Vector3(
piu * u.x + pjv * v.x,
piu * u.y + pjv * v.y,
0);
. . , . , .
R http://www.plasmaphysics.org.uk/collision2d.htm. w. , . , w = (u1 + u2)/2 = (v1 + v2)/2.
v1 '= v1-w, v2' = v2-w, v1 '' = R (v1'-w), v2 '' = R (v2'-w) .
v1''' = R(v1-v2)/2 + (v1+v2)/2
v2''' = R(v1-v2)/2 + (v1+v2)/2
. wikipedia , 1D.
var u = pjmpi.clone().normalize();
var v = new THREE.Vector3(u.y,-u.x,0);
var piu = pi.velocity.dot(u);
var piv = pi.velocity.dot(v);
var pju = pj.velocity.dot(u);
var pjv = pj.velocity.dot(v);
var v1x = pju * u.x + piv * v.x;
var v1y = pju * u.y + piv * v.y;
var v2x = piu * u.x + pjv * v.x;
var v2y = piu * u.y + pjv * v.y;
var wx = (v1x+v2x)/2;
var wy = (v1y+v2y)/2;
var dx = (v1x-v2x)/2;
var dy = (v1y-v2y)/2;
pi.velocity = new THREE.Vector3(
wx + restitution * dx,
wy + restitution * dy,
0);
pj.velocity = new THREE.Vector3(
wx - restitution * dx,
wy - restitution * dy,
0);
console.log("KE before ",
pi.velocity.lengthSq()+pj.velocity.lengthSq());
console.log("M before ",
pi.velocity.x+pj.velocity.x ,
pi.velocity.y+pj.velocity.y);
console.log("KE after",v1x*v1x+v1y*v1y + v2x*v2x + v2y*v2y);
console.log("M after ", v1x+v2x, v1y+v2y);
console.log("KE rest",
pi.velocity.lengthSq()+pj.velocity.lengthSq());
console.log("M rest ",
pi.velocity.x+pj.velocity.x ,
pi.velocity.y+pj.velocity.y);
. .
var len = pjmpi.length();
var nx = pjmpi.x / len;
var ny = pjmpi.y / len;
var tx = -ny;
var ty = nx;
var wx = (pi.velocity.x+pj.velocity.x)/2;
var wy = (pi.velocity.y+pj.velocity.y)/2;
var dx = (pi.velocity.x-pj.velocity.x)/2;
var dy = (pi.velocity.y-pj.velocity.y)/2;
var a = dx * nx + dy * ny;
var b = dx * tx + dy * ty;
var cx = -a * nx + b * tx;
var cy = -a * ny + b * ty;
pi.velocity.set(
wx + restitution * cx,
wy + restitution * cy,
0);
pj.velocity.set(
wx - restitution * cx,
wy - restitution * cy,
0);
, .
http://jsfiddle.net/SalixAlba/8axnL59k/. , .