检测3D OpenGL图形中的碰撞的最佳方法?

时间:2017-01-08 16:59:43

标签: c++ opengl collision-detection game-physics

我正在OpenGL中实现一个3D游戏,但我仍然坚持碰撞检测,游戏中包含多个摆锤和一个射出钟摆的大炮:

ShootOut Pendulum

我尝试通过在两个对象之间取距离来实现碰撞检测:

dx=ax-bx    
dy=ay-by    
dz=az-bz
distance = sqrt(dx*dx + dy*dy + dz*dz);

其中ax,ay,az是摆点和bx,by,bz是炮弹点。

距离< = A_radius + B_radius 用于碰撞检测检查距离小于确定发生碰撞检测。

但不幸的是,这种逻辑不起作用。

使用gluSpheregluCylinder制作Pendulum,此处为此代码

 glPushMatrix();
    glTranslated(0,0,40);
    drawCanon();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(600, 0, -410);
    glRotatef(rorate, 0, 0, 1);
    drawBob();
    glPopMatrix();

    glPushMatrix();

    glTranslatef(bx, by, bz);
    glRotatef(rorate, 0, 0, 1);
    drawBob();
    glPopMatrix();
    //GLUquadricObj *ctrees;


    for (int i = 0; i < allPendulum.size(); i++)
    {

        glPushMatrix();
        glTranslatef(400, 50, -400);
        allPendulum[i].drawRope();
        glPopMatrix();

        glPushMatrix();
        glTranslatef(400, 50, -400);
        allPendulum[i].drawPend();
        glPopMatrix();

    }

drawCanon代码在这里

void drawCanon()
{

    glPushMatrix();
    glTranslatef(600, 0, -380);
    glRotatef(rorate, 0, 0, 1);
    drawBob();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(600, 0, -410);
    glRotatef(rorate, 0, 0, 1);
    drawBob();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(300, 0, -40);
    //glRotatef(rorate, 0, 0, 0);
    drawWheelAttacher();
    glPopMatrix();

    //canon fire with bomb
    glPushMatrix();
    glTranslatef(605, 3, -390);
    glRotatef(longFire, xRotate, yRotate, zRotate);

    //glTranslatef(0, 0, -25);
    drawFireHolder();
    //printf_s("Long fire is %d", longFire);

    glPopMatrix();

    glPushMatrix();

    glTranslatef(605, 3, -390);
    glRotatef(longFire, xRotate, yRotate, zRotate);
    glTranslatef(0, 0, bombX);
    glTranslatef(0, 0, 25);

    drawBomb();
    glPopMatrix();  
}

完整代码(如果有人想要的话)

#include <iostream>
#include <fstream>
#include <math.h>
#include <glut.h>
#include <glut.h>
#include "Turtle.h"
#include "cube.h"
#include"pixMap.h"
#include "pendulum.h"
#include "vector"

void timer(int);

void drawRope(void);
void drawBob(void);
void drawLongPipe(void);
void initializePendulum(void);
void addPendulumInVector(void);
void drawCanon(void);
void drawCar(void);
void drawWheel(void);
void drawWheelAttacher(void);
void drawFireHolder(void);
void drawBomb(void);
void mouseControl(int x,int y);
void mouseClick(int button, int state, int x, int y);
void specialKeyHandler(int key, int x, int y);
void handlerMove();
RGBpixmap pix[5];
int zaxis = -300;
int xaxis=0, yaxis;
int rorate = 0;
bool check = false;
int carX = 0, carY, carZ;
int bombX=0;

void key(unsigned char key, int x, int y);
void drawFlorr();
float L = 100;
int longFire = 200;
bool fire = false;


const int screenWidth = 1000;      // width of screen window in pixels 
const int screenHeight = 1000;     // height of screen window in pixels
float ww = 800;
float wh = 800;
float f = 520, n = 10.0; 
static GLdouble ort1[] = { -200, 200, -33, 140 };
//static GLdouble viewer[] = { 525, 25, -180 };
static GLdouble viewer[] = { 729, 25, -334 };
// 729, 25, -334 ,525, 25 ,-350
static GLdouble up[] = { 0, 1, 0 };

static GLdouble objec[] = { 525, 25, -350 };
//
//static GLdouble objec[] = { 605.0, 0, -300 };
float x, y = 0.0, z, z1;
float xmax = screenWidth - 200.0;
float zmax = screenWidth - 200.0;
float xmin, zmin;
float step = 10.0;
float bx = bombX, by = 0, bz = 450;
float dx, dy, dz;
float fov = 39;  // previous 80

