问题描述
我正在尝试制作一个自上而下的太空飞船游戏,并且我希望该运动更加逼真.具有惯性,重力等的360度.
我的问题是我可以使飞船毫无惯性地移动360°,但是我需要做的是限制发动机的行驶速度,同时又不限制其他推动/拉动飞船的力.
>因此,如果引擎速度最大为500,并且船舶在重力井中驶向1000,则在引擎开启时船舶不会驶向1500,但是如果方向偏离角度,则它可能会放慢速度.
对于它的价值,我使用的是构造,而我所需要的只是它的数学原理. /p>
感谢您的帮助,我正试图解决这个问题.
(有关我正在使用的C ++代码段和正在运行的演示,请参见下文 [仅适用于Windows ].)
- 将常数c设置为对象可以达到的最大速度(游戏中的光速").
- 如果施加力会增加对象的速度,则将加速度(速度变化)除以洛伦兹因子. 就狭义相对论而言,if条件并不现实,但它使飞船在高速行驶时更可控".
- 更新:通常,在以接近c的速度航行时,船舶将难以操纵,因为改变方向需要将速度推过c的加速度(洛伦兹因数最终将使在新方向上的加速度几乎缩放为零).要重新获得可操作性,请使用没有经过Lorentz缩放的速度矢量本来的方向以及缩放后的速度矢量的大小.
说明:
洛伦兹因子的定义,其中v是速度,c是光速:
之所以起作用,是因为随着速度的增加,洛伦兹因子接近无穷大.物体将需要施加无限量的力来跨越光速.在较低的速度下,洛仑兹因子非常接近1,接近经典的牛顿物理学.
随速度增加的洛伦兹因子图:
注意:我以前尝试通过摩擦设置来解决小行星游戏中的类似问题.在阅读您的问题时,我只是想出了这个解决方案^^
更新: 进行了修改,以考虑矢量的方向,从而使船舶不会在高速下失去可控制性".
更新:这是我的小行星游戏的代码段,该代码段使用洛伦兹因子来限制游戏对象的速度.效果很好!
更新:*添加了可下载的演示(仅Windows;根据源代码构建其他平台).我不确定zip中是否包含所有依赖项;请让我知道是否有东西丢失.玩得开心^^
void CObject::applyForces()
{
// acceleration: change in velocity due to force f on object with mass m
vector2f dv = f/m;
// new velocity if acceleration dv applied
vector2f new_v = v + dv;
// only apply Lorentz factor if acceleration increases speed
if (new_v.length() > v.length())
{
// maximum speed objects may reach (the "speed of light")
const float c = 4;
float b = 1 - v.length_squared()/(c*c);
if (b <= 0) b = DBL_MIN;
double lorentz_factor = 1/sqrt(b);
dv /= lorentz_factor;
}
// apply acceleration to object's velocity
v += dv;
// Update:
// Allow acceleration in the forward direction to change the direction
// of v by using the direction of new_v (without the Lorentz factor)
// with the magnitude of v (that applies the Lorentz factor).
if (v.length() > 0)
{
v = new_v.normalized() * v.length();
}
}
I'm trying to make a top-down spaceship game and I want the movement to somewhat realistic. 360 degrees with inertia, gravity, etc.
My problem is I can make the ship move 360° with inertia with no problem, but what I need to do is impose a limit for how fast the engines can go while not limiting other forces pushing/pulling the ship.
So, if the engines speed is a maximum of 500 and the ship is going 1000 from a gravity well, the ship is not going to go 1500 when it's engines are on, but if is pointing away from the angle is going then it could slow down.
For what it's worth, I'm using Construct, and all I need is the math of it.
Thanks for any help, I'm going bald from trying to figure this out.
Take a page from relative physics, where objects cannot exceed the speed of light:
(See below for my working C++ code snippet and running demo [Windows only].)
- Set the constant c to the maximum speed an object can reach (the "speed of light" in your game).
- If applying a force will increase the speed of the object, divide the acceleration (change in velocity) by the Lorentz factor. The if condition is not realistic in terms of special relativity, but it keeps the ship more "controllable" at high speeds.
- Update: Normally, the ship will be hard to maneuver when going at speeds near c because changing direction requires an acceleration that pushes velocity past c (The Lorentz factor will end up scaling acceleration in the new direction to nearly nothing.) To regain maneuverability, use the direction that the velocity vector would have been without Lorentz scaling with the magnitude of the scaled velocity vector.
Explanation:
Definition of Lorentz factor, where v is velocity and c is the speed of light:
This works because the Lorentz factor approaches infinity as velocity increases. Objects would need an infinite amount of force applied to cross the speed of light. At lower velocities, the Lorentz factor is very close to 1, approximating classical Newtonian physics.
Graph of Lorentz factor as velocity increases:
Note: I previously tried to solve a similar problem in my asteroids game by playing with friction settings. I just came up with this solution as I read your question^^
Update: The algorithm has been modified to account for directions of vectors so the ship does not "lose controllability" at high speeds.
Update: Here is a code snippet from my asteroids game, which uses the Lorentz factor to limit the speed of game objects. It works pretty well!
update:* added downloadable demo (Windows only; build from source code for other platforms) of this algorithm in action. I'm not sure if all the dependencies were included in the zip; please let me know if something's missing. And have fun^^
void CObject::applyForces()
{
// acceleration: change in velocity due to force f on object with mass m
vector2f dv = f/m;
// new velocity if acceleration dv applied
vector2f new_v = v + dv;
// only apply Lorentz factor if acceleration increases speed
if (new_v.length() > v.length())
{
// maximum speed objects may reach (the "speed of light")
const float c = 4;
float b = 1 - v.length_squared()/(c*c);
if (b <= 0) b = DBL_MIN;
double lorentz_factor = 1/sqrt(b);
dv /= lorentz_factor;
}
// apply acceleration to object's velocity
v += dv;
// Update:
// Allow acceleration in the forward direction to change the direction
// of v by using the direction of new_v (without the Lorentz factor)
// with the magnitude of v (that applies the Lorentz factor).
if (v.length() > 0)
{
v = new_v.normalized() * v.length();
}
}
这篇关于2D宇宙飞船运动数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!