2D 游戏物理向量问题

发布于 2024-10-01 22:17:28 字数 403 浏览 1 评论 0原文

我一直在用 C# 编写一个简单的程序,其中 Ball [X,Y] 坐标定期递增。

我已经设法实现了碰撞检测方法,但我正在尝试确定如何以相反的角度反射球,使其沿着相同的线性路径弹回。

dx = -dx //This bounces the ball back along the same linear path
dy = -dy

解决方案 三角学

theta = range between 0<theta<=360 depending on where it bounced
x = cos(theta)*time
y=  sin(theta)*time

I've been working on a simple program in C# in which a Ball [X,Y] cordinates are periodical incremented.

I've managed to implement a collision detection method, but I'm trying to determine how to reflect the ball at an angle oposed bouncing it back along the same linear path.

dx = -dx //This bounces the ball back along the same linear path
dy = -dy

Solution
Trigonometry

theta = range between 0<theta<=360 depending on where it bounced
x = cos(theta)*time
y=  sin(theta)*time

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

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

发布评论

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

评论(4

撩动你心 2024-10-08 22:17:28

牛顿物理学的要点在于它不是随机的,而是确定性的。如果你以相同的角度、相同的速度和相同的旋转向同一面墙扔同一个球,它每次都会到达相同的位置。

这种程序对于编程和物理来说都是一个非常好的学习机会。我鼓励您首先编写一个模拟非常简单的弹跳的程序。正如您所注意到的,当物体垂直向下移动并撞击水平表面时,您可以将反弹建模为简单地反转垂直速度分量。只要做到这一点即可;没有重力,什么都没有。这是一个很好的开始。

然后尝试以同样的方式添加水平墙壁的弹跳。

然后尝试添加从不与水平或垂直方向对齐的墙壁反弹的效果。这就是你必须学习向量和三角学如何工作的地方,因为你必须计算出球的速度的哪个分量通过倾斜地撞击墙壁而改变。

然后添加重力。然后增加空气的摩擦力。然后添加球可以旋转的事实。添加弹性,以便您可以模拟球的变形。

一旦达到这一点,如果您想引入随机性,您将能够弄清楚如何做到这一点。例如,您可以通过说“好吧,当球撞击墙壁并变形时,我将引入一个随机元素,将其变形改变 0-10%”来引入随机性。这将改变模拟弹球的方式。您可以尝试不同类型的随机性:例如,添加随机气流。

The whole point of Newtonian physics is that it is not random, it is deterministic. If you throw the same ball against the same wall at the same angle and with the same velocity and the same spin, it goes to the same place every time.

This sort of program is a really great learning opportunity for both programming and physics. What I encourage you to do is to first write a program that simulates very simple bouncing. As you note, when an object is moving straight down and hits a horizontal surface, then you can model the bounce as simply reversing the vertical velocity component. Just get that right; no gravity, no nothing. That's a great start.

Then try adding bouncing off of horizontal walls, the same way.

Then try adding bouncing off of walls that are not aligned with horizontal or vertical directions. That's where you're going to have to learn how vectors and trigonometry work, because you'll have to work out what component of the ball's velocity is changed by striking the wall obliquely.

Then add gravity. Then add friction from the air. Then add the fact that the ball can be spinning. Add elasticity, so that you can model deformation of the ball.

Once you get to that point, if you want to introduce randomness you'll be able to figure out how to do it. For example, you might introduce randomness by saying "well, when the ball strikes the wall and deforms, I'll introduce a random element that changes its deformation by 0-10%". That will then change how the simulation bounces the ball. You can experiment with different kinds of randomness: add random air currents, for instance.

书间行客 2024-10-08 22:17:28

您必须自己添加随机性。重新表述你的问题:“确定性地,它以角度 theta 反弹。我怎样才能让它以角度 theta + epsilon 反弹,其中 epsilon 是一些随机值?”

要旋转向量,请参阅。您只需指定 theta。

伪代码:

RotateVector(vec):
    bounce_vec = [-vec.x vec.y];  //deterministic answer is negative x, normal y
    bounce_angle = acos(dot(vec,bounce_vec) / (norm(vec)*norm(bounce_vec)));
    modified_angle = bounce_angle + random_number();
    ca = cos(modified_angle);
    sa = sin(modified_angle);
    rotation_matrix = [ca -sa; sa ca];
    return rotation_matrix * vec;

第 3 行使用余弦定律来计算角度。在第 4 行中,该角度被随机修改。该函数的其余部分将原始向量旋转新角度。

You will have to add in randomness yourself. To rephrase your question: "Deterministically, it bounces off at angle theta. How can I make it bounce back at angle theta + epsilon, where epsilon is some random value?"

To rotate a vector, see this. You will just specify theta.

pseudocode:

RotateVector(vec):
    bounce_vec = [-vec.x vec.y];  //deterministic answer is negative x, normal y
    bounce_angle = acos(dot(vec,bounce_vec) / (norm(vec)*norm(bounce_vec)));
    modified_angle = bounce_angle + random_number();
    ca = cos(modified_angle);
    sa = sin(modified_angle);
    rotation_matrix = [ca -sa; sa ca];
    return rotation_matrix * vec;

Line 3 uses the law of cosines to figure out the angle. In line 4, that angle is modified randomly. The rest of the function rotates the original vector by your new angle.

甩你一脸翔 2024-10-08 22:17:28

只要它是一个具有完美表面的完美球,它就不会随机反弹。向量和三角函数都不会给你任何随机性。

As long as it's a perfect ball with a perfect surface it will not bounce back randomly. Neither vectors nor trigonometry will give you any randomness.

柳絮泡泡 2024-10-08 22:17:28

“随机,尽管适用于物理基本定律”似乎是一个矛盾修辞法。但是...

如果您希望它在随机方向上反弹,同时保持其当前速度,您可以执行以下操作(伪代码):

  • 首先,反弹规范方式(dx = -dx 或 dy = -dy,具体取决于碰撞),
  • 然后将 dx 和 dy 转换为极坐标(theta 和 r),
  • 抖动 theta 少量(+或 - 几度,根据您的喜好)
  • 确保 theta 不会撞到刚被弹回的墙壁上
  • 将 theta 和 r 转换回 dx 和 dy

这将保持标量动量守恒。

"randomly, though applying to the basic laws of physics" seems like an oxymoron. However...

If you want it to bounce in a random direction, while maintaining its current speed, you might do something like this (pseudocode):

  • first, bounce back the canonical way (dx = -dx or dy = -dy depending on the collision)
  • then convert the dx and dy to polar coordinates (theta and r)
  • jitter theta by a small amount (+ or - a few degrees, according to your taste)
  • make sure theta isn't heading into a wall that you just bounced off
  • convert theta and r back to dx and dy

That would be conserving scalar momentum.

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