# Gravity, Orbits And Velocity Verlet

Hello!

I’m new to game programming and C++, I’m trying to make a Solar System simulator with physics behind it for college and I’m having some issues with the maths. I’m trying to use the velocity verlet integrator but it’s not quite working (I suck at maths). Unreal is actually crashing at the moment whenever I try to play what I have. All I’m trying to do right now is orbiting the moon around a stationnary earth. Here’s the code in Moon.cpp

``````float DistanceX = 0.0, DistanceY = 0.0, DistanceZ = 0.0;
float MMass = 109.456; //Mass of Moon
float EMass = 1854.833; //Mass of Earth
float VelX = 0.0, VelY = 0.0, VelZ = 0.0; //New velocities

float EVelocityX = 0; //Velocity of Earth for when things actually work
float EVelocityY = 0;
float EVelocityZ = 0;

float MVelocityX = 100; //Base velocity of the Moon
float MVelocityY = 0;
float MVelocityZ = 0;

float MForceX = 0; //Acceleration
float MForceY = 0;
float MForceZ = 0;

float MPositionX = 0; //New positions
float MPositionY = 0;
float MPositionZ = 0;

// Called every frame
void AMoon::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );

FVector EPosition = FVector(0.0, 0.0, 0.0);
FVector MPosition = GetActorLocation();

DistanceX = (MPosition.X - EPosition.X) / 100; //I divide by 100 to convert cm into m
DistanceY = (MPosition.Y - EPosition.Y) / 100;
DistanceZ = (MPosition.Z - EPosition.Z) / 100;

MForceX = G * MMass * EMass / (DistanceX * DistanceX); //G being the constant 0.0006673
MForceY = G * MMass * EMass / (DistanceY * DistanceY); //I multiply the mass of earth with the mass of the moon and the gravitational constant then divide by the distance2
MForceZ = G * MMass * EMass / (DistanceZ * DistanceZ);

VelX = MVelocityX + MForceX * DeltaTime; //I get the new velocity by adding the old with the acceleration times dt
VelY = MVelocityY + MForceY * DeltaTime;
VelZ = MVelocityZ + MForceZ * DeltaTime;

MPositionX = MPosition.X + (MVelocityX + VelX) * 0.5 * DeltaTime; //I then get the new position by multiplying dt with 0.5 then add the 2 velocities and multiply that result then finally add the old position
MPositionY = MPosition.Y + (MVelocityY + VelY) * 0.5 * DeltaTime;
MPositionZ = MPosition.Z + (MVelocityZ + VelZ) * 0.5 * DeltaTime;

MVelocityX = VelX; //Then I assign the new value to the old velocity
MVelocityY = VelY;
MVelocityZ = VelZ;

FVector NewMPosition = FVector(MPositionX, MPositionY, MPositionZ);

SetActorLocation(NewMPosition); //Set the new location

}
``````

If anyone of you has any idea what I’m doing wrong, I would bre very grateful to hear your reply.