0

目前我正在使用 JOGL 进行球检测程序,有人告诉我,一旦球彼此靠近,它们就会消失。

    //this is the method from the main class
    public void display(GLAutoDrawable drawable) {
    GL gl = drawable.getGL();

    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    gl.glColor3f(1.0f, 1.0f, 1.0f);
    glut.glutWireCube(2.0f * limit);
    for (int i = 0; i < ball.length; ++i)
    {
    ball[i].display(gl);        
    }
    for (int i = 0; i < ball.length; ++i)
    {
    ball[i].moveRandomly();
    }
    //this is the method from the auxiliary class 
    for (int i = 0; i < ball.length; ++i)
    {
      for (int j = 0; j < ball.length; ++j)
      {
        if (ball[i].distanceFrom(ball[j]) <= 10)
        {

        }
      }
    }
}

  void display(GL gl) {

    gl.glLoadIdentity();
    gl.glTranslatef(position[0], position[1], position[2]);
    gl.glColor3fv(colour, 0);
    glut.glutSolidSphere(radius, 10, 10);
    //glut.glutSolidTeapot(radius);
}

我尝试这样做无济于事,球一下子消失了,我还尝试以相同的结果减小半径,任何指向正确方向的点都将不胜感激。

4

2 回答 2

2

它们全部消失的原因是每个球都在与自己进行比较。

在语句之前的内部循环中添加它if(这是一个快速修复):

if (i == j) continue;
于 2009-10-30T17:00:54.997 回答
0

目前我的问题多于帮助。

首先,你有多少个球?

这条线困扰着我:

if(ballGone == false)
{
    glut.glutSolidSphere(radius, 10, 10);
}

如果 ballGone 为假,则不显示球,但这意味着只有一个球,因此当它设置为假时,不会显示球。

根据这里:http ://www.cs.umd.edu/~meesh/kmconroy/JOGLTutorial/我的担心应该是有道理的:

Display 与 java.awt.Component.paint() 非常相似,因为每次画布需要重新绘制/重新绘制/重新显示时都会调用它

因此,您可能想查看如何重绘并确保将绘制每个未将状态设置为 false 的对象。

于 2009-10-30T16:49:50.463 回答