我必须制作一个在两个球体之间进行碰撞的程序。我做了这个但是当球体碰撞时,一切都被堵住了。我不能再移动球体了。我只使用sphere1移动而另一个是静态的。代码用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 and the 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 is where I verify if collide
initRaza();
double xac1,yac1,zac1,xac2,yac2,zac2;//the new centers after the 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 the centers
//the sum of the radiuses
suma_raze=sf[0].raza+sf[1].raza;
dist_ramasa=distanta-sf[0].raza-sf[1].raza;
// we compare the distance and the sum of radiuses
//if the distance is lower than the 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 up
{
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);
}
我不知道什么不起作用。当他们碰撞时,我想互相拒绝。我希望你能帮助我。
答案 0 :(得分:1)
一旦球体碰撞,你的函数conditie
将返回1.所有按键功能都会在修改球体位置之前检查这一点,因此,一旦它们发生碰撞,你就不能再移动球体了。