Android 粒子系统收敛单点

发布于 2024-12-07 06:22:21 字数 625 浏览 1 评论 0原文

好吧,我在 Android 中有一个粒子系统。

现在我使用 500 个左右的粒子,它们在屏幕上随机移动。我将其设置为触摸(实际上现在是运动)。所有的颗粒都会接近你的手指。问题是它们以静态角度接近(不考虑它们与触摸点的角度)。

有人有算法来确定如何均匀地接近一个点吗?我尝试以 randians 为单位获取角度..然后进行转换等..变得非常混乱。这个简单的方法表明我以正确的速度移动..但这是一个简单的 45 角方法。 (请记住,在 Android 上,没有负 x,y 坐标.. 左上角是 0,0.. 右下角是 (max,max)。函数采用 ontouch (x,y) 坐标。bx, by 是粒子坐标。

public void moveParticles(float x, float y) {
    for (Particle b : Particles)
        {
         if (x <= b.x)
             b.x = b.x -1;
         else b.x = b.x +1;

          if (y <= b.y)
            b.y = b.y -1;
          else b.y = b.y +1;
         }          
}

Ok I have a particle system going in Android.

Right now I use 500 or so particles and they randomly move around the screen. I set it so that on touch (actually motion right now). that all the particles would approach your finger. Problem being is that they're approaching at a static angle (not taking into the angle they are from the touch point).

Anyone have the algorithm to determine how to evenly approach a point? My attempt to get the angle in randians.. then convert etc.. got really messy. This simple approach showed that I move at the right pace.. but it's a simple 45 angle approach. (keep in mind on android there are no negative x,y coordinates.. and top left is 0,0.. bottom right is (max,max). the functions takes the ontouch (x,y) coordinates. b.x, b.y are the particle coordiantes.

public void moveParticles(float x, float y) {
    for (Particle b : Particles)
        {
         if (x <= b.x)
             b.x = b.x -1;
         else b.x = b.x +1;

          if (y <= b.y)
            b.y = b.y -1;
          else b.y = b.y +1;
         }          
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撧情箌佬 2024-12-14 06:22:21

假设触摸位于屏幕中心的简单代码:

public void moveParticles(float x, float y) {
    for (Particle b : Particles) {
      b.x += ((b.x-x)/(x/2))*speedmodifier;
      b.y += ((b.y-y)/(y/2))*speedmodifier;
    }
}

触摸轴每一侧具有标准化速度的代码:

public void moveParticles(float x, float y) {
    for (Particle b : Particles) {
      height_difference = screenheight-x;
      width_difference = screenwidth-y;
      height_ratio = (b.x < x) ? screen_height-height_difference : height_diffrence;
      width_ratio = (b.y < y) ? screenwidth-width_difference : width_difference;

      b.x += ((b.x-x)/height_ratio)*speedmodifier;
      b.y += ((b.y-y)/width_ratio)*speedmodifier;
    }
}

制作此代码的步骤:
您需要获取屏幕在 x 轴和 y 轴上方和下方的比例,以便无论触摸在哪里,都将粒子的速度从 0 归一化到 1:

height_difference = screenheight-x;
width_difference = screenwidth-y;
height_ratio = (b.x < x) ? screen_height-height_difference : height_diffrence;
width_ratio = (b.y < y) ? screenwidth-width_difference : width_difference;

一旦我们有了归一化信息,我们就可以用它来归一化粒子速度:

b.x += ((b.x-x)/height_ratio)*speedmodifier;
b.y += ((b.y-y)/width_ratio)*speedmodifier;

Naive code that assumes touch is in the centre of the screen:

public void moveParticles(float x, float y) {
    for (Particle b : Particles) {
      b.x += ((b.x-x)/(x/2))*speedmodifier;
      b.y += ((b.y-y)/(y/2))*speedmodifier;
    }
}

Code with normalised speeds each side of the touch axis:

public void moveParticles(float x, float y) {
    for (Particle b : Particles) {
      height_difference = screenheight-x;
      width_difference = screenwidth-y;
      height_ratio = (b.x < x) ? screen_height-height_difference : height_diffrence;
      width_ratio = (b.y < y) ? screenwidth-width_difference : width_difference;

      b.x += ((b.x-x)/height_ratio)*speedmodifier;
      b.y += ((b.y-y)/width_ratio)*speedmodifier;
    }
}

Steps to make this code:
You need to get the ratio of the screen above and below the x and y axis, so as to normalise the speed of the particles from 0 to 1 no matter where the touch is:

height_difference = screenheight-x;
width_difference = screenwidth-y;
height_ratio = (b.x < x) ? screen_height-height_difference : height_diffrence;
width_ratio = (b.y < y) ? screenwidth-width_difference : width_difference;

Once we have that normalisation information we can use it to normalise the particle speeds:

b.x += ((b.x-x)/height_ratio)*speedmodifier;
b.y += ((b.y-y)/width_ratio)*speedmodifier;
套路撩心 2024-12-14 06:22:21

将粒子的位移作为以收敛点为原点的向量:

x = particle.x - finger.x
y = particle.y - finger.y

获取单位向量:

norm = sqrt(pow(x, 2.0) + pow(y, 2.0))
x = x / norm
y = y / norm

按单位向量位移粒子 * -1 * 速度因子:

particle.x -= x * speed
particle.y -= y * speed

这有效吗?我只是写了这个,没有尝试或什么都没有。

Take the particle's displacement as a vector with convergence point as origin:

x = particle.x - finger.x
y = particle.y - finger.y

Get unit vector:

norm = sqrt(pow(x, 2.0) + pow(y, 2.0))
x = x / norm
y = y / norm

Displace particle by unit vector * -1 * speed factor:

particle.x -= x * speed
particle.y -= y * speed

Does that work? I just wrote this out, didn't try it or nothin'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文