C# 弹跳角如何计算?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/573084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate bounce angle?
提问by Moulde
I played around with it for a while, but I simply can't figure it out.
我玩了一段时间,但我就是想不通。
I made a tank that fires missiles, and when the missiles hit the walls, I want them to bounce off, but I want them to bounce off to the right angle.
我制作了一个发射导弹的坦克,当导弹击中墙壁时,我希望它们反弹,但我希望它们以正确的角度反弹。
Right now I haven't got any obstacles, the missiles just bounce off when they get outside the viewportRectangle
I made.
现在我没有任何障碍物,导弹在viewportRectangle
我制造的外面时会反弹。
Is the solution I'm looking for quite advanced?
我正在寻找的解决方案是否非常先进?
Is there a relativly simple way to do it?
有没有一种相对简单的方法来做到这一点?
采纳答案by Bill the Lizard
I think an easier way to do this is to use the velocity of the missile instead of calculating angles. Say you have a missile that has xVelocity
and yVelocity
to represent its movement horizontally and vertically. Those velocities can be positive or negative to represent left, right, up, or down.
我认为更简单的方法是使用导弹的速度而不是计算角度。假设您有一枚导弹,它具有xVelocity
和yVelocity
代表其水平和垂直运动。这些速度可以是正数或负数来表示左、右、上或下。
- If a missile hits a top or bottom border reverse the sign of the
yVelocity
. - If a missile hits a left or right border reverse the sign of the
xVelocity
.
- 如果导弹击中顶部或底部边界,则反转
yVelocity
. - 如果导弹击中左边界或右边界,则反转
xVelocity
.
This will keep the movement in the opposite axis the same.
这将使相反轴上的运动保持不变。
Borrowing the image from ChrisF's answer, let's say the missile starts out at position I.
借用ChrisF 的回答中的图像,假设导弹从位置 I 开始。
With the xVelocity
and yVelocity
both being positive (in 2D graphics right and down are typically positive) the missile will travel in the direction indicated. Let's just assign values of
当xVelocity
和yVelocity
都为正(在 2D 图形中,右和下通常为正)时,导弹将沿指示的方向行进。让我们只分配值
xVelocity = 3
yVelocity = 4
When the missile hits the wall at position C, its xVelocity
shouldn't change, but its yVelocity
should be reversed to -4 so that it travels back in the up direction, but keeps going to the right.
当导弹在C位置击中墙壁时,它xVelocity
不应该改变,但它yVelocity
应该反转到 -4 以便它向上移动,但继续向右移动。
The benefit to this method is that you only need to keep track of a missile's xPosition
, yPosition
, xVelocity
, and yVelocity
. Using just these four components and your game's update rate, the missile will always get redrawn at the correct position. Once you get into more complicated obstacles that are not at straight angles or are moving, it will be a lot easier to work with X and Y velocities than with angles.
这种方法的好处是,你只需要保持一个导弹的轨道xPosition
,yPosition
,xVelocity
,和yVelocity
。仅使用这四个组件和游戏的更新率,导弹将始终在正确的位置重新绘制。一旦您遇到更复杂的不是直角或正在移动的障碍物,使用 X 和 Y 速度将比使用角度容易得多。
回答by Yuval Adam
Not complicated at all - pseudo-code:
一点都不复杂——伪代码:
angleObjectHitWall = a;
bounceAngle = 180-a;
Of course this is a very simple calculation, and is totally irrelevant once you start to take into account factors such as material, gravity, walls which aren't straight, etc...
当然,这是一个非常简单的计算,一旦您开始考虑材料、重力、不直的墙壁等因素,就完全无关紧要了……
回答by ChrisF
For perfect particles (& light) the angle of reflection is equal to the angle of incidence, as illustrated by this diagram (from commons.wikimedia.org).
对于完美的粒子(和光),反射角等于入射角,如下图所示(来自 commons.wikimedia.org)。
Do a search for "angle of reflection" (without the quotes) in Google.
在 Google 中搜索“反射角”(不带引号)。
It's a little bit more complicated when you take into account the elasticity and materials of the object and the obstacles ;)
当您考虑到物体的弹性和材料以及障碍物时,它会稍微复杂一些;)
回答by Gregory A Beamer
180-a will not work in all instances, unless you are merely working a bounce on a top surface when X is increasing.
180-a 并非在所有情况下都有效,除非您只是在 X 增加时在顶面上进行反弹。
One direction to head is the XNA forums or pick up XNA sample code. It is C# and it is for building games. I am not stating you want to build your games in XNA, but it is a great tool, and it is free.
一个方向是 XNA 论坛或选择 XNA 示例代码。它是 C#,用于构建游戏。我并不是说您想在 XNA 中构建您的游戏,但它是一个很棒的工具,而且是免费的。
回答by David Bo?jak
This is really a physics question, so if you are not a physicist (and since you are asking this question, I'm going to take it that you are not) it will require a lot of reading and brainstorming to get it right.
这确实是一个物理问题,因此如果您不是物理学家(并且既然您在问这个问题,我将认为您不是),则需要大量阅读和集思广益才能正确解决。
I suggest reading thiswikipedia entry to get the basic idea about the depth of your question.
我建议阅读此维基百科条目以了解有关问题深度的基本概念。
If you only want to make it "look plausible" then I wouldn't worry about it too much and use Bill the Lizard'sanswer, however if you want to make it right you will have quite an adventure. Don't let this scare you tho! Good luck!
如果您只想让它“看起来合理”,那么我不会太担心并使用蜥蜴比尔的答案,但是如果您想让它正确,那么您将有很大的冒险经历。不要让这吓到你!祝你好运!
回答by Gareth Rees
You might think that because your walls are aligned with the coordinate axes that it makes sense to write special case code (for a vertical wall, negate the x-coordinate of the velocity; for a horizontal wall, negate the y-coordinate of the velocity). However, once you've got the game working well with vertical and horizontal walls, probably the next thing you'll think is, "what about walls at arbitrary angles?" So it's worth thinking about the general case from the beginning.
您可能会认为,因为您的墙与坐标轴对齐,所以编写特殊情况代码是有意义的(对于垂直墙,否定速度的 x 坐标;对于水平墙,否定速度的 y 坐标)。然而,一旦您让游戏在垂直和水平墙壁上运行良好,接下来您可能会想到,“任意角度的墙壁呢?” 所以值得从一开始就考虑一般情况。
In the general case, suppose your missile has velocity vand hits a wall with surface normal n.
在一般情况下,假设您的导弹具有速度v并击中表面法线为n的墙壁。
Split vinto components uperpendicular to the wall and wparallel to it.
将v分成垂直于墙的分量u和平行于墙的w。
Where:
在哪里:
u= (v?·?n/ n?·?n) n
w= v? u
u= ( v?·? n/ n?·? n) n
w= v? 你
Here, v?·?nis the dot productof the vectors vand n. See the link for an explanation of how to compute it. The dot product n?·?nevaluates to the square of the length of the normal vector; if you always keep your normals in the form of unit vectorsthen n?·?n= 1 and you can omit the division.
在这里,v?·? n是向量v和n的点积。有关如何计算它的说明,请参阅链接。点积n?·? n计算为法向量长度的平方;如果你总是以单位向量的形式保持你的法线,那么n?·? n= 1,您可以省略除法。
After bouncing, the component of motion parallel to the wall is affected by friction f, and the component perpendicular to the wall is affected by elasticity, which can be given in the form of a coefficient of restitutionr.
弹跳后,平行于壁面的运动分量受到摩擦力f 的影响,垂直于壁面的运动分量受到弹性的影响,弹性可以用恢复系数r的形式给出。
So the velocity after the collision is v′= fw? ru. In ?a perfectly elastic, frictionless collision, v′= w? u; that is, the motion is reflected about the normal at the point of collision, as in the diagram given in Bill's answer.
所以碰撞后的速度是v′= f w?[R ü。在 ? 完全弹性的、无摩擦的碰撞中,v′= w? ü; 也就是说,运动在碰撞点围绕法线反射,如比尔的答案中给出的图表所示。
This approach works just the same in three dimensions too.
这种方法在三个维度上也同样适用。
(Obviously this is a very simplified notion of bouncing; it takes no account of angular momentum or deformation. But for many kinds of video games this kind of simplification is perfectly adequate.)
(显然,这是一个非常简化的弹跳概念;它没有考虑角动量或变形。但对于许多类型的视频游戏,这种简化是完全足够的。)
回答by itsmatt
As an aside to the specific physics question you are asking, I would recommend the book "Beginning Math and Physics for Game Programmers" by Wendy Stahler. I found it quite useful for my game/physics programming projects.
作为您所问的特定物理问题的旁白,我会推荐温迪·斯塔勒 (Wendy Stahler) 所著的《游戏程序员的数学和物理入门》一书。我发现它对我的游戏/物理编程项目非常有用。
The code that accompanies the book is C++ but if you know C#, it would be pretty easy to make the conversion.
本书随附的代码是 C++,但如果您了解 C#,则进行转换将非常容易。
Have a good one!
祝你有个好的一天!
回答by Diogenes Silveira
I've had this problem, the only way I found was separating the axes of collision!
我遇到了这个问题,我发现的唯一方法是分离碰撞轴!
Try it:
尝试一下:
x += velocity * Math.cos(angle * Math.PI /180);
y += velocity * Math.sin(angle * Math.PI /180);
if (x < 0 || x > canvas.width) {
angle = 180 - angle;
}
else if (y < 0 ||y > canvas.height) {
angle = 360 - angle;
}
I hope this helps you!
我希望这可以帮助你!
回答by grant fisher
if(!Collide(Missle, Mainchar)){
(Velocity.x)*-1;
(Velocity.y)*-1;
}
It works and is simple, good luck.
它有效并且很简单,祝你好运。