重力变形可视化算法(2D)

发布于 2024-10-27 04:02:53 字数 348 浏览 0 评论 0原文

我正在开发一款 Android 游戏,希望实现一个 2D 网格来可视化重力对比赛场地的影响。我想根据运动场上的各种物体来扭曲网格。我正在寻找的效果类似于处理库中的以下效果:

gravity

除了我的网格会更简单 - 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:

gravity

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 技术交流群。

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

发布评论

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

评论(1

小矜持 2024-11-03 04:02:53

正如您提到的,我会将每条线绘制为单独的线段。如果网格稀疏,它可能是最快的。

如果您从上方查看网格,则需要计算 xy 坐标位移。最简单的方法是实际沿 z 轴进行位移,然后使用 x_result = x/zy_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 and y coordinate displacements. The easiest way would be to actually do displacement along the z axis and then fake perspective with x_result = x/z and y_result = y/z . You set z=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 calculate delta_x^2 + delta_y^2 - so you save yourself that square root calculation == faster.

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