javascript - 《Build an HTML5 Game》一书中开发游戏时如何计算碰撞

标签 javascript math 2d-games

我不明白作者在计算两个圆(气泡)之间的碰撞时登录。这是Calculating collisions部分。

作者写道:

The bubble being fired follows a set of coordinates defined by the equations:

px = ex + tdx py = ey + tdy

where px and py are points on the trajectory of the bubble’s center point. The calculation of px and py happens in jQuery’s animate method and is the standard equation for moving a point along a line. Next, we’ll calculate t at the closest point on this line to the center of the bubble that we’re checking against:

var t = dx * distToBubble.x + dy * distToBubble.y;

我不明白t是什么以及为什么它按以下公式计算:

var t = dx * distToBubble.x + dy * distToBubble.y;?

最佳答案

这与编程几乎没有关系,它绝对是数学。所以我认为这个问题不应该在这里,但我会回答它只是为了帮助你。

I don't understand what is t

被发射的气泡的运动线由 parametric equation 定义。 ,其中参数是变量t

why it calculates by the following formula

这个有点复杂,书上只是展示了下面计算的最终结果。有一个公式可以计算点和线之间的距离。在这种情况下,该点是目标气泡的中心,并且该线由前面显示的参数方程表示。解决这个问题后,您可以推导该方程来找到最小距离。

尽管有更简单的方法可以解决这个问题。使用dot product使用参数函数将目标气泡的原点与射出的气泡的坐标之间的距离设为0(垂直交集)即可求出t的值。

这里重要的一点是,这本书已经为您解决了问题,并且它显示了最终结果,以便您可以使用它。

祝您游戏愉快!

关于javascript - 《Build an HTML5 Game》一书中开发游戏时如何计算碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31269178/

相关文章:

javascript - mozilla 的绑定(bind)函数问题

javascript - 自定义 NodeJS Grunt 命令

javascript - Angular JS 组件绑定(bind)

math - 可以用N个键创建的二进制搜索树的可能数目由第N个加泰罗尼亚语数字给出。为什么?

algorithm - 实现 2D 多体碰撞解决方案

javascript - 正则表达式(en-US)查找所有标点符号,除了单词的一部分

c# - 在几个小时内平均分配项目数量

algorithm - 这个算法的运行时间复杂度是多少

java - 通用计数器与多事件计数器

java - ArrayIndexOutOfBoundsException 二维游戏开发