Note that there are some explanatory texts on larger screens.

plurals
  1. POcreating a 3d cube and bouncing balls
    primarykey
    data
    text
    <p>I'm writing a program in OpenGL, I'm drawing a simple cube 3D and I want to add bouncing balls so that they bounce inside the walls of the cube, I tried to add a ball class and the following code:</p> <pre><code> GLfloat WHITE[] = {1, 1, 1}; GLfloat RED[] = {1, 0, 0}; GLfloat GREEN[] = {0, 1, 0}; GLfloat MAGENTA[] = {0, 0, 1}; class Ball { double radius; GLfloat* color; double maximumHeight; double x; double y; double z; int direction; public: Ball(double r, GLfloat* c, double h, double x, double z): radius(r), color(c), maximumHeight(h), direction(-1), y(h), x(x), z(z) { } void update() { y += direction * 0.05; if (y &gt; maximumHeight) { y = maximumHeight; direction = -1; } else if (y &lt; radius) { y = radius; direction = 1; } glPushMatrix(); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); glTranslated(x, y, z); glutSolidSphere(radius, 30, 30); glPopMatrix(); } }; Ball balls[] = { Ball(0.5, GREEN, 7, 6, 1), Ball(0.5, MAGENTA, 6, 3, 4), Ball(0.5, WHITE, 5, 1, 7) }; </code></pre> <p>and in the display function I added this:</p> <pre><code> for (int i = 0; i &lt; sizeof balls / sizeof(Ball); i++) { balls[i].update(); } </code></pre> <p>This is my cube code:</p> <pre><code>void display(); void specialKeys(); double rotate_y=0; double rotate_x=0; void display(){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef( rotate_x, 1.0, 0.0, 0.0 ); glRotatef( rotate_y, 0.0, 1.0, 0.0 ); glBegin(GL_POLYGON); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.5, -0.5, -0.5 ); // P1 is red glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( 0.5, 0.5, -0.5 ); // P2 is green glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( -0.5, 0.5, -0.5 ); // P3 is blue glColor3f( 1.0, 0.0, 1.0 ); glVertex3f( -0.5, -0.5, -0.5 ); // P4 is purple glEnd(); glBegin(GL_POLYGON); glColor3f( 1.0, 1.0, 1.0 ); glVertex3f( 0.5, -0.5, 0.5 ); glVertex3f( 0.5, 0.5, 0.5 ); glVertex3f( -0.5, 0.5, 0.5 ); glVertex3f( -0.5, -0.5, 0.5 ); glEnd(); glBegin(GL_POLYGON); glColor3f( 1.0, 0.0, 1.0 ); glVertex3f( 0.5, -0.5, -0.5 ); glVertex3f( 0.5, 0.5, -0.5 ); glVertex3f( 0.5, 0.5, 0.5 ); glVertex3f( 0.5, -0.5, 0.5 ); glEnd(); glBegin(GL_POLYGON); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( -0.5, -0.5, 0.5 ); glVertex3f( -0.5, 0.5, 0.5 ); glVertex3f( -0.5, 0.5, -0.5 ); glVertex3f( -0.5, -0.5, -0.5 ); glEnd(); glBegin(GL_POLYGON); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( 0.5, 0.5, 0.5 ); glVertex3f( 0.5, 0.5, -0.5 ); glVertex3f( -0.5, 0.5, -0.5 ); glVertex3f( -0.5, 0.5, 0.5 ); glEnd();*/ glBegin(GL_POLYGON); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.5, -0.5, -0.5 ); glVertex3f( 0.5, -0.5, 0.5 ); glVertex3f( -0.5, -0.5, 0.5 ); glVertex3f( -0.5, -0.5, -0.5 ); glEnd(); glFlush(); glutSwapBuffers(); } void specialKeys( int key, int x, int y ) { if (key == GLUT_KEY_RIGHT) rotate_y += 5; else if (key == GLUT_KEY_LEFT) rotate_y -= 5; else if (key == GLUT_KEY_UP) rotate_x += 5; else if (key == GLUT_KEY_DOWN) rotate_x -= 5; glutPostRedisplay(); } int main(int argc, char* argv[]){ glutInit(&amp;argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Awesome Cube"); glEnable(GL_DEPTH_TEST); glutDisplayFunc(display); glutSpecialFunc(specialKeys); glutMainLoop(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload