gpt4 book ai didi

javascript - 随时间绘制 spaceship 距离

转载 作者:行者123 更新时间:2023-11-29 14:39:50 25 4
gpt4 key购买 nike

我正在研究使用力、加速度和质量在笛卡尔 map 上进行点对点宇宙飞船旅行的一些逻辑。飞船将以 1G 的加速度加速燃烧至目的地,在中途标记处翻转 180 度,并以 1G 的加速度减速到达目的地的相对停靠点。

我遇到的问题是使用船在加速或减速时行驶的时间来确定 (x, y) 坐标。

这是飞船的规范:

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) 的中点公式相匹配。但是,您输入的任何其他距离/时间都将大错特错。我的猜测是加速打乱了计算,但我无法弄清楚如何更改公式以使其工作。感谢您的宝贵时间。

最佳答案

首先,您说distance 是总距离,但它实际上是从船到目的地的剩余距离。我不完全理解您关于如何进行计算的计划,因此我将在下面提出一些不同的建议。

我们称起始位置为 start,它有一个 x 和 y 坐标:start.xstart.y。与 end 类似。

现在,为了使这样的模拟正常工作,我们还需要将速度作为船上的一个属性,所以

ship = {
...
v : {x : 0, y : 0}
}

它会从静止开始,也应该到达静止。理想情况下,它应该为一般运动提供加速度 a,但我们现在可以跳过它。我们会有两种不同的行为

  1. 飞船从静止开始,以 9.8 m/s^2 的速度朝目标加速,直到到达一半点。
  2. 飞船在中点以 v 的速度开始,以 -9.8 m/s^2 减速朝向目标,直到速度为 0。现在我们应该已经到达目标了。

要从加速度中获取速度,我们使用 v = v_start + a*time,并从速度中获取位置,我们使用 x = x_start + v*time。那么对于这两种情况,船的当前位置就是

// Case 1
current = start + a*time*time
// the above is due to the fact that
// current = start + v*time
// and the fact that v = v_start + a*time as I wrote previously,
// with v_start.x = v_start.y = 0

//Case 2
current = midpoint + (v_at_midpoint - a*time_since_midpoint)*time_since_midpoint

注意这里的startcurrenta都是向量,所以它们会有一个xy(可能还有 z)坐标。

通过以下算法得到的加速度

 a = (start - end)/totalDistance * 9.81 // 9.81 is the desired acceleration -- change it to whatever you want

如果你想了解上面的实际含义,它会计算所谓的unit vector。 ,它告诉我们力指向的方向

您现在需要做的事情如下:

  1. 计算总距离和加速度

  2. 在给定函数中的输入 time 的情况下,确定您是情况 1 还是情况 2。

  3. 到达中点后,存储速度和到达中点所需的时间,并使用它来确定情况 2 中的运动。

  4. 一旦到达目的地就停下来,否则您最终会回到起点!

祝你好运!

P.S 我还应该注意这里没有考虑 special relativity ,所以如果你们的距离相距太远,你将获得非物理速度。但是,如果您想考虑这一点,事情会变得更加困惑。

关于javascript - 随时间绘制 spaceship 距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40146032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com