title: C++ SDL2 circular motion
body: <p>I am currently making a simple solar system orbit program. So far I have managed to display the planets and sun in the window however I have yet to figure out how to get the time based circular motion needed for the planets. The motion is just a simple circle no in depth physics. My code below will show the class for the earth (no other planets at the moment) and how the time function works and the original straight motion I also found online to see how motion could work in c++. I have seen many different equations online but I am unsure on how to implement them into the code.</p>
  
  <p>Earth Header:</p>
  
  <pre><code>#ifndef Earth_H_
  #define Earth_H_
  
  #include "Entity.h"
  #include &lt;math.h&gt;
  
  // Define a Sun speed in pixels per second
  //const double Degree= 180/ M_PI;
  
  class Earth : public Entity {
  
      const float Earth_Speed = 100;
  public:
      Earth(SDL_Renderer* renderer);
      ~Earth();
  
      void Update(float delta);
      void Render(float delta);
  
      void SetDirection(float dirx, float diry);
      const double Degree = 180 / M_PI;
      /*float Orbital_Speed=0.01;*/
      double Angle;
  
      /*float Speed= 900;*/
      double Radius=100;
  
      float dirx, diry;
  
  private:
      SDL_Texture* texture;
  
  };
  
      #endif  
  </code></pre>
  
  <p>Earth cpp:</p>
  
  <pre><code>#include "Earth.h"
  
  
  Earth::Earth(SDL_Renderer* renderer) : Entity(renderer) {
      SDL_Surface* surface = IMG_Load("Earth.png");
      texture = SDL_CreateTextureFromSurface(renderer, surface);
      SDL_FreeSurface(surface);
  
      x = 0;
      y = 0;
      width = 20;
      height = 20;
  
  
  }
  
  Earth::~Earth() {
      // Clean resources
      SDL_DestroyTexture(texture);
  }
  
  void Earth::Update(float delta) {
      // Move the Sun, time based
      x +=  dirx  * delta;
      y += diry * delta; 
  
      //Angle = atan2(diry / Degree, dirx / Degree);
      //x += ((dirx*Degree) - cos(Angle)*Radius)* delta;
      //y += ((diry*Degree) - sin(Angle)*Radius)* delta;
  }
  
  void Earth::Render(float delta) {
      SDL_Rect rect;
      rect.x = (int)(x + 0.5f); // Round the float to the nearest integer
      rect.y = (int)(y + 0.5f); // Round the float to the nearest integer
      rect.w = width;
      rect.h = height;
      SDL_RenderCopy(renderer, texture, 0, &amp;rect);
  }
  
  void Earth::SetDirection(float dirx, float diry) {
  
  
      //Angle = atan2(diry/ Degree, dirx/ Degree);
  
      //this-&gt;dirx = ((dirx*Degree) - cos(Angle)*Radius)*Earth_Speed;
      //this-&gt;diry = ((diry*Degree) - sin(Angle)*Radius)*Earth_Speed;
      float length = sqrt(dirx * dirx + diry * diry);
      this-&gt;dirx = Earth_Speed * (dirx / length);
      this-&gt;diry = Earth_Speed * (diry / length);
  
   }
  </code></pre>
  
  <p>Simulation Header:</p>
  
  <pre><code>#ifndef Simulation_H_
  #define Simulation_H_
  
  #include "SDL.h"
  #include "SDL_image.h"
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  
  
  #include "Sun.h"
  #include "Mercury.h"
  #include "Venus.h"
  #include "Earth.h"
  #include "Mars.h"
  #include "Jupiter.h"
  #include "Saturn.h"
  #include "Uranus.h"
  #include "Neptune.h"
  
  #define FPS_DELAY 500
  
  
  class Simulation {
  public:
      Simulation();
      ~Simulation();
  
      bool Init();
      void Run();
  
  private:
      SDL_Window* window;
      SDL_Renderer* renderer;
  
      // Timing
      unsigned int lasttick, fpstick, fps, framecount;
  
      float width= 650, height= 650;
  
  
  
      Sun* sun;
      Mercury* mercury;
      Venus* venus;
      Earth* earth;
      Mars* mars;
      Jupiter* jupiter;
      Saturn* saturn;
      Uranus* uranus;
      Neptune* neptune;
  
      void Clean();
      void Update(float delta);
      void Render(float delta);
  
      void NewSimulation();
  
  
  };
  
  #endif
  </code></pre>
  
  <p>Simulation cpp:</p>
  
  <pre><code>#include "Simulation.h"
  
  Simulation::Simulation() {
      window = 0;
      renderer = 0;
  }
  
  Simulation::~Simulation() {
  
  }
  
  bool Simulation::Init() {
      // Initialize SDL and the video subsystem
      SDL_Init(SDL_INIT_VIDEO);
  
      // Create window
      window = SDL_CreateWindow("Suraj's Solar System Simulator",
          100, 100,
          width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
      if (!window) {
          // Error creating window
          return false;
      }
  
      // Create renderer
      renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
      if (!renderer) {
          // Error creating renderer
          return false;
      }
  
      // Initialize timing
      lasttick = SDL_GetTicks();
  
  
      return true;
  }
  
  void Simulation::Clean() {
      // Clean renderer and window
      SDL_DestroyRenderer(renderer);
      SDL_DestroyWindow(window);
  }
  
  void Simulation::Run() {
      // Create Sun and Planets
  
      sun = new Sun(renderer);
      mercury = new Mercury(renderer);
      venus= new Venus(renderer);
      earth = new Earth(renderer);
      mars= new Mars(renderer);
      jupiter = new Jupiter(renderer);
      saturn = new Saturn(renderer);
      uranus = new Uranus(renderer);
      neptune = new Neptune(renderer);
  
  
      NewSimulation();
  
      // Main loop
      while (1) {
          // Handler events
          SDL_Event e;
          if (SDL_PollEvent(&amp;e)) {
              if (e.type == SDL_QUIT) {
                  break;
              }
          }
  
          // Calculate delta and fps
          unsigned int curtick = SDL_GetTicks();
          float delta = (curtick - lasttick) / 1000.0f;
          lasttick = curtick;
  
          // Update and render the Simulation
          Update(delta);
          Render(delta);
      }
  
      // Delete objects
  
      delete sun;
      delete mercury;
      delete venus;
      delete earth;
      delete mars;
      delete jupiter;
      delete saturn;
      delete uranus;
      delete neptune;
  
      Clean();
  
      SDL_Quit();
  }
  
  void Simulation::NewSimulation() {
  
  
      // Center the Sun horizontally and vertically
      sun-&gt;x = width / 2 - sun-&gt;width / 2;
      sun-&gt;y = height / 2 - sun-&gt;height / 2;
  
      mercury-&gt;x = width / 2.4 - mercury-&gt;width / 2.4;
      mercury-&gt;y = height / 2 - mercury-&gt;height / 2;
  
      venus-&gt;x = width / 2.7 - venus-&gt;width / 2.7;
      venus-&gt;y = height / 2 - venus-&gt;height / 2;
  
      earth-&gt;x = width / 3 - earth-&gt;width / 3;
      earth-&gt;y = height / 2 - earth-&gt;height / 2;
  
      mars-&gt;x = width / 3.4 - mars-&gt;width / 3.4;
      mars-&gt;y = height / 2 - mars-&gt;height / 2;
  
      jupiter-&gt;x = width / 5 - jupiter-&gt;width / 5;
      jupiter-&gt;y = height / 2 - jupiter-&gt;height / 2;
  
      saturn-&gt;x = width / 9- saturn-&gt;width / 9;
      saturn-&gt;y = height / 2 - saturn-&gt;height / 2;
  
      uranus-&gt;x = width / 15 - uranus-&gt;width / 15;
      uranus-&gt;y = height / 2 - uranus-&gt;height / 2;
  
      neptune-&gt;x = width / 50 - neptune-&gt;width / 50;
      neptune-&gt;y = height / 2 - neptune-&gt;height / 2;
  
      //// Set the initial direction of the planets
      /*mercury-&gt;SetDirection(1, -1);
      venus-&gt;SetDirection(1, -1);*/
  
      earth-&gt;SetDirection((earth-&gt;x - sun-&gt;x)*cos(M_PI/180)+ sun-&gt;x, - ((earth-&gt;x - sun-&gt;x)*sin(M_PI / 180) + sun-&gt;y));
      //mars-&gt;SetDirection(1, -1  );
      //jupiter-&gt;SetDirection(1, -1);
      //saturn-&gt;SetDirection(1, -1);
      //uranus-&gt;SetDirection(1, -1);
      //neptune-&gt;SetDirection(1, -1);
  
  }
  
  void Simulation::Update(float delta) {
  
  
      // Update Earth and Sun
      sun-&gt;Update(delta);
      mercury-&gt;Update(delta);
      venus-&gt;Update(delta);
      earth-&gt;Update(delta);
      mars-&gt;Update(delta);
      jupiter-&gt;Update(delta);
      saturn-&gt;Update(delta);
      uranus-&gt;Update(delta);
      neptune-&gt;Update(delta);
  }
  
  
  void Simulation::Render(float delta) {
      // Clear the screen to black
      SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
      SDL_RenderClear(renderer);
  
      // Render board and Sun
  
      sun-&gt;Render(delta);
      mercury-&gt;Render(delta);
      venus-&gt;Render(delta);
      earth-&gt;Render(delta);
      mars-&gt;Render(delta);
      jupiter-&gt;Render(delta);
      saturn-&gt;Render(delta);
      uranus-&gt;Render(delta);
      neptune-&gt;Render(delta);
  
      // Update the screen
      SDL_RenderPresent(renderer);
  }
  </code></pre>
  
  <p>Entity Header:</p>
  
  <pre><code>#ifndef ENTITY_H_
  #define ENTITY_H_
  
  #include "SDL.h"
  #include "SDL_image.h"
  
  class Entity {
  public:
      Entity(SDL_Renderer* renderer);
      virtual ~Entity();
  
      float x, y, width, height;
  
      virtual void Update(float delta);
      virtual void Render(float delta);
  
  
  protected:
      SDL_Renderer* renderer;
  
  };
  
  #endif
  </code></pre>
  
  <p>Enitity cpp:</p>
  
  <pre><code>#include "Entity.h"
  
  Entity::Entity(SDL_Renderer* renderer) {
      this-&gt;renderer = renderer;
  
      x = 0;
      y = 0;
      width = 1;
      height = 1;
  }
  
  Entity::~Entity() {
  }
  
  void Entity::Update(float delta) {
  }
  
  void Entity::Render(float delta) {
  }
  </code></pre>
  
  <p>Main Header:</p>
  
  <pre><code>#ifndef MAIN_H_
  #define MAIN_H_
  
  #include "Simulation.h"
  
  int main(int argc, char* argv[]);
  
  #endif
  </code></pre>
  
  <p>Main cpp:</p>
  
  <pre><code>#include "main.h"
  
  int main(int argc, char* argv[]) {
      // Create the game object
      Simulation* simulation = new Simulation();
  
      // Initialize and run the game
      if (simulation-&gt;Init()) {
          simulation-&gt;Run();
      }
  
      // Clean up
      delete simulation;
      return 0;
  }
  </code></pre>