我正在用 Javascript、Canvas、HTML5 编写一个游戏,我刚刚发现了一个与高等数学相关的大问题。该游戏是平面 2D 游戏,因此您可以从另一个角度看世界。这意味着没有重力,只有摩擦力。
CODE:
var friction = 0.97
var target = {
x: 70,
y: 90,
}
var ball = {
x: 10,
y: 20,
vx: 0,
vy: 0,
}
var dx = ball.x - target.x
var dy = ball.y - target.y
var angle = Math.atan2(dy, dx)
var dist = Math.sqrt(dx * dx + dy * dy) // <--- AS FAR AS I'VE COME
var speed = ??? // <--- HERE IS THE PROBLEM
ball.vx = Math.cos(angle) * speed
ball.vy = Math.sin(angle) * speed
function update() {
ball.vx *= friction
ball.vy *= friction
ball.x += ball.vx
ball.y += ball.vy
// Drawing and other unneccesary things to involve here
window.requestAnimationFrame(update)
}
update()
所以,我的问题是:如何计算创建球的部分的速度,以使球准确地停在目标 x 和 y 位置?就我而言,我只计算了球和目标位置之间的起始距离。Soo,有什么想法吗?有一个方程式吗?
So you want the sum for i from 1 to ∞ over the term (speed × frictioni), and you want the result to equal dist. This is an infinite geometric series http://en.wikipedia.org/wiki/Geometric_series, the limit of which is speed×friction/(1 − friction).
因此,只需求解速度方程,即可得到:
speed = (1/friction - 1)*dist
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)