重力变形可视化算法(2D)
我正在开发一款 Android 游戏,希望实现一个 2D 网格来可视化重力对比赛场地的影响。我想根据运动场上的各种物体来扭曲网格。我正在寻找的效果类似于处理库中的以下效果:
除了我的网格会更简单 - 2D,严格从顶部观看,就像俯视比赛场地一样。
有人可以指出我绘制这种网格的算法吗?
我想到的一个想法是像“粒子”一样绘制线条 - 从屏幕的一端开始,将线条分成多个段,将每个段视为一个粒子,计算每个段的重力影响段的位置。
该应用程序旨在在 Android 上运行。
谢谢
I'm working on an Android game and would like to implement a 2D grid to visualize the effects of gravity on the playing field. I'd like to distort the grid based on various objects on my playing field. The effect I'm looking for is similar to the following from the Processing library:
Except that my grid will be simpler- 2D, and viewed strictly from the top, as if looking down at the playfield.
Can someone point me to an algorithm for drawing such a grid?
The one idea that I came up with was to draw the lines as if they were "particles"- start at one end of the screen and draw the line in multiple segments, treating each segment as a particle, calculating the effect of gravity at each segment's location.
The application is intended to run on Android.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您提到的,我会将每条线绘制为单独的线段。如果网格稀疏,它可能是最快的。
如果您从上方查看网格,则需要计算
x
和y
坐标位移。最简单的方法是实际沿z
轴进行位移,然后使用x_result = x/z
和y_result = y/z
进行假透视。您设置z=1
并确保仅相对轻微地改变它(例如 +- 0.1)。您的 z 应与
1/(到球体的距离)^2 之和成正比。这模拟了重力的工作原理——它随着距离的平方而逐渐减小。好消息 - 距离的平方意味着计算
delta_x^2 + delta_y^2
- 这样您就可以更快地节省平方根计算。I would draw each line as a separate segment, as you mentioned. If the grid is sparse, it might be fastest.
If you are viewing the grid from above, you would need to calculate
x
andy
coordinate displacements. The easiest way would be to actually do displacement along thez
axis and then fake perspective withx_result = x/z
andy_result = y/z
. You setz=1
and make sure to vary it only relatively slightly (+- 0.1 for instance).Your z should be proportional to the sum of
1/(distance to the sphere)^2
. This simulates how gravity works - it tapers off with square of the distance. Great news - square of the distance means to calculatedelta_x^2 + delta_y^2
- so you save yourself that square root calculation == faster.