2D 球与角点的碰撞

发布于 2024-08-15 20:37:08 字数 563 浏览 5 评论 0原文

我正在尝试编写一个从固定的垂直和水平墙壁弹起的球的 2D 模拟。模拟与墙壁表面的碰撞非常简单——只需抵消垂直墙壁的 X 速度或水平墙壁的 Y 速度。问题是球也可能与墙壁的角落碰撞,即水平墙壁与垂直墙壁相交的地方。我已经弄清楚如何检测何时发生与拐角的碰撞。我的问题是球应该如何应对这种碰撞——也就是说,它的 X 和 Y 速度将如何改变。

以下是我已经知道或知道如何查找的内容的列表:

  • 检测到碰撞时帧期间球中心的 X 和 Y 坐标
  • 球速度的 X 和 Y 分量
  • 角点的 X 和 Y 坐标
  • 角度球的中心和角之间
  • 碰撞前球运动的角度
  • 检测到碰撞时球与角重叠的量

我猜最好假装角是一个无限小的圆,因此我可以将球和圆之间的碰撞视为球与碰撞点与圆相切的墙碰撞。在我看来,我所需要做的就是旋转坐标系以与这面假想的墙对齐,反转该系统下球速度的 X 分量,然后将坐标旋转回原始系统。问题是我不知道如何编程。

顺便说一句,这是一个理想的模拟。我没有考虑摩擦力或球的旋转等任何因素。我正在使用 Objective-C,但我真的只是想要一个通用算法或一些建议。

I'm trying to write a 2D simulation of a ball that bounces off of fixed vertical and horizontal walls. Simulating collisions with the faces of the walls was pretty simple--just negate the X-velocity for a vertical wall or the Y-velocity for a horizontal wall. The problem is that the ball can also collide with the corners of the walls, where a horizontal wall meets with a vertical wall. I have already figured out how to detect when a collision with a corner is occurring. My question is how the ball should react to this collision--that is, how its X and Y velocities will change as a result.

Here's a list of what I already know or know how to find:

  • The X and Y coordinates of the ball's center during the frame when a collision is detected
  • The X and Y components of the ball's velocity
  • The X and Y coordinates of the corner
  • The angle between the ball's center and the corner
  • The angle in which the ball is traveling just before the collision
  • The amount that the ball is overlapping the corner when the collision is detected

I'm guessing that it's best to pretend that the corner is an infinitely small circle, so I can treat a collision between the ball and that circle as if the ball were colliding with a wall that runs tangent to the circles at the point of collision. It seems to me that all I need to do is rotate the coordinate system to line up with this imaginary wall, reverse the X component of the ball's velocity under this system, and rotate the coordinates back to the original system. The problem is that I have no idea how to program this.

By the way, this is an ideal simulation. I'm not taking anything like friction or the ball's rotation into account. I'm using Objective-C, but I'd really just like a general algorithm or some advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

水溶 2024-08-22 20:37:08

正如你所说,你可以将角视为半径无限小的圆。在这种情况下,碰撞平面的法线由从接触点到球中心的单位向量给出:

float nx = ballX - cornerX;
float ny = ballY - cornerY;
const float length = sqrt(nx * nx + ny * ny);
nx /= length;
ny /= length;

要反映速度向量,请执行以下操作:

const float projection = velocityX * nx + velocityY * ny;
velocityX = velocityX - 2 * projection * nx;
velocityY = velocityY - 2 * projection * ny;

As you say you can treat the corner as a circle with infinitely small radius. The normal of the collision plane in this case is given by a unit vector from the contact point to the center of the ball:

float nx = ballX - cornerX;
float ny = ballY - cornerY;
const float length = sqrt(nx * nx + ny * ny);
nx /= length;
ny /= length;

to reflect the velocity vector you do this:

const float projection = velocityX * nx + velocityY * ny;
velocityX = velocityX - 2 * projection * nx;
velocityY = velocityY - 2 * projection * ny;
独闯女儿国 2024-08-22 20:37:08

如果它是一个尖锐的(没有任何圆角)直角,它将充当后向反射器 并将其沿着它进来的路径反弹回来。

If it's a sharp (without any fillet) right-angled corner it will act as a retroreflector and bounce it back along the path it came in on.

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