在余弦曲线上找到与另一个位置有指定距离的位置
Finding a location on cosine curve with a specified distance to another location JS
我正在制作一款"拉力赛"游戏,其中一辆汽车在由余弦曲线组成的山丘上绘图。我知道赛车目前的xspeed
(没有山丘),但问题是我需要知道赛车在山丘上的xspeed
,以便能够将车轮画在正确的位置并保持速度稳定。
function drawWheelOnBasicHill(hillStart, xLocWheel, wheelNro) {
var cw = 400 //the width of the hill
t_max = 2*Math.PI;
var scale = 80, step = cw, inc = t_max/step;
var t1 = (xLocWheel-hillStart)*inc
var y1 = -scale*0.5 * Math.cos(t1);
if(wheelNro == 1 ){ //backwheel
drawRotatedImage(wheel, car.wheel1x, car.wheel1y-y1-45,sx);
//drawing the wheel on canvas
} else { //frontwheel
drawRotatedImage(wheel, car.wheel2x, car.wheel2y-y1-45,sx);
}
for(var i=1; i<=car.speed; i++){ //finding the next xlocation of the wheel with the
//same distance (on the curve) to the previous location as the speed of the car(=the
//distance to the new point on the flat ground)
var t2 = (xLocWheel + i -hillStart)*inc
var y2 = -scale*0.5 * Math.cos(t2);
if(Math.round(Math.sqrt(i^2+(y2-y1)^2))==car.speed){
sx = sx+i; //the new xcoordinate break;
}
}
}
for
循环是问题所在。它可能太慢了(fps 24的动画)。我不明白为什么if
声明现在不起作用。它有时有效,但大多数情况下,条件更新的值达到实际的xspeed
。
有没有更有效和更简单的方法来做到这一点?还是这段代码包含一些错误?非常感谢您为解决这个问题所做的努力!这段代码我看了一整天了
i是变量
x2=x1+i
t2=t1+i*inc
y1=-scale*0.5 * Math.cos(t1)
y2=-scale*0.5 * Math.cos(t2)
有点奇怪。横向应该是与时间无关的,也就是说,y应该只是x的函数。时间步长是外部的,由动画循环的速度决定。更合乎逻辑的模型应该是dx为变量
dt = t2-t1
x2 = x1 + dx
y1 = f(x1) = -0.5*scale*cos(x1)
y2 = f(x2) = -0.5*scale*cos(x2)
和
的交集(x2-x1)^2+(y2-y1)^2 = (speed*dt)^2
简化为
(speed*dt)^2=dx^2+0.25*scale^2*(cos(x1+dx)-cos(x1))^2
对于dx的小值,也就是dt或速度*dt很小的情况,
cos(x1+dx)-cos(x1) is approx. -sin(x1)*dx
,
dx = (speed*dt) / sqrt( 1+0.25*scale^2*sin(x1)^2 )
为了更接近曲线与圆的交点,可以迭代不动点方程
dydx = 0.5*scale*(cos(x1+dx)-cos(x1))/dx
dx = (speed*dt) / ( 1+dydx^2 )
相关文章:
- 地理位置和计算从当前纬度/液化天然气到另一纬度/液化石油气的距离
- 计算地理位置距离返回 NaN
- javascript中两个位置之间的距离
- 在谷歌地图API JS V3上显示距离用户当前位置最近的多个标记,距离30公里
- 获取位置A的位置并计算其到位置B的距离
- 检测当前位置并使用网站的预定义位置计算距离
- Javascript:获取行驶距离和时间,并在谷歌地图中显示当前位置
- 坐标之间的音差地理位置距离
- 获取两个位置之间的距离和时间的功能
- 两个位置之间的距离 - 谷歌地图
- 如何将当前GPS导航器位置设置为destinationA的变量,以查找距离和时间
- 打印从当前位置到另一个位置的距离javascript
- jQuery UI如果移动的距离小于一定距离,则可拖动恢复到原始位置
- 当标记更改位置时,传单不会更新距离值
- Parse.com云代码,计算两个位置之间的行驶距离
- 计算多个位置的距离谷歌地图
- 使用地理位置确定与固定点的距离
- 根据地理位置获得最近的距离
- 谷歌位置Api找到附近的位置与当前位置和附近位置之间的距离排序
- 地理定位手表位置距离计算器