N 体重力/太阳系 Javascript 模拟

斯洛帕克斯

美好的一天,我正在尝试用 javascript 创建一个简单的 2D 太阳系模型,但是在理解如何计算下一帧行星的位置以及其他一些我将详细介绍的部分时遇到了一些麻烦随着很快。

看完这个非常好的视频和他的一大堆其他视频,我制作了一个快速的 MS 绘画图像来尝试简化我的情况。在第二个场景中,您可以看到新位置是使用速度、引力和这两个“方向”之间的角度来计算的?

在此处输入图片说明

我无法理解如何解决这一切。

Below is a JS fiddle of my code. You'll notice I'm trying my best to use real NASA given data to keep it accurate.

You'll want to look specifically at lines 138 which is where all the calculations for its next move are made.

https://jsfiddle.net/c8eru7mk/9/

attraction: function(p2) {
    // Distance to other body
    var dx = p2.position.x - this.position.x;
    var dy = p2.position.y - this.position.y;
    var d = Math.sqrt(dx ** 2 + dy ** 2); // Possibly correct

    // Force of attracrtion
    this.f = G * (this.mass * p2.mass) / (d ** 2); // Possibly Correct

    // Direction of force, If you read it hard enough you should be able to hear my screams of pain
    // Not sure if this is correct, most likely not.
    var theta = Math.atan2(dy, dx);
    var fx = Math.cos(theta) * this.f;
    var fy = Math.sin(theta) * this.f;

    this.velocity.x += fx / this.mass;
    this.velocity.y += fy / this.mass;

    this.position.x += this.velocity.x;
    this.position.y += this.velocity.y;
}

The problems I'm currently facing are

  • If I am to use NASA values, the distance between planets is so big, they won't fit on the screen, and I can't simply scale the distances down by multiplying them by 0.0002 or whatever, as that'll mess with the gravitational constant, and the simulation will be completely off.
  • I have no idea how to caluclate the next position and my brain has imploded several times this past week trying to attempt it several times.
  • I have no idea on how to check if my configuration data of planets is wrong, or if the simulation is wrong, so I'm pretty much just guessing.

This is also my first time actually coding anything more complex than a button in javascript too, so feedback on code layout and whatnot is welcome!

Many thanks

coproc

Using NASA values is not a problem when using separate coordinates for drawing. Using an appropriate linear transfomration from real coordinates to screen coordinatees for displaying does not influence the physical values and computations.

For simulating the motion of a planet with iterative updates one can assume that the gravitational force and the velocity are constant for a small portion of time dt. This factor dt is missing in your conversions from accelration to velocity and from velocity to distance. Choosing an appropriate value for dt may need some experiments. If the value is too big the approximation will be too far off from reality. If the value is too small you may not see any movement or rounding errors may influence the result.

For the beginning let us assume that the sun is always at (0,0). Also for a start let us ignore the forces between the planets. Then here are the necessary formulas for a first not too bad approximation:

  • 行星在(x,y)太阳引力作用下的标量加速度(质量为M):a = G*M/(d*d)其中d=sqrt(x*x+y*y)请注意,这与行星的质量无关。
  • 加速度向量:ax = -a*x/day = -a*y/d(向量(-x,-y)指向太阳,必须带上长度a
  • 行星速度的变化(vx,vy)vx += ax*dtvy += ay*dt
  • 行星位置的变化:x += vx*dty += vy*dt

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

随机太阳系模拟碰撞检测问题

来自分类Dev

太阳系模拟器物理集成问题(虚幻引擎4,C ++)

来自分类Dev

JavaScript中的N体重力仿真

来自分类Dev

就尺寸和质量而言,是否可以进行逼真的n体太阳系仿真?

来自分类Dev

OpenGL C ++-太阳系

来自分类Dev

太阳系轨道HTML中心

来自分类Dev

太阳系模型动态壁纸

来自分类Dev

无法在我的太阳系模型上获得正确的轨道

来自分类Dev

OpenGL 中的太阳系,相机位置

来自分类Dev

Java模拟现实重力

来自分类Dev

javascript-如何模拟时区?

来自分类Dev

JavaScript Regexp模拟回声语句

来自分类Dev

页面上的Javascript点击模拟

来自分类Dev

JavaScript:使用值模拟点击

来自分类Dev

模拟实体关系n:m

来自分类Dev

可以给我一些关于太阳系的源代码示例(android opengl,opengl)

来自分类Dev

尝试模拟重力时奇怪的行为

来自分类Dev

在重力模拟中物体缓慢向右漂移

来自分类Dev

用箭头功能模拟javascript函数

来自分类Dev

Javascript-模拟空格键

来自分类Dev

从JavaScript中删除的ClojureScript模拟是什么?

来自分类Dev

使用javascript函数调用模拟表单提交

来自分类Dev

用JavaScript模拟Gameboy ROM中的音频

来自分类Dev

“模拟” JavaScript中的32位整数溢出

来自分类Dev

Javascript模拟“悬停”触发CSS属性

来自分类Dev

如何使用javascript模拟div的点击

来自分类Dev

如何在javascript中模拟文件?

来自分类Dev

无法模拟通过javascript单击提交按钮

来自分类Dev

如何在JavaScript中模拟按键?