太阳系模拟器物理集成问题(Unreal Engine 4,C++)
Solar System Simulator Physics Integration Issues (Unreal Engine 4, C++)
所以我在 Unreal Engine 4 中使用 C++ 为一个大学项目制作了这个太阳系模拟器,但是,我是 C++ 和 UE4 的新手,而且我的数学很烂,所以我需要一个有点帮助,我现在想使用 Euler 积分器来获得一些基本物理学知识,让月球绕地球运行,然后继续使用 Velocity Verlet 方法并以这种方式构建整个太阳系。然而,截至目前,即使是欧拉积分也不起作用。这是 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);
}
可能数值不对,我这时候只是在做测试。我将此代码基于我在 Google 和多个网站上获得的不同信息,但此时我很困惑。正在发生的事情是月亮刚开始朝一个方向移动并且永远不会停止。我知道我的问题是地球的 force/acceleration/actual 引力,它应该拉动月球而不是将其推开。但无论如何,如果有人知道我做错了什么,我会很高兴听到你要说的话!谢谢
力取决于欧氏旋转不变距离。因此使用
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
....
所以我在 Unreal Engine 4 中使用 C++ 为一个大学项目制作了这个太阳系模拟器,但是,我是 C++ 和 UE4 的新手,而且我的数学很烂,所以我需要一个有点帮助,我现在想使用 Euler 积分器来获得一些基本物理学知识,让月球绕地球运行,然后继续使用 Velocity Verlet 方法并以这种方式构建整个太阳系。然而,截至目前,即使是欧拉积分也不起作用。这是 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);
}
可能数值不对,我这时候只是在做测试。我将此代码基于我在 Google 和多个网站上获得的不同信息,但此时我很困惑。正在发生的事情是月亮刚开始朝一个方向移动并且永远不会停止。我知道我的问题是地球的 force/acceleration/actual 引力,它应该拉动月球而不是将其推开。但无论如何,如果有人知道我做错了什么,我会很高兴听到你要说的话!谢谢
力取决于欧氏旋转不变距离。因此使用
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
....