带粒子碰撞的矢量反射问题 [英] Problems With Vector Reflection with Particle Collisions

查看:101
本文介绍了带粒子碰撞的矢量反射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否在此处.

在碰撞解决期间,粒子似乎无法正确分离.这是该函数的代码片段,用于分离粒子并更改其速度:

The particles don't seem to separate properly during collision resolution. Here is a code snippet from the function which separates particles and changes their velocities:

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

我尝试在更改pi.velocity时用pjmpi反转pimpj,但没有效果.

I have tried reversing pimpj with pjmpi while changing pi.velocity, but to no effect.

注意:我正在使用 three.js

推荐答案

首先,您似乎正在寻找的粒子碰撞为

Firstly, the particle collisions you seem to be looking for are Elastic collisions, for which there is maths covering the calculation of the velocities after a collision.

碰撞在动量框架的中心最简单,因此,如果您首先计算该框架 V =(m 1 v 1 + m 2 v 2 )/(m 1 + m 2 ),然后可以从两个粒子中减去它,进行一次简单的对称碰撞,然后再将帧速度加回来.

The collision is simplest in the centre of momentum frame, so if you first calculate that frame V = (m1v1 + m2v2)/(m1+m2), then you can subtract it from both particles, do a simple symmetric collision and add the frame velocity back on afterwards.

从那里开始,使用2& amp;中的公式计算速度.该页面的3d部分.

From there, calculate the velocities using the formulae in the 2 & 3d section of that page.

代码上的特定点:

  1. pimpj =-pjmpi,所以您不需要两者
  2. 当最后一帧与该帧之间的路径太近时,发生冲突.如果仅检查每个帧的距离,则会遇到粒子以高速彼此飞过的问题,并且必须不断移动其位置,因为当您检测到碰撞时它们已经重叠.
  3. 理想情况下计算影响的位置,并使用这些位置来重定向它们.
  4. 为了提高速度,只计算一次pimpj.clone().normalize()并将其存储-您以后无需更改此方向单位矢量,因此无需继续重新计算它或计算pjmpi派生的等效项(请参阅#1).
  1. pimpj = -pjmpi, so you don't need both
  2. A collision occurs when the paths between the last frame and this frame got too close; if you only check the distance at each frame you will have problems where particles fly through each other at high speed, and that you have to keep shifting their positions because they are already overlapping when you detect the collision.
  3. Ideally calculate the positions on impact and use those to redirect them.
  4. For speed, only calculate pimpj.clone().normalize() once, and store it - you're not changing this direction unit vector later, so you don't need to keep recalculating it, or calculating pjmpi-derived equivalents (see #1)

这篇关于带粒子碰撞的矢量反射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