如何计算反弹角? [英] How to calculate bounce angle?

查看:991
本文介绍了如何计算反弹角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打得四处了一段时间,但我根本无法弄明白。

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.

现在,我还没有得到任何障碍,导弹刚刚反弹时,他们得到外 viewportRectangle 我做了。

Right now I haven't got any obstacles, the missiles just bounce off when they get outside the viewportRectangle I made.

时我正在寻找解决方案比较先进?

Is the solution I'm looking for quite advanced?

有没有relativly简单的方法来做到这一点?

Is there a relativly simple way to do it?

推荐答案

我觉得一个更简单的方式做到这一点是使用导弹的速度,而不是计算的角度。假设你有一个具有 xVelocity yVelocity 水平和垂直重新present其运动导弹。这些速度可以是正的或负的重新present向左,向右,向上或向下。

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.


  • 如果一枚导弹击中一个顶部或底部边框扭转 yVelocity
  • 的符号
  • 如果导弹击中左侧或右侧边框扭转 xVelocity
  • 的符号
  • 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.

这将保持在相对轴相同的运动。

This will keep the movement in the opposite axis the same.

中借鉴<一个图像href=\"http://stackoverflow.com/questions/573084/how-to-calculate-bounce-angle/573100#573100\">ChrisF's回答,让我们说,导弹在我的位置开出。

Borrowing the image from ChrisF's answer, let's say the missile starts out at position I.

随着 xVelocity yVelocity 均为阳性(在2D图形向右和向下通常是正)导弹将在所指示的方向行进。让我们只分配值

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 = 3
yVelocity = 4

当导弹击中墙壁在位置 C ,其 xVelocity 不应改变,但其 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.

这种方法的好处是,你只需要跟踪导弹的 xPosition位置 yPosition xVelocity yVelocity 。只用这四个组件和游戏的更新速度,导弹总是会得到重新绘制在正确位置。一旦你进入那个是不是在直角度或正在更复杂的障碍,这将是一个更容易与X和Y速度上班比的角度。

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.

这篇关于如何计算反弹角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