Published on

Universe Studio Dev Log 1

Authors
  • avatar
    Name
    Luiz Henrique Amaral Soares
    Twitter

The Physics Theory of the software

Universe Studio uses classical mechanics, which explains why your very simple implementation and performance compare to General Relativity.

The Newton analysis of orbital movement

The following derivation applies to such an elliptical orbit. We start only with the Newtonian law of gravitation, which states that the gravitational acceleration towards the central body is related to the inverse of the square of the distance between them, namely

F=GMmR2\vec{F} = \frac{GMm}{R^2}

Where F is the force acting on the mass m caused by the gravitational attraction mass M has for m, G is the universal gravitational constant, and r is the distance between the two masses centers.

From Newton's Second Law, the summation of the forces acting on m related to that body's acceleration:

F=mA\vec{F} = m*A

Where A is the acceleration of m caused by the force of gravitation attraction F of M acting on m

Combining the First and Second Equations:

GMmr2=mA-\frac{GMm}{r^2} = m\vec{A}

Solving for the acceleration, A:

A=Fm=1mGMmr2=µr2\vec{A} = \frac{F}{m} = -\frac{1}{m}\frac{GMm}{r^2} = -\frac{µ}{r^2}

Starting the Implementation

In this project, I will use OpenGL, C++ language, GLFW and GLM.

First is need set the planets properties

The velocity and the Mass

struct PlanetConfig {
  ncp::Vec3 pos, vel;
  double mass;
}

To calculate the gravity forces on the N-body, the code is more complex.

void ncp::Sim::computeGravityForces(const EntityVec& objects) {
  for (size_t j = i + 1; j < nObjects - 1; i++) {
    for(sie_t j = i + 1; j < nObjects; j++) {
      const ncp::Vec3 p1 = objects[i]->cTransform->pos;
      const ncp::Vec3 p2 = objects[j]->cTransform->pos;
      const double d = r.mag();

      const double m1 = objects[i]->cGravity->mass;
      const double m2 = objects[j]->cGravity->mass;

      constexpr double minr = 0.5;
      constexpr double minr3 = minr * minr * minr;

      const ncp::Vec3 a = r * (0.5 * m1 * m2 / std::max(d * d * d, minr3))};

      objects[i]->cTransform->vel += a * dt;
      objects[j]->cTransform->vel += a * -dt;
    }
  }
}

Shader compilation problem

One of my big problems that I can solve is the problem with the Fragment Shader compilation.

A Fragment Shader is the stage that will process a Fragment generated by the Rasterization into a set of colors and a single depth value.

The Code Who Loads the Shader:

GLuint Shader::compileShader(const char* shaderPath, GLenum shaderType) const {
  std::string shaderCode;
  std::ifstream shaderFile;

  shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);

  try {
    shaderFile.open(shaderPath);
    std::stringstream shaderStream;
    shaderStream << shaderFile.rdbuf();
    shaderFile.close();
    shaderCode = shaderStream.str();
  } catch (const std::exception& e) {
    std::cerr << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << e.what()
              << '\n';
  }

  const char* shaderCode_c = shaderCode.c_str();
  unsigned int shader;

  shader = glCreateShader(shaderType);
  glShaderSource(shader, 1, &shaderCode_c, NULL);
  glCompileShader(shader);
  if (shaderType == GL_VERTEX_SHADER)
    checkCompilerErrors(shader, "VERTEX");
  else
    checkCompilerErrors(shader, "FRAGMENT");
  return shader;
}

The Solution of my problem is the OpenGL compiler glslc who Don't I have installed in my system

Planned Next Feature

The next feature who I planned to implement is the GUI for user interaction I intend to use the ImGUI library because it is lightweight and has excellent performance.