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 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
int main (int argc,char *argv[])
{
bool continuer = true;
SDL_Event evenement;//pour la gestion des evenements
//initialisation de ODE
world = dWorldCreate();
space = dHashSpaceCreate(0);
dWorldSetGravity (world,0,0,-9.81);
dWorldSetERP(world,(dReal)0.8);
dWorldSetCFM(world,(dReal)1E-5);
///
//initialisation des objets ODE
dBodyID bodyCube = dBodyCreate(world);//création de l'objet
dMass matricePoid;
dMassSetBoxTotal(&matricePoid, (dReal)10 , (dReal)2 ,(dReal)2,(dReal)2);//initialisation de la matrice
dBodySetMass(bodyCube ,&matricePoid);//application de la matrice poid
dBodySetPosition(bodyCube,5,2,3);
dGeomID geomCube = dCreateBox (space,(dReal)2,(dReal)2,(dReal)2);//création de l'objet pour les collisions
dGeomSetBody (geomCube,bodyCube);//liaison de l'objet physique et de collision
dCreatePlane ( space , 0 , 0 , 1 , 0);
dCreatePlane ( space , 1 , 0 , 1 , -50);
dCreatePlane ( space , -1 , 0 , 1 , -50);
dCreatePlane ( space , 0 , 1 , 1 , -50);
dCreatePlane ( space , 0 , -1 , 1 , -50);
std::vector<dBodyID> cubes;//Cube à dessiner
std::vector<dGeomID> cubesGeom;//Cube à dessiner
int nombre_de_cube = 0;
///
//initialisation de la SDL
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("OpenGL with ODE",NULL);
SDL_SetVideoMode(L, H, 32, SDL_OPENGL);
SDL_ShowCursor(0); // on cache le cursor
///
//inistialisation de OpenGL
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluPerspective(70,(double)L/H,1,1000);
glEnable(GL_DEPTH_TEST);
///
//Gestion du temps
Uint32 last_time = SDL_GetTicks();
Uint32 current_time,ellapsed_time;
Uint32 start_time;
///
while (continuer)
{
start_time = SDL_GetTicks();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // mise a jour d'opengl
SDL_PollEvent(&evenement);
if(evenement.type == SDL_KEYDOWN && evenement.key.keysym.sym == SDLK_F9)
continuer = false;
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
if(evenement.type == SDL_KEYDOWN && evenement.key.keysym.sym == SDLK_LEFT)
dBodyAddForce (bodyCube,0,10,0);
if(evenement.type == SDL_KEYDOWN && evenement.key.keysym.sym == SDLK_UP)
dBodyAddForce (bodyCube,10,0,0);
if(evenement.type == SDL_KEYDOWN && evenement.key.keysym.sym == SDLK_RIGHT)
dBodyAddForce (bodyCube,0,-10,0);
if(evenement.type == SDL_KEYDOWN && evenement.key.keysym.sym == SDLK_DOWN)
dBodyAddForce (bodyCube,-10,0,0);
if(evenement.type == SDL_KEYDOWN && evenement.key.keysym.sym == SDLK_SPACE)
dBodyAddForce (bodyCube,0,0,10);
if(nombre_de_cube < 500 && evenement.type == SDL_MOUSEBUTTONDOWN && evenement.button.button == SDL_BUTTON_LEFT)
{
//ajoute un cube
cubes.push_back( dBodyCreate(world));//création de l'objet
dBodySetMass(cubes[nombre_de_cube] ,&matricePoid);//application de la matrice poid
dBodySetPosition(cubes[nombre_de_cube],0,nombre_de_cube%6 * 2,((int)nombre_de_cube/6)*2);
cubesGeom.push_back(dCreateBox (space,(dReal)2,(dReal)2,(dReal)2));
dGeomSetBody (cubesGeom[nombre_de_cube],cubes[nombre_de_cube]);//liaison de l'objet physique et de collision
++nombre_de_cube;
}
if(nombre_de_cube > 0 && evenement.type == SDL_MOUSEBUTTONDOWN && evenement.button.button == SDL_BUTTON_RIGHT)
{
//suprime le dernier cube
--nombre_de_cube;
dGeomDestroy(cubesGeom[nombre_de_cube]);
dBodyDestroy(cubes[nombre_de_cube]);
cubesGeom.pop_back();
cubes.pop_back();
}
if( evenement.type == SDL_KEYDOWN && evenement.key.keysym.sym == SDLK_F1)
{
//Explosion
int i = 0;
while ( i < nombre_de_cube)
{
dReal d = sqrt(dBodyGetPosition(cubes[i])[0] * dBodyGetPosition(cubes[i])[0] + dBodyGetPosition(cubes[i])[1] * dBodyGetPosition(cubes[i])[1] + dBodyGetPosition(cubes[i])[2] * dBodyGetPosition(cubes[i])[2]);
dReal a = 40000*(1.0-0.2*d*d);
if (a<0)
a=0;
dBodySetForce(cubes[i], dBodyGetPosition(cubes[i])[0]*a ,dBodyGetPosition(cubes[i])[1]*a ,dBodyGetPosition(cubes[i])[2]*a);
++i;
}
}
if( evenement.type == SDL_KEYDOWN && evenement.key.keysym.sym == SDLK_F2)
{
//implosion
int i = 0;
while ( i < nombre_de_cube)
{
dReal x,y,z;
x = dBodyGetPosition(cubes[i])[0]*-100;
y = dBodyGetPosition(cubes[i])[1]*-100;
z =dBodyGetPosition(cubes[i])[2]*-100;;
dBodySetForce(cubes[i],x,y,z );
++i;
}
}
//caméra
gluLookAt( 0,50,50,//possition
(float)dBodyGetPosition(bodyCube)[0],(float)dBodyGetPosition(bodyCube)[1],(float)dBodyGetPosition(bodyCube)[2],//cible
0,0,1);//vecteur vertical
dSpaceCollide(space,contactgroup,&nearCallback); // teste les collisions entre les objets
dWorldQuickStep(world, 0.1f);//lancement de la simulation
dJointGroupEmpty (contactgroup);
dessinePlan();
dessineCube(bodyCube);
int i = 0;
while ( i < nombre_de_cube)
dessineCube(cubes[i++]);
glFlush();
SDL_GL_SwapBuffers();
ellapsed_time = SDL_GetTicks() - start_time;
if (ellapsed_time < 10)
SDL_Delay(10 - ellapsed_time);
}
//on détruit les éléments de ODE
dSpaceDestroy (space);
dWorldDestroy (world);
dCloseODE(); // on ferme ODE
///
return EXIT_SUCCESS;
} |
Partager