Particle Collision Vector Reflection Problems

I was wondering if I made a mathematical mistake in modeling particle collisions, here .

Particles do not seem to separate properly during collision resolution. Here is a code snippet from a function that separates particles and changes their speeds:

//particle 1
var pi = particles[i];
//particle 2
var pj = particles[j];

//particle 1 to particle 2
var pimpj = pi.mesh.position.clone().sub(pj.mesh.position); 
//particle 2 to particle 1
var pjmpi = pj.mesh.position.clone().sub(pi.mesh.position); 
//if colliding (radius is 20)
if(pimpj.length() < 20 && pimpj.length() != 0) 
{


    //reflect velocity off of 1->2
    pi.velocity = pi.velocity.reflect(pimpj.clone().normalize()).multiplyScalar(restitution);
    //reflect velocity off of 2->1 
    pj.velocity = pj.velocity.reflect(pjmpi.clone().normalize()).multiplyScalar(restitution);

    //move particle 1 to appropiate location based off of distance in between
    var pip = pi.velocity.clone().normalize().multiplyScalar(20-pimpj.length());
    //move particle 2 
    var pjp = pj.velocity.clone().normalize().multiplyScalar(20-pimpj.length());
    pi.mesh.position.add(pip);
    pj.mesh.position.add(pjp);
}

I tried to flip pimpj with pjmpi when changing pi.velocity, but has no effect.

Note: I am using three.js

0
source share
3 answers

-, , , , , - , , .

, , V= (m 1 v 1 + m 2 v 2)/(m 1 + m 2), , .

, 2- 3- .

:

  • pimpj= - pjmpi,
  • , ; , , , , .
  • .
  • pimpj.clone().normalize() - , pjmpi (. # 1)
+1

, , . . 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);

// resolve in two directions

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);

// resolve in two directions
var piu = pi.velocity.dot(u);
var piv = pi.velocity.dot(v);
var pju = pj.velocity.dot(u);
var pjv = pj.velocity.dot(v);

// velocities after collision
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;

// vel center of mass
var wx = (v1x+v2x)/2;                
var wy = (v1y+v2y)/2;

// difference
var dx = (v1x-v2x)/2;
var dy = (v1y-v2y)/2;

// final velocities
pi.velocity = new THREE.Vector3(
    wx + restitution * dx,
    wy + restitution * dy,
    0);

pj.velocity = new THREE.Vector3(
    wx - restitution * dx,
    wy - restitution * dy,
    0);

// We can print the KE and momentum before and after to check     
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();
// unit vector normal to plane of collision
var nx = pjmpi.x / len;
var ny = pjmpi.y / len;
// unit vector tangent to plane of collision
var tx = -ny;
var ty = nx;
// center of mass
var wx = (pi.velocity.x+pj.velocity.x)/2;
var wy = (pi.velocity.y+pj.velocity.y)/2;
// half difference
var dx = (pi.velocity.x-pj.velocity.x)/2;
var dy = (pi.velocity.y-pj.velocity.y)/2;
// resolve in two directions
var a = dx * nx + dy * ny;
var b = dx * tx + dy * ty;
// reflect difference in normal
var cx = -a * nx + b * tx;
var cy = -a * ny + b * ty;
// apply restitution and add back center of mass                                
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/. , .

0

, , . , pi-pj vi-vj .

, , .

0

Source: https://habr.com/ru/post/1627359/


All Articles