gpt4 book ai didi

Android粒子系统收敛单点

转载 作者:行者123 更新时间:2023-11-29 18:17:00 25 4
gpt4 key购买 nike

好的,我有一个在 Android 中运行的粒子系统。

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

谁有确定如何均匀接近一个点的算法?我试图在 randians 中获得角度......然后转换等......变得非常困惑。这个简单的方法表明我以正确的速度移动……但这是一个简单的 45 度角方法。 (请记住,在 Android 上没有负 x、y 坐标。左上角是 0,0..右下角是(max,max)。函数采用 ontouch(x,y)坐标。b.x,b.y 是粒子坐标。

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

最佳答案

假定触摸位于屏幕中央的简单代码:

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;

关于Android粒子系统收敛单点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7591461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com