作为我的项目的第一步,我想用2D物理学来创造一个世界。由于我对OpenGL有一定的了解,因此我尝试在项目中一起使用这两个。
在变得复杂之前,我只是想让一个方盒掉在地上。
问题是:
我有3个文件(main.cpp physique.cpp和affichage.cpp)和2个标头(physique.h affichage.h)。为了能够在不同文件中操纵我的身体,我制作了一个全局变量,该变量是指向我的身体的指针(该指针称为body)。
我在Bigque的physique.cpp中创建自己的世界,身体和地面。我将身体的位置声明为(1; 4)。但是每次我叫cout<<body->GetPosition().x<<endl
时,我在第1帧得到0 ...
这是physique.cpp
#include "physique.h"
extern int com;
extern unsigned int fps;
float bodyWidth=0.5f;
float bodyHeight=0.5f;
float groundWidth=10.0f;
float groundHeight=0.5f;
b2Body* body;
b2Body* groundBody;
b2World* ptrWorld;
void BigBang(){
//DEFINITION WORLD
b2Vec2 gravity(0.0f, 0.0f);
b2World world(gravity);
ptrWorld = &world;
//DEFINITION GROUND
b2BodyDef groundBodyDef;
groundBodyDef.type = b2_staticBody;
groundBodyDef.position.Set(0.0f,-groundHeight);
groundBody = world.CreateBody(&groundBodyDef); //renvoie un pointeur
b2PolygonShape groundBox;
groundBox.SetAsBox(groundWidth, groundHeight); //2*groundWith units wide and 2*groundHeight units tall;
groundBody->CreateFixture(&groundBox, 0.0f);
//DEFINITION CUBE
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(1.0f, 4.0f);
bodyDef.angle = 0;
body = world.CreateBody(&bodyDef); //renvoie un pointeur
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(bodyWidth, bodyHeight);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 0.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
}
void PhysicEngine(){
float32 timeStep = fps;
int32 velocityIterations = 6;
int32 positionIterations = 2;
ptrWorld->Step(timeStep, velocityIterations, positionIterations);
}
affichage.cpp:
//INCLUDES
#include "affichage.h"
//VARIABLES
GLfloat deplacement;
extern int previousTime;
extern int currentTime;
extern int elapsedTime;
extern unsigned int fps;
extern int com;
extern float groundHeight;
extern float groundWidth;
extern float bodyWidth;
extern float bodyHeight;
extern b2Body* body;
extern b2Body* groundBody;
extern b2World* ptrWorld;
int nTotFrames=0;
GLvoid affichage(){
glClear(GL_COLOR_BUFFER_BIT); // Effacement du frame buffer
glBegin(GL_QUADS); //sol
glColor3f(1.0f, 0.5f, 0.5f);
glVertex2f(groundBody->GetLocalCenter().x-groundWidth, groundBody->GetLocalCenter().y+groundHeight);
glColor3f(1.0f, 0.5f, 0.5f);
glVertex2f(groundBody->GetLocalCenter().x-groundWidth, groundBody->GetLocalCenter().y-groundHeight);
glColor3f(1.0f, 0.5f, 0.5f);
glVertex2f(groundBody->GetLocalCenter().x+groundWidth, groundBody->GetLocalCenter().y-groundHeight);
glColor3f(1.0f, 0.5f, 0.5f);
glVertex2f(groundBody->GetLocalCenter().x+groundWidth, groundBody->GetLocalCenter().y+groundHeight);
glEnd();
glFlush();
glBegin(GL_QUADS); //BOX
glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(body->GetLocalCenter().x-bodyWidth, body->GetLocalCenter().y+bodyHeight);
glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(body->GetLocalCenter().x-bodyWidth, body->GetLocalCenter().y-bodyHeight);
glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(body->GetLocalCenter().x+bodyWidth, body->GetLocalCenter().y-bodyHeight);
glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(body->GetLocalCenter().x+bodyWidth, body->GetLocalCenter().y+bodyHeight);
glEnd();
glFlush();
glLoadIdentity();
gluOrtho2D(-2.0, 4.0, -1.0, 3.0);
glutSwapBuffers();
}
GLvoid update(int fps){
glutTimerFunc(fps, update, 100/6); // setups the timer to be called again
PhysicEngine();
previousTime = currentTime; // Get the time when the previous frame was rendered
currentTime = glutGet(GLUT_ELAPSED_TIME); // Get the current time (in milliseconds) and calculate the elapsed time
elapsedTime = currentTime - previousTime;
deplacement = 0.01*elapsedTime;
glutPostRedisplay();
nTotFrames++;
}
GLvoid clavier(unsigned char touche, int x, int y) {
// ESCAPE ou 'q' : fermera l'application
switch(touche) {
case 'i':
break;
case 'q' : // quitter
case 27 :
cout<<nTotFrames;
exit(0);
break;
}
// Demande a GLUT de reafficher la scene
glutPostRedisplay();
}
GLvoid clavierUp(unsigned char key, int x, int y){}
这是main.cpp
//MAINFILE
#ifdef __APPLE__ //OpenGL
#include <OpenGL/gl.h>
#include <GLUT/GLUT.h>
#else
#include <GL/gl.h>
#include <GL/glut.h>
#endif
#include <iostream>
#include <math.h>
#include "affichage.h"
#include "physique.h"
using namespace std;
int previousTime;
int currentTime;
int elapsedTime;
extern b2Body* body;
unsigned int fps = 60;
int com=0; //correspond au numéro de la jambe qui se lève
int main (int argc, char *argv[]){
float w = 1000;
float h = 600;
glutInit(&argc, argv); // Initialisation de GLUT
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); // Choix du mode d'affichage (ici RVB)
glutInitWindowPosition(0,0); // Position initiale de la fenetre GLUT
glutInitWindowSize(w,h); // Taille initiale de la fenetre GLUT
glutCreateWindow("DaddyLongLeg - Algo Genetique"); // Creation de la fenetre GLUT
glClearColor(0.1f, 0.1f, 0.1f, 0.0f); // Définition de la couleur d'effacement du framebuffer //le fond est noir
// Définition des fonctions de callbacks
glutDisplayFunc(affichage);
glutKeyboardFunc(clavier);
glutKeyboardUpFunc(clavierUp);
glutReshapeFunc(reshape);
glutTimerFunc(1000/fps, update, 0);
currentTime = glutGet(GLUT_ELAPSED_TIME);
BigBang();
glutMainLoop(); // Lancement de la boucle infinie GLUT
return 0;
}