c++ - Two spheres collision -
i have make program doing collision between 2 spheres. made when spheres collide blocked. can't move sphere anymore. made sphere1 move , other static. code written in vb/ c++.
#include "glos.h" #include <math.h> #include <gl/gl.h> #include <gl/glu.h> #include <glaux.h> glfloat max1=0,max2=0,v,v1; float d,distanta=0; int i,j; void myinit(void); void callback display(void); void callback myreshape(glsizei w, glsizei h); void callback mutastanga(void); void callback mutadreapta(void); int k=0,k1=0; int dist_ramasa; static float dx1=200,dy1=300,dz1=0; int deplasare=100; float rez; static int flag=1; float pxc,pyc,pzc,sum,suma_raze; void myinit (void) { //iluminating glclearcolor(1.0, 1.0, 1.0, 1.0); glfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; glfloat mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 }; glfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; glfloat mat_shininess[] = { 100.0 }; glfloat light_ambient[] = { 0.4, 0.4, 0.4, 1.0 }; glfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; glfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; glfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 }; glfloat lmodel_ambient[] = { 0.5, 0.5, 0.5, 1.0 }; glmaterialfv(gl_front, gl_ambient, mat_ambient); glmaterialfv(gl_front, gl_diffuse, mat_diffuse); glmaterialfv(gl_front, gl_specular, mat_specular); glmaterialfv(gl_front, gl_shininess, mat_shininess); gllightfv(gl_light0, gl_ambient, light_ambient); gllightfv(gl_light0, gl_diffuse, light_diffuse); gllightfv(gl_light0, gl_specular, light_specular); gllightfv(gl_light0, gl_position, light_position); gllightmodelfv(gl_light_model_ambient, lmodel_ambient); glenable(gl_lighting); // activare iluminare glenable(gl_light0); // activare sursa 0 glcolormaterial(gl_front,gl_diffuse); glenable(gl_color_material); gldepthfunc(gl_less); glenable(gl_depth_test); } struct sfera //the spheres { glfloat raza, xcentru, ycentru, zcentru; //the radius , centers glfloat xd1,xd2,yd1,yd2,zd1,zd2; }sf[2]; void initraza(){ //radius init sf[0].raza=100; sf[1].raza=100; } int conditie(void){ //this verify if collide initraza(); double xac1,yac1,zac1,xac2,yac2,zac2;//the new centers after movement xac1=sf[0].xcentru+dx1; yac1=sf[0].ycentru+dy1; zac1=sf[0].zcentru+dz1; //static sphere xac2=sf[1].xcentru+700; yac2=sf[1].ycentru+300; zac2=sf[1].zcentru; pxc = pow((xac1-xac2),2); pyc = pow((yac1-yac2),2); pzc = pow((zac1-zac2),2); sum=(pxc + pyc + pzc); distanta=sqrt(sum); //the distance between centers //the sum of radiuses suma_raze=sf[0].raza+sf[1].raza; dist_ramasa=distanta-sf[0].raza-sf[1].raza; // compare distance , sum of radiuses //if distance lower sum -> collide if(distanta>suma_raze) return 1; else return 0; } void callback mutastanga(void) //movement left { if(conditie()==1){ if(dist_ramasa<deplasare) dx1=dx1-dist_ramasa; else dx1=dx1-deplasare; } } void callback mutadreapta(void) //movement right { if(conditie()==1){ if(dist_ramasa<deplasare) dx1=dx1+dist_ramasa; else dx1=dx1+deplasare; } } void callback mutasus(void) //movement { if(conditie()==1){ if(dist_ramasa<deplasare) dy1=dy1+dist_ramasa; else dy1=dy1+deplasare; } } void callback mutajos(void) //movement down { if(conditie()==1){ if(dist_ramasa<deplasare) dy1=dy1-dist_ramasa; else dy1=dy1-deplasare; } } void drawball1() //the first sphere { glpushmatrix(); glcolor3f(0,1,0); gltranslatef(dx1, dy1, 0.0); glrotatef(30,1,0,0); auxsolidsphere(sf[0].raza); glpopmatrix(); } void drawball2() //the second sphere { glpushmatrix(); glcolor3f(1,0,0); gltranslatef(700,300,0); glrotatef(30,1,0,0); auxwiresphere(sf[1].raza); glpopmatrix(); } void callback display (void) { initraza(); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glloadidentity (); drawball1(); drawball2(); auxswapbuffers(); } void callback myreshape(glsizei w, glsizei h) { if (!h) return; glviewport(0, 0, w, h); glmatrixmode(gl_projection); glloadidentity(); if (w <= h) glortho (0, 800.0, 0*(glfloat)h/(glfloat)w, -860.0*(glfloat)h/(glfloat)w, -200.0, 200.0); else glortho (0*(glfloat)w/(glfloat)h, 900.0*(glfloat)w/(glfloat)h, 0, 900.0, -500.0, 500.0); glmatrixmode(gl_modelview); } int main(int argc, char** argv) { auxinitdisplaymode (aux_single | aux_rgb | aux_depth16); auxinitposition (0, 0, 900, 700); auxinitwindow ("bounding sphere collision"); myinit (); auxkeyfunc (aux_left, mutastanga); auxkeyfunc (aux_right, mutadreapta); auxkeyfunc (aux_up, mutasus); auxkeyfunc(aux_down,mutajos); auxreshapefunc (myreshape); auxmainloop(display); return(0); }
i don't know not working. when collide want reject each other . hope can me.
once spheres collide, function conditie
return 1. keypress functions check before modifying sphere's position, thus, once collide, can no longer move sphere.
Comments
Post a Comment