Android 粒子系统收敛单点
好吧,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设触摸位于屏幕中心的简单代码:
触摸轴每一侧具有标准化速度的代码:
制作此代码的步骤:
您需要获取屏幕在 x 轴和 y 轴上方和下方的比例,以便无论触摸在哪里,都将粒子的速度从 0 归一化到 1:
一旦我们有了归一化信息,我们就可以用它来归一化粒子速度:
Naive code that assumes touch is in the centre of the screen:
Code with normalised speeds each side of the touch axis:
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:
Once we have that normalisation information we can use it to normalise the particle speeds:
将粒子的位移作为以收敛点为原点的向量:
获取单位向量:
按单位向量位移粒子 * -1 * 速度因子:
这有效吗?我只是写了这个,没有尝试或什么都没有。
Take the particle's displacement as a vector with convergence point as origin:
Get unit vector:
Displace particle by unit vector * -1 * speed factor:
Does that work? I just wrote this out, didn't try it or nothin'.