太阳系模拟器物理集成问题(虚幻引擎4,C++)
Solar System Simulator Physics Integration Issues (Unreal Engine 4, C++)
所以我正在Unreal Engine 4中使用C++为一个大学项目制作这个太阳系模拟器,然而,我对C++和UE4是新手,而且我的数学很差,所以我需要一些帮助,我现在想使用欧拉积分器来获得一些基本的物理信息,让月球绕地球运行,然后可能使用速度维莱特方法,以这种方式构建整个太阳系。然而,到目前为止,即使是Euler积分也不起作用。这是Moon.cpp 中的代码
//Declare the masses
float MMass = 109.456;
float EMass = 1845.833;
//New velocities
float NewMVelX = 0.0;
float NewMVelY = 0.0;
float NewMVelZ = 0.0;
//Distance
float DistanceX = 0.0;
float DistanceY = 0.0;
float DistanceZ = 0.0;
//Earth's velocity
float EVelocityX = 0.0;
float EVelocityY = 0.0;
float EVelocityZ = 0.0;
//Moon's base velocity
float MVelocityX = 0.1;
float MVelocityY = 0.0;
float MVelocityZ = 0.0;
//Moon's acceleration
float MForceX = 0.0;
float MForceY = 0.0;
float MForceZ = 0.0;
//New position
float MPositionX = 0.0;
float MPositionY = 0.0;
float MPositionZ = 0.0;
// Called every frame
void AMoon::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//Get Earth Location
FVector EPosition = FVector(0.0, 0.0, 0.0);
//Get Moon Location
FVector MPosition = GetActorLocation();
//Get the distance between the 2 bodies
DistanceX = (MPosition.X - EPosition.X) / 100;
DistanceY = (MPosition.Y - EPosition.Y) / 100;
//DistanceZ = MPosition.Z - EPosition.Z / 100;
//Get the acceleration/force for every axis
MForceX = G * MMass * EMass / (DistanceX * DistanceX);
MForceY = G * MMass * EMass / (DistanceY * DistanceY);
//MForceZ = G * MMass * EMass / (DistanceZ * DistanceZ);
//Get the new velocity
NewMVelX = MVelocityX + MForceX;
NewMVelY = MVelocityY + MForceY;
//NewMVelZ = MVelocityZ + MForceZ * DeltaTime;
//Get the new location
MPositionX = (MPosition.X) + NewMVelX;
MPositionY = (MPosition.Y) + NewMVelY;
//MPositionZ = MPosition.Z * (MVelocityZ + NewMVelZ) * 0.5 * DeltaTime;
//Set the new velocity on the old one
MVelocityX = NewMVelX;
MVelocityY = NewMVelY;
//MVelocityZ = NewMVelZ;
//Assign the new location
FVector NewMPosition = FVector(MPositionX, MPositionY, MPositionZ);
//Set the new location
SetActorLocation(NewMPosition);
}
这些值可能不正确,我只是在做测试。我根据在谷歌和多个网站上获得的不同信息编写了这段代码,但在这一点上我很困惑。现在的情况是,月球只是开始朝着一个方向前进,而且从未停止。我知道我的问题是地球的力/加速度/实际重力,它应该拉月球,而不是把它推开。但不管怎样,如果有人知道我做错了什么,我会非常感激听到你的话!感谢
力取决于欧氏旋转不变距离。因此使用
distance = sqrt(distanceX²+distanceY²+distanceZ²)
force = - G*Emass*Mmass/distance²
forceX = force * X/distance
forceY = force * Y/distance
forceZ = force * Z/distance
速度的时间步进也是错误的,应该是
velocityX += forceX/Mmass * deltaTime
velocityY += forceY/Mmass * deltaTime
velocityZ += forceZ/Mmass * deltaTime
当然,位置更新也包含时间步长
positionX += velocityX * deltaTime
....
相关文章:
- 当使用带有VS2019或VSCode的虚幻引擎4.24.2时,我如何修复这些错误的Intellisense错误
- Unity在虚幻引擎4中的"Vector3.Slerp"等效C++?
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- 在虚幻引擎中删除NXOpen对象时崩溃
- 引擎节点:未定义的符号:_ZTV6Config
- Agora.io 虚幻引擎插件构建错误
- <random>在实践中应该实际使用哪个随机数引擎? std::mt19937?
- 无法在 Arch Linux 中启动虚幻引擎 4
- 在虚幻引擎中触发C++ dll的事件
- 在 C/C++ 中加载 OpenSSL 自定义引擎
- 组件上的虚幻引擎可蓝图UFUNCTION会导致构建错误
- 使用Visual Studio在虚幻引擎中创建一个新的类c ++给了我太多的错误
- 提高基于组件的游戏引擎的效率
- 在C++中嵌入 Mozilla 的 JavaScript 引擎
- 如何在虚幻引擎4中将char*转换为TCHAR?
- 如何将外部代码包含在虚幻引擎4 C++项目中
- 如何使用虚幻引擎"filter"多播?
- 我可以在没有Xcode的情况下在Mac上使用带有C++的虚幻引擎吗?
- 虚幻引擎C++生成演员错误
- DirectX 11 引擎在第三次代码改进后没有突然运行,我不知道为什么