using std::cout;
using std::fstream;
using std::ios;
#define PI 3.1415926535898
pendulum Pendulum,PedulumTwo,PendulumThree;
std::vector<pendulum> allPendulum;
float camAngle = 10;
int xRotate = 0, yRotate = 0, zRotate = 1;


 void myInit(void)
 {
        //glClearColor(0.0,0.0,0.0,0.0);       // background color is white

    //glPointSize(2.0);               // a 'dot' is 2 by 2 pixels
    //glMatrixMode(GL_PROJECTION);     
    //glLoadIdentity();
     gluOrtho2D(0.0, screenWidth, 0.0, screenHeight);//dino window
    //gluOrtho2D(1.0, 1.0, 1.0,1.0);//house window
    //gluOrtho2D(1.0, 1.0, 1.0, 1.0);//bird window
     glViewport(0, 0, screenWidth, screenHeight);

}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    //glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); 
    gluLookAt(viewer[0], viewer[1], viewer[2], objec[0], objec[1], objec[2], 0, 1, 0);


    glMatrixMode(GL_PROJECTION);
    //glOrtho(-1, 1, -1, 1, -1, 100);
    glLoadIdentity();
    gluPerspective(fov, 1.333, n, f); 
    //gluPerspective(90, 1, 1.333, 1000);
    //gluPerspective(50, screenWidth / screenHeight, 0.000001, 2000);
    glPointSize(2.0);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glTranslatef(viewer[0], viewer[1], viewer[2]); // Translation to the camera center
    glRotatef(-camAngle * 57.2957795, 0, 1, 0); // Rotate to correspond to the camera
    glTranslatef(0.016, 0, -0.05); // Offset to draw the object

    //glutWireCone(0.005, 0.9, 20, 20);
    glPopMatrix();
    //cube.drawFace(10, 20, 10, 22);
    drawFlorr();

    glPushMatrix();
    //glTranslated(0, 0, 40);
    drawPipe();
    glPopMatrix();

    glPushMatrix();
    glTranslated(0,0,40);
    drawCanon();
    glPopMatrix();

    /*

    glPushMatrix();
    glTranslatef(600, 0, -410);
    glRotatef(rorate, 0, 0, 1);
    drawBob();
    glPopMatrix();

    glPushMatrix();

    glTranslatef(bx, by, bz);
    glRotatef(rorate, 0, 0, 1);
    drawBob();
    glPopMatrix();
    */

//  drawWheelAttacher();
    //Line.turn(90);
    /*

    */
    //glLineWidth(10.0);

    //Turtle Line(Point2(600, 70, -300), -90);
    //glColor3f(1, 0, 0);
    //Line.forward(1, 1);
    //Line.turnTo(-45);
    //Line.forward(100, 1);
    //drawRope();
    //GLUquadricObj *ctrees;

    for (int i = 0; i < allPendulum.size(); i++)
    {       
        glPushMatrix();
        glTranslatef(400, 50, -400);
        allPendulum[i].drawRope();
        glPopMatrix();

        glPushMatrix();
        glTranslatef(400, 50, -400);
        allPendulum[i].drawPend();
        glPopMatrix();  
    } 

    //drawEveryThing();
    glutSwapBuffers();   
}

