javascript - Problems With Vector Reflection with Particle Collisions -
i wondering whether made math mistake in particle collision simulation found here.
the particles don't seem separate during collision resolution. here code snippet function separates particles , changes velocities:
//particle 1 var pi = particles[i]; //particle 2 var pj = particles[j]; //particle 1 particle 2 var pimpj = pi.mesh.position.clone().sub(pj.mesh.position); //particle 2 particle 1 var pjmpi = pj.mesh.position.clone().sub(pi.mesh.position); //if colliding (radius 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 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 have tried reversing pimpj pjmpi while changing pi.velocity, no effect.
note: using three.js
firstly, particle collisions seem looking elastic collisions, there maths covering calculation of velocities after collision.
the collision simplest in centre of momentum frame, if first calculate frame v = (m1v1 + m2v2)/(m1+m2), can subtract both particles, simple symmetric collision , add frame velocity on afterwards.
from there, calculate velocities using formulae in 2 & 3d section of page.
specific points on code:
pimpj
= -pjmpi
, don't need both- a collision occurs when paths between last frame , frame got close; if check distance @ each frame have problems particles fly through each other @ high speed, , have keep shifting positions because overlapping when detect collision.
- ideally calculate positions on impact , use redirect them.
- for speed, calculate
pimpj.clone().normalize()
once, , store - you're not changing direction unit vector later, don't need keep recalculating it, or calculating pjmpi-derived equivalents (see #1)
Comments
Post a Comment