Har*_*uin 2 javascript algorithm equation createjs
我的画布上有一个圆圈.鼠标位置是相对于画布计算的.我希望当鼠标距离它<= 100px时,圆圈会移动.开始移动的最小距离为100px,为0.5px/tick.它在20px距离处达到2px/tick.
到目前为止,当距离小于或等于100时,我移动圆圈 - (我正在使用画架库)
function handleTick() {
distance = calculateDistance(circle, mX, mY);
if (distance<=100) {
circle.x += 0.3;
stage.update();
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的是
function handleTick() {
distance = calculateDistance(circle, mX, mY);
if (distance<=100) {
circleSpeed = // equation that takes distance and outputs velocity px/tick.
circle.x += circleSpeed;
stage.update();
}
}
Run Code Online (Sandbox Code Playgroud)
所以我认为这是一个数学问题并将其发布在数学交换上,但到目前为止还没有答案.我试过谷歌搜索几个主题,如:"如何得出一个关系的方程",因为我有域(100,20)和范围(0.5,2).什么功能可以与他们联系?
事情是我不擅长数学,这些数字甚至可能没有关系 - 我不确定我在这里寻找什么.
我应该写一个随机算法" circleSpeed = 2x + 5x;"并希望它做我想要的吗?或者有可能像我一样 - "我希望这些是最小值和最大值,现在我需要为它提出一个等式"?
向右方向的指针会很棒,因为到目前为止我在黑暗中拍摄.
如果我理解正确的话,你想circleSpeed成为的功能distance,使得
circleSpeed是0.5时候distance为100.circleSpeed是2时候distance为20.有无限功能可以实现这一点,所以我将假设线性.
与斜率线的方程式m和包含点(x?,y?)是
y = m (x-x?) + y?
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,你有两个点,(x?,y?)并且(x?,y?),这样你就可以计算与斜率
y? - y?
m = ???????
x? - x?
Run Code Online (Sandbox Code Playgroud)
所以这条线的方程是
y? - y?
y = ??????? (x - x?) + y?
x? - x?
Run Code Online (Sandbox Code Playgroud)
有了你的数据,
0.5 - 2
y = ???????? (x - 20) + 2 = -0.01875 x + 2.375
100 - 20
Run Code Online (Sandbox Code Playgroud)
因此,
circleSpeed = -0.01875 * distance + 2.375
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
92 次 |
| 最近记录: |