int main(int argc, char** argv)
{   
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // set display mode
    glutInitWindowSize(screenWidth, screenHeight); // set window size
    glutInitWindowPosition(10, 10); // set window position on screen

    glEnable(GL_DEPTH_TEST);
    glutCreateWindow("Dino Line Drawing"); // open the screen window
    glutDisplayFunc(myDisplay);     // register redraw function
    myInit();       
    glEnable(GL_TEXTURE_2D);

    pix[0].readBMPFile("grass.bmp");
    pix[0].setTexture(2001);

    pix[1].readBMPFile("wood.bmp");
    pix[1].setTexture(2002);
    pix[2].readBMPFile("ROPE.bmp");
    pix[2].setTexture(2003);
    pix[3].readBMPFile("bob.bmp");
    pix[3].setTexture(2004);
    glutKeyboardFunc(key);

    initializePendulum();
    addPendulumInVector();
    glutTimerFunc(1,timer,1);
    glutMouseFunc(mouseClick);
    glutPassiveMotionFunc(mouseControl);
    glutSpecialFunc(specialKeyHandler);
    glutMainLoop();              // go into a perpetual loop
    return 1;
}
void timer(int t)
{
    //CarX + cos(someangle)*(distance from car)

    if (fire == true)
    {           
        bx = 0;
        bz = 415-bombX;
        dx = 400 - 605-bombX;
        dy = 50 - 3;
        dz = 400 +bz;

        float distance = sqrt(dx*dx + dy*dy + dz*dz);

        printf("DISTANCE IS %f",distance);
    //  distance=floor(distance);
        if (distance <=12)
        {
            printf("Collision Occur");
        }
        if (distance <= (12))// radius is the radus of our bounding sphere for each object
        {
            printf("Collision Occur");
        }

        bombX++;
    }
    else
    {
        rorate++;
    }

    for (int i = 0; i < allPendulum.size(); i++)
    {
        allPendulum[i].changeDirection();
    }
    glutTimerFunc(10, timer, t);
    zaxis++;

    glutPostRedisplay();
}
void drawHook()
{ 
    //glColor3f(1.0f, 0.0f, 0.0f);         // drawing color is black

    glPointSize(5.0);       
}
void drawFlorr()
{
    glEnable(GL_TEXTURE_2D);
    glColor3ub(255,255,255);
    xmin = -100;
    zmin = -100;

//  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

    for (x = xmin; x < xmax; x += step)
    {
        for (z = zmin; z < zmax; z += step)
        {
            z1 = -z;
            glBindTexture(GL_TEXTURE_2D, 2001);
            glBegin(GL_QUADS);
            glTexCoord2f(0, 0);
            glVertex3f(x, y, z1);
            glTexCoord2f(1, 0);
            glVertex3f(x, y, z1-step);
            glTexCoord2f(0, 1);
            glVertex3f(x + step, y, z1 - step);
            glTexCoord2f(1, 1);
            glVertex3f(x+step, y, z1);

            glEnd();  
        }
    }
    glDisable(GL_TEXTURE_2D);
}
void key(unsigned char key, int x, int y)
{
    if (key == 't' || key == 'T')
    {
        xRotate = 1, yRotate = 0, zRotate = 0;
        handlerMove();
    }
    if (key == 'r' || key == 'R')
    {
        xRotate = 0, yRotate = 1, zRotate = 0;
        handlerMove();
    }
    if (key == 'x' && up[0] == 0)
    {
        viewer[0] -= 1.0;       
    }
    if (key == 'X' && up[0] == 0)
    {
        viewer[0] += 1.0;
    }
    if (key == 'y' && up[0] == 0)
    {
        viewer[1] -= 1.0;
    }
    if (key == 'Y' && up[0] == 0)
    {
        viewer[1] += 1.0;
    }
    if (key == 'z' && up[0] == 0)
    {
        viewer[2] += 1.0;
    }
    if (key == 'Z' && up[0] == 0)
    {
        viewer[2] -= 1.0;
    }
    // zomm changing
    if (key == 'v')
    {
        fov+=1.0;
        if (fov > 180)
        {
            fov = 180.0;
        }  
    }
    if (key == 'V')
    {
        fov -= 1.0;
        if (fov < 0)
        {
            fov = 0;
        }
    }
    //change near clipping
    if (key == 'f')
    {
        f -= 1.0;
    }
    if (key == 'F')
    {
        f += 1.0;
    }
    if (key == 'l')
    {
        carX++;
    }
    if (key == 'k')
    {
        carX--;
    }
    if (key == 'o')
    {
        carY++;
    }
    if (key == 'm')
    {
        carY--;
    }
    glutPostRedisplay();
}
void drawEveryThing()
{   
    drawFlorr();
    drawLine();
    glPushMatrix();
    //glTranslatef(0, 0, -500);
    drawHouse();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(300.0, 0, -500);
    drawMountain(96, 150);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(380.0, 0, -500);
    drawMountain(90, 100);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(400.0, 0, -300);
    drawTrees(3,20);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(525.0, 0, -550);
    drawTrees(6, 25);
    glPopMatrix();
}
void drawPipe()
{
    int circle_points;
    float size, angle;
    int i;

    //House Main Vertices
    GLfloat housevert[] = { 400.0, 50.0, -500.0,  //0
        400.0, 60.0, -500.0, //1
        415.0, 25.0, -500.0, //2
        410.0, 60, -500.0, //3
        410.0, 50.0, -500.0,  //4
        400.0, 50.0, -300.0,  //5
        400.0, 60, -300.0, //6
        415.0, 25.0, -300.0, //7
        410.0, 60, -300.0, //8

        410.0, 50.0, -300.0 };  //9

    glEnable(GL_TEXTURE_2D);
    glLineWidth(2.0);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, housevert);

    //glColor3f(1.0, 0.0, 0.0);
    glBindTexture(GL_TEXTURE_2D, 2002);
    // Front Wall
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glArrayElement(0);
    glTexCoord2f(1, 0);
    glArrayElement(1);
    glTexCoord2f(0, 1);
    glArrayElement(3);
    glTexCoord2f(1, 1);
    glArrayElement(4);
    glEnd();

    glBindTexture(GL_TEXTURE_2D, 2002);
    //Right Wall
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glArrayElement(4);
    glTexCoord2f(1, 0);
    glArrayElement(3);
    glTexCoord2f(0, 1);
    glArrayElement(8);
    glTexCoord2f(1, 1);
    glArrayElement(9);
    glEnd();

    glBindTexture(GL_TEXTURE_2D, 2002);
    //Left Wall
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glArrayElement(0);
    glTexCoord2f(0, 1);
    glArrayElement(1);
    glTexCoord2f(1, 0);
    glArrayElement(6);
    glTexCoord2f(1, 1);
    glArrayElement(5);
    glEnd();

    glBindTexture(GL_TEXTURE_2D, 2002);
    //Back Wall
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glArrayElement(5);
    glTexCoord2f(0, 1);
    glArrayElement(6);
    glTexCoord2f(1, 0);
    glArrayElement(8);
    glTexCoord2f(1, 1);
    glArrayElement(9);
    glEnd();
    glDisable(GL_TEXTURE_2D);
}
void drawRope()
{
    GLUquadricObj *mount;
    //glMatrixMode(GL_TEXTURE);
    //glLoadIdentity();
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 2003);
    mount = gluNewQuadric();
    gluQuadricDrawStyle(mount, GL_QUADS);
    gluQuadricTexture(mount, true);
    glPushMatrix();

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    //glColor3f(0.0, 0.1, 0.3);

    glColor3f(0.0, 1.0, 0.0);
    //glTranslatef(-100, 20, 0);
    glRotatef(rorate, 1, 0, 0);
    gluCylinder(mount, 1, 1, 100, 2, 1);
    glDisable(GL_TEXTURE_2D);

    glTranslatef(0, 0, 100);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 2004);
    gluQuadricTexture(mount, true);
    gluSphere(mount, 1 * 6, 10, 10);
    glDisable(GL_TEXTURE_2D);

    //gluQuadricTexture(mount, true);
    glPopMatrix();  
}
void drawBob()
{
    GLUquadricObj *trees;

    trees = gluNewQuadric();
    gluQuadricDrawStyle(trees, GLU_LINE);
    glPushMatrix();
    glRotatef(0,0,0,0);
    glColor3f(0.0, 1.0, 0.0);
    gluSphere(trees, 1 * 10, 20, 2);
    glPopMatrix(); 
}
void initializePendulum()
{
    Pendulum.Amplitude = 25;
    PendulumThree.Amplitude = 133;
    PedulumTwo.Amplitude = 100;
}
void addPendulumInVector()
{
    allPendulum.push_back(Pendulum);
    allPendulum.push_back(PedulumTwo);
    allPendulum.push_back(PendulumThree);
}
void drawWheel()
{
    glPushMatrix();
    glTranslatef(600, 0, -380);
    glRotatef(rorate, 1, 0, 0);
    drawBob();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(600, 0, -350);
    glRotatef(rorate, 1, 0, 0);
    drawBob();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(630, 0, -380);
    glRotatef(rorate, 1, 0, 0);
    drawBob();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(630, 0, -350);
    glRotatef(rorate, 1, 0, 0);
    drawBob();
    glPopMatrix();  
}
void drawCanon()
{

    glPushMatrix();
    glTranslatef(600, 0, -380);
    glRotatef(rorate, 0, 0, 1);
    drawBob();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(600, 0, -410);
    glRotatef(rorate, 0, 0, 1);
    drawBob();
    glPopMatrix();

    glPushMatrix();
    glTranslatef(300, 0, -40);
    //glRotatef(rorate, 0, 0, 0);
    drawWheelAttacher();
    glPopMatrix();

    //canon fire with bomb
    glPushMatrix();
    glTranslatef(605, 3, -390);
    glRotatef(longFire, xRotate, yRotate, zRotate);

    //glTranslatef(0, 0, -25);
    drawFireHolder();
    //printf_s("Long fire is %d", longFire);

    glPopMatrix();

    glPushMatrix();

    glTranslatef(605, 3, -390);
    glRotatef(longFire, xRotate, yRotate, zRotate);
    glTranslatef(0, 0, bombX);
    glTranslatef(0, 0, 25);

    drawBomb();
    glPopMatrix();  
}
void drawWheelAttacher()
{
    glPushMatrix();
    glTranslatef(300, 0, -380);
    //glRotatef(0, 0, 0, 1);
    drawLongPipe();
    glPopMatrix();    
}
void drawLongPipe()
{
GLUquadricObj *trees;

trees = gluNewQuadric();
gluQuadricDrawStyle(trees, GLU_LINE);
glPushMatrix();
glRotatef(0, 0, 0, 0);
glColor3f(0.0, 1.0, 0.0);
gluCylinder(trees, 1, 1, 50, 5, 5);
glPopMatrix();
}
void drawFireHolder()
{
    GLUquadricObj *trees;

    trees = gluNewQuadric();
    gluQuadricDrawStyle(trees, GL_EYE_LINEAR);
    glPushMatrix();
    glRotatef(0, 0, 0, 1);
    glColor3f(0.0, 1.0, 0.0);
    gluCylinder(trees, 1, 1, 20, 5, 5);
    glPopMatrix();
}
void drawBomb()
{
    GLUquadricObj *mount;
    //glMatrixMode(GL_TEXTURE);
    //glLoadIdentity();
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 2003);
    mount = gluNewQuadric();
    gluQuadricDrawStyle(mount, GL_QUADS);
    gluQuadricTexture(mount, true);
    glPushMatrix();

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glDisable(GL_TEXTURE_2D);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 2004);
    gluQuadricTexture(mount, true);
    gluSphere(mount, 1 * 6, 10, 10);
    glDisable(GL_TEXTURE_2D);

    //gluQuadricTexture(mount, true);
    glPopMatrix();   
}
void mouseClick(int button, int state, int x, int y)
{
    fire = true;
}
void mouseControl(int x, int y)
{

//  printf_s("mouse x is %d", x);
    if (x > 280)
    {
        check = true;
    }
    else if (x == 150)
    {
        check = false;
    }   
}
void handlerMove()
{
    if (check == true && fire == true)
    {

    }
    else if (check == true)
    {
        longFire--;
    }
    else
    {
        longFire++;
    }
    if (longFire > 280)
    {
        check = true;
    }
    else if (longFire == 150)
    {
        check = false;
    }
}

Pendulum Class就在这里

#pragma once
#include <glut.h>
class pendulum
{
public:
    int Amplitude = 0;
    bool isCheck = false;
    int radius = 6;
    void drawRope()
    {
        GLUquadricObj *mount;
        //glMatrixMode(GL_TEXTURE);
        //glLoadIdentity();
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, 2003);
        mount = gluNewQuadric();
        gluQuadricDrawStyle(mount, GL_QUADS);
        gluQuadricTexture(mount, true);
        glPushMatrix();

        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

        //glColor3f(0.0, 0.1, 0.3);

        glColor3f(0.0, 1.0, 0.0);
        //glTranslatef(-100, 20, 0);
        glRotatef(Amplitude, 1, 0, 0);
        gluCylinder(mount, 1, 1, 50, 2, 1);
        glDisable(GL_TEXTURE_2D);

        //gluQuadricTexture(mount, true);
        glPopMatrix();   
    }
    void changeDirection()
    {
        if (isCheck)
        {
            Amplitude--;
        }
        else
        {
            Amplitude++;
        }
        if (Amplitude == 150)
        {
            isCheck = true;
        }
        else if (Amplitude < 25)
        {
            isCheck = false;
            Amplitude = 25;
        }
    }
    void drawPend()
    {
        GLUquadricObj *mount;
        //glMatrixMode(GL_TEXTURE);
        //glLoadIdentity();
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, 2003);
        mount = gluNewQuadric();
        gluQuadricDrawStyle(mount, GL_QUADS);
        gluQuadricTexture(mount, true);

        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

        //glColor3f(0.0, 0.1, 0.3);    
        //gluQuadricTexture(mount, true);

        glPushMatrix();
        glRotatef(Amplitude, 1, 0, 0);
        glTranslatef(0, 0, 50);
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, 2004);
        gluQuadricTexture(mount, true);

        gluSphere(mount, 1 * radius, 10, 10);
        glDisable(GL_TEXTURE_2D);

        glPopMatrix();      
    }
};

1 个答案:

答案 0 :(得分:0)

只有当两个对象是球体时,你的逻辑才有效。 你需要计算球体中心和穿过圆柱体上下圆心的线之间的距离;然后将该距离与球体半径和圆柱半径之和进行比较。