作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,我有一个在 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/
这个问题在这里已经有了答案: Git for beginners: The definitive practical guide (37 个答案) 关闭 9 年前。 在使用句点(或句点,单点)将文件
我是一名优秀的程序员,十分优秀!