【发布时间】:2016-10-20 05:21:08
【问题描述】:
我正在研究使用力、加速度和质量在笛卡尔地图上进行点对点宇宙飞船旅行的一些逻辑。船将以 1G 的速度加速并燃烧到目的地,在中途标记处翻转 180 度,并以 1G 的速度减速到达目的地的相对停靠点。
我遇到的问题是使用船在加速或减速时所经过的时间来确定 (x, y) 坐标。
这是ship 的规格:
ship = {
mass: 135000, // kg
force: 1324350, // Newtons
p: { x: -1, y: -5 } // (x,y) coordinates
}
dest: {
p: { x: 15, y: 30 } // (x,y) coordinates
}
对于问题的第一部分,我计算到达目的地的时间:
var timeToDestination = function(ship, dest) {
// calculate acceleration (F = ma)
var acceleration = ship.force / ship.mass; // ~9.81 (1G)
// calculate distance between 2 points (Math.sqrt((a - x)^2+(b - y)^2))
var totalDistance = Math.sqrt(
Math.pow(dest.p.x - ship.p.x, 2) +
Math.pow(dest.p.y - ship.p.y, 2)
); // 38.48376280978771
// multiply grid system to galactic scale
var actualDistance = totalDistance * 1e9; // 1 = 1Mkm (38,483,763km) Earth -> Venus
// determine the mid-point where ship should flip and burn to decelerate
var midPoint = actualDistance / 2;
// calculate acceleration + deceleration time by solving t for each: (Math.sqrt(2d/a))
return Math.sqrt( 2 * midPoint / acceleration ) * 2; // 125,266s or 34h or 1d 10h
}
第二部分有点棘手,在增量时间之后获取 (x, y) 坐标。这就是我卡住的地方,但这是我目前所拥有的:
var plotCurrentTimeDistance = function(ship, dest, time) {
// recalculate acceleration (F = ma)
var acc = ship.force / ship.mass; //~9.81m/s^2
// recalculate total distance
var distance = Math.sqrt(
Math.pow(dest.p.x - ship.p.x, 2) +
Math.pow(dest.p.y - ship.p.y, 2)
) * 1e9; // 38,483,762,810m
// get distance traveled (d = (1/2) at^2)
var traveled = (acc * Math.pow(time, 2)) / 2;
// get ratio of distance traveled to actualDistance
var ratio = traveled / distance;
// midpoint formula to test @ 50% time ((x+a)/2,(y+b)/2)
console.log({ x: (ship.p.x+dest.p.x)/2, y: (ship.p.y+dest.p.y)/2})
// get the point using this formula (((1−t)a+tx),((1−t)b+ty))
return {
x: (( 1 - ratio ) * ship.p.x) + (ratio * dest.p.x),
y: (( 1 - ratio ) * ship.p.y) + (ratio * dest.p.y)
};
}
@ 50% 时间,62,633s 点返回为 (~7, ~12.5),与返回为 (~7, ~12.5) 的中点公式相匹配。但是,您输入的任何其他距离/时间都将大错特错。我的猜测是加速度会打乱计算,但我不知道如何更改公式以使其工作。感谢您的宝贵时间。
【问题讨论】:
-
在
// get distance travelled (d = (1/2) at^2) var travelled = (acc * Math.pow(time, 2)) / 2;distance不用作计算中的变量?(d = (1/2) at^2的预期结果是什么?timeplotCurrentTimeDistance是timeToDestination的返回值吗? -
travelled用于确定plotCurrentTimeDistance()函数的比率。(d = (1/2) at^2返回 n 秒后经过的距离为time -
time在plotCurrentTimeDistanceMath.sqrt( 2 * midPoint / acceleration ) * 2是从timeToDestination返回的吗?为什么在计算结束时包含* 2,而/ 2位于(acc * Math.pow(time, 2)) / 2?计算中的唯一动态变量是acc? -
不,
time是动态变量。您正试图根据船在一定数量的time后行驶的距离来确定笛卡尔地图(x,y)上的点。第一个*2只是将距离的两半(加速+减速)相加(我们将距离一分为二)。/2只是重新排列公式来解决时间而不是距离。还是一样的公式。
标签: javascript math physics game-physics