Bonjour, je réalise en ce moment en C++ une simulation de Gravité en 2D, pour le moment je me concentre sur l'axe X.
PS : je ne suis pas très bon en programmation

Voici mon problème : mon code est orienté objet, quand je lance le programme, tous ce passe bien jusqu'à que les points se rencontrent en coordonnée 0.0. A ce stade là, il va réafficher ce qui c'est afficher depuis le debut et va le refaire tout en changeant les distance ?aléatoirement?

Voici le code (j'utilise SDL2 OpenGL et math.h)

le header
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once
#include <stdlib.h>
#include <iostream>
#include <SDL2/SDL.h>
#include <string>
#include <GL\glew.h>
 
class graphic
{
public:
    graphic();
    ~graphic();
 
    int InitSDL();
    int InitOpenGl();
    int Create_Window(const char *titre, int widht, int height);
    void DrawPoint(float points[]);
    void Quit_SDL();
 
    void CalculePosition(float points[], float masse1, float masse2);
    float CalculeDistance(float points[]);
    float CalculeForce(float points[], float masse1, float masse2);
 
private:
    SDL_Window* window;
    SDL_GLContext contexteOpenGL;
    GLenum initialisationGLEW;
    GLuint vertexbuffer;
 
    float m_DistX;
    float m_G;
    float m_ForceX;
    float m_accX1;
};
La définition de la classe
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "graphic.h"
#include <string>
 
using namespace std;
 
graphic::graphic()
{
    window = 0;
    contexteOpenGL = 0;
    initialisationGLEW = 0;
    m_G = 0.001;
    m_accX1 = 0;
}
 
graphic::~graphic()
{
}
 
int graphic::InitSDL()
{
    //Init SDL
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        cout << "Erreur d'initialisation de la SDL : " << SDL_GetError() << endl;
        Quit_SDL();
        return -1;
    }
 
    //OpenGL version
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
 
    //DoubleBuffer
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
 
    return 0;
}
 
int graphic::InitOpenGl()
{
    //Init OpenGL
    //OpenGL context
    contexteOpenGL = SDL_GL_CreateContext(window);
    if (contexteOpenGL == 0)
    {
        cout << SDL_GetError() << endl;
        Quit_SDL();
        return -1;
    }
 
    //Init Glew
    initialisationGLEW = glewInit();
    if (initialisationGLEW != GLEW_OK)
    {
        cout << "Erreur d'initialisation de GLEW : " << glewGetErrorString(initialisationGLEW) << endl;
        SDL_Delay(2000);
        Quit_SDL();
        return EXIT_FAILURE;
    }
}
 
int graphic::Create_Window(const char *titre, int widht, int height)
{
    window = SDL_CreateWindow(titre, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, widht, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
    return 0;
}
 
void graphic::Quit_SDL()
{
    SDL_GL_DeleteContext(contexteOpenGL);
    SDL_DestroyWindow(window);
    SDL_Quit();
}
 
void graphic::DrawPoint(float points[])
{
    glClear(GL_COLOR_BUFFER_BIT);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, points);
    glEnableVertexAttribArray(0);
    glDrawArrays(GL_POINTS, 0, 2);
    glDisableVertexAttribArray(0);
    SDL_GL_SwapWindow(window);
}
 
void graphic::CalculePosition(float points[], float masse1, float masse2)
{     
    cout << m_ForceX << endl;
    //Points X
    //Points 0
    if (points[0] < 0)
    {
        points[0] += m_accX1;
    }
    else if (points[0] > 0)
    {
        points[0] -= m_accX1;
    }
    else
    {
        points[0] = 0;
    }
 
    //Points 2
    if (points[2] < 0)
    {
        points[2] += m_accX1;
    }
    else if (points[2] > 0)
    {
        points[2] -= m_accX1;
    }
    else
    {
        points[2] = 0;
    }
 
    CalculeDistance(points);
    CalculeForce(points, masse1, masse2);
    m_accX1 = m_ForceX / masse1;
    DrawPoint(points);
}
float graphic::CalculeForce(float points[], float masse1, float masse2)
{
    //Calcule Force
    m_ForceX = m_G*((masse1 * masse2) / pow(m_DistX, 2.0));
    return m_ForceX;
}
 
float graphic::CalculeDistance(float points[])
{
    //Calcule Distance X
    if (points[0] < points[2])
    {
        m_DistX = fdim(points[2], points[0]);
    }
    else if (points[2] < points[0])
    {
        m_DistX = fdim(points[0], points[2]);
    }
    else if (points[0] == points[2])
    {
        m_DistX = 0;
    }
    return m_DistX;
}
Et le main
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "graphic.h"
#include "GravitySimulation.h"
#include <iostream>
#include <SDL2\SDL.h>
#include <GL\glew.h>
 
using namespace std;
 
int main(int argc, char *argv[])
{
    float points[] = { -0.5, 0.0,**** 0.5, 0.0 };
    int masse1(2), masse2(2);
    bool etat(true);
 
    graphic window;
 
    window.InitSDL();
    window.Create_Window("GamEngine", 800, 600);
    window.InitOpenGl();
 
    while (etat)
    {
        window.CalculePosition(points, masse1, masse2);
        SDL_Delay(10);
    }
    window.Quit_SDL();
    return 0;
}
Voilà, ça fait environ 2 jours que j'essai de régler ce problème sans résultats, merci d'avance pour votre aide