运动和速度增加量,反之亦然
除了代数 1 之外,我对数学不太了解,但是我通常可以“破解”一本数学书,并使方程在我编写的软件中运行 - 我正在尝试编写一个程序,让物体在屏幕上移动,具有动量、速度、质量、推力等,这似乎被称为矢量数学。
我将如何在笛卡尔坐标 x 和 x 的世界之间进行转换?进入物理世界了吗?例如,如何确定循环每次迭代的增量量,以便创建一个飞行物体,该飞行物体通过简单的左右上下推进器的行为来模拟(2d)航天器?
我试图弄清楚的一个例子:
x=x+getnextstep(thrust, direction).x
y=y+getnextstep(thrust, direction).y
所以我会输入一个量,例如推力为 0-127,方向为 0-360,然后返回增量的量
如果可能并且您有耐心,请用伪代码回答足够了,解释一下笛卡尔逐步世界和动量/质量/速度世界之间的转换是如何完成的。
I don't really know math well beyond Algebra 1, however I can usually "hack" a math book and make the equations work in software I write - I'm trying to write a program that has objects moving onscreen, with momentum, velocity, mass, thrust, etc which seems to be called Vector Math.
How would I go about converting between the world of cartesian coordinates x & y to the world of physics? For instance, how do I determine the increment amount per iteration of a loop in order to create a flying object that simulates a (2d) spacecraft in terms of behavior with simple left-right-up-down thrusters?
An example of what I"m trying to figure out:
x=x+getnextstep(thrust, direction).x
y=y+getnextstep(thrust, direction).y
so I would input an amount, say 0-127 for thrust and 0-360 as direction, and get back the amount to increment
Please answer in psuedocode if possible and if you feel patient enough, explain the how the conversion is done between the cartesian step-wise and momentum/mass/velocity world.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从一维开始。
速度是位置随时间的变化率。在时间 t1 时,对象位于位置 x1。您让它以恒定速度 v 从时间 t1 滑行到时间 t2。它行驶的距离 (x2-x1) 是速度 (v) 乘以经过的时间 (t2-t1 ):
(x2-x1) = v (t2-t1)
x2 = x1 + v (t2-t1)
或者在代码中:
x += v dt
其中 dt 是自您上次更新所有内容以来的(模拟)时间量。
加速度是速度随时间的变化率。如果加速度恒定,则必须跟踪 x 和 v,我们得到
x += v dt + a(dt)/22
v += a dt
(请注意,如果不更改代数,则无法颠倒这些步骤的顺序。)
这足以模拟使用单个推进器来回滑动的物体。您控制由推进器提供的“a”。如果你想在重力场中进行垂直运动,重力会施加自己的加速度;只需将重力添加到推进器的加速度上,这就是您的a。 (根据定义,重力加速度指向下,按照惯例,它被称为“g”。)
现在不要担心动量或能量。或者摩擦力或者空气阻力。或者碰撞。
2D:x 方向的运动和 y 方向的运动基本上是独立的,因此您可以将它们视为两个 1D 运动模拟:{x, vx, ax} 和 { y, vy, ay}。
如果您想要一个可以指向的推进器(例如0-360),则必须将该矢量分解为x分量和y分量,然后应用上述内容。像这样分解一个向量就是三角学;如果你不熟悉它,你就必须破解一本书(或维基百科)。事情没那么复杂,但是这里很难解释。
这应该会让你忙碌一段时间。如果它太简单并且您想要更大的挑战,请查看物理介绍性文本的前几章。
Start with 1D.
Velocity is the rate of change of position over time. At time t1, an object is at position x1. You let it coast at constant velocity v, from time t1 to time t2. The distance it's traveled (x2-x1) is velocity (v) multiplied by elapsed time (t2-t1):
(x2-x1) = v (t2-t1)
x2 = x1 + v (t2-t1)
Or in code:
x += v dt
where dt is the amount of (simulated) time since you last updated everything.
Acceleration is the rate of change of velocity over time. If acceleration is constant, you have to keep track of x and v, and we get
x += v dt + a(dt)/22
v += a dt
(Note that you can't reverse the order of those steps, not without changing the algebra.)
That's enough to simulate an object sliding back and forth with a single thruster. You control the "a" which is supplied by the thruster. If you want vertical motion in a gravity field, gravity applies its own acceleration; just add gravity to the acceleration of the thruster, and that's your a. (The acceleration of gravity points down, by definition, and by convention it's called "g".)
Don't worry about momentum or energy for now. Or friction or air resistance. Or collisions.
2D: motion in x and motion in y are basically independent, so you can just treat them as two 1D motion simulations: {x, vx, ax} and {y, vy, ay}.
If you want a thruster you can point (e.g. 0-360), you must break up that vector into an x component and a y component, then apply the above. Breaking up a vector like that is trigonometry; if you're not familiar with it, you'll have to crack a book (or wikipedia). It's not that complicated, but it's hard to explain here.
That should keep you busy for a while. If it's too easy and you want a greater challenge, look at the first few chapters in an introductory physics text.