通过距离和摩擦计算速度 [英] Calculate speed by distance and friction

查看:200
本文介绍了通过距离和摩擦计算速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Javascript,Canvas,HTML5编写一个游戏,我刚刚发现了一个与先进数学相关的大问题。游戏是在平面2D,所以你看到从过去的世界。这意味着没有重力,只有摩擦。

I am writing a game in Javascript, Canvas, HTML5, and I just found a big problem, related to advanced maths. The game is in flat 2D, so you see the world from over. That means there is no gravity, only friction.

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, my question is: How do I calculate the speed in the section where I create the ball, to make the ball stop at exactly the targets x and y position? As far as I've come I've only calculated the starting distance between the ball and the targets position.. Soo, any ideas? Is there an equation for this?

推荐答案

因此,你希望i的和从1到∞, i ),并且您希望结果等于dist。这是一个无限的几何系列,其极限是速度×摩擦/(1-摩擦) )。

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, the limit of which is speed×friction/(1 − friction).

因此,只需求解速度方程,即可得到:

So simply solve the equation for speed, and you obtain:

speed = (1/friction - 1)*dist

这篇关于通过距离和摩擦计算速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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