Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a cube fall smoothly down the Y-Axis in Open Gl, C++
    text
    copied!<p>I am trying to create a very simple physics simulator for a school project, all i want it to do is have a cube that falls dew to gravity and when it hits a floor it will bounce and so on until the cube has no energy and it will just stop moving e.g. rest on the floor. I haven't added the collision detection in yet but most other things work fine the only problem i have is that the cube doesn't fall smoothly its very jumpy e.g. it falls and speed up then slows down and then speeds up again and i have no idea why.</p> <p>I have incluided the code below:</p> <pre><code>timestep++; velo += 0.005; cout &lt;&lt; velo &lt;&lt; "\n"; glTranslatef(0.0, -velo, 0.0);//timestep * gravity, 0.0); </code></pre> <p>I have also included the entire program code just incase it is a probem somewhere else the extract above is just at the top of the display function</p> <pre><code>#include &lt;GLTools.h&gt; #include &lt;GLShaderManager.h&gt; #include &lt;GLFrustum.h&gt; #include &lt;GLBatch.h&gt; #include &lt;GLFrame.h&gt; #include &lt;GLMatrixStack.h&gt; #include &lt;GLGeometryTransform.h&gt; #include &lt;StopWatch.h&gt; #include &lt;math.h&gt; #include &lt;stdio.h&gt; #ifdef __APPLE__ #include &lt;glut/glut.h&gt; #else #define FREEGLUT_STATIC #include &lt;GL/glut.h&gt; #endif #include &lt;iostream&gt;; using namespace std; void display(); void specialKeys(int, int, int); void animate(); double rotate_y = 0; double rotate_x = 0; // Gravity Varibles int timestep = 0; float gravity = 0.0098; float velo = 0.0f; int main( int argc, char* argv[] ) { glutInit(&amp;argc, argv); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL ); glutCreateWindow("DANIELS CUBE"); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glutDisplayFunc(display); glutSpecialFunc(specialKeys); glutIdleFunc(animate); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glutMainLoop(); return 0; } void animate() { glutPostRedisplay(); } void display() { //Clears the window glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Changes the way the polygons are drawn so it looks like a wire frame glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); /////////// // CUBE /// /////////// // Resets the transformation matrix glLoadIdentity(); glScalef(0.2, 0.2, 0.2); // Rotates the cuube around the x by 'rotate_x' glRotatef( rotate_x, 1.0, 0.0, 0.0 ); // Rotates the cuube around the y by 'rotate_y' glRotatef( rotate_y, 0.0, 1.0, 0.0 ); // move dew to gravity timestep++; velo += 0.005; cout &lt;&lt; velo &lt;&lt; "\n"; glTranslatef(0.0, -velo, 0.0);//timestep * gravity, 0.0); // Defines the folowing verticys as this polygon glBegin(GL_POLYGON); //Changes color glColor3f( 1.0, 0.0, 0.5 ); // Adds verted to polygon glVertex3f( -0.5, -0.5, -0.5 ); // F1 glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( -0.5, 0.5, -0.5 ); // F2 glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( 0.5, 0.5, -0.5 ); // F3 glColor3f( 1.0, 0.0, 1.0 ); glVertex3f( 0.5, -0.5, -0.5 ); // F4 // Closes the polygon glEnd(); glBegin(GL_POLYGON); glColor3f( 1.0, 1.0, 1.0 ); glVertex3f( 0.5, -0.5, -0.5 ); // Back1 glVertex3f( 0.5, 0.5, -0.5 ); // Back2 glVertex3f( 0.5, 0.5, 0.5 ); // Back3 glVertex3f( 0.5, -0.5, 0.5 ); // Back4 glEnd(); glBegin(GL_POLYGON); glColor3f( 1.0, 0.0, 1.0 ); glVertex3f( 0.5, -0.5, -0.5 ); // F1 glVertex3f( 0.5, 0.5, -0.5 ); // F2 glVertex3f( 0.5, 0.5, 0.5 ); // F3 glVertex3f( 0.5, -0.5, 0.5 ); // F4 glEnd(); glBegin(GL_POLYGON); glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( -0.5, -0.5, 0.5 ); // F1 glVertex3f( -0.5, 0.5, 0.5 ); // F2 glVertex3f( -0.5, 0.5, -0.5 ); // F3 glVertex3f( -0.5, -0.5, -0.5 ); // F4 glEnd(); glBegin(GL_POLYGON); glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( 0.5, 0.5, 0.5 ); // F1 glVertex3f( 0.5, 0.5, -0.5 ); // F2 glVertex3f( -0.5, 0.5, -0.5 ); // F3 glVertex3f( -0.5, 0.5, 0.5 ); // F4 glEnd(); glBegin(GL_POLYGON); glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.5, -0.5, -0.5 ); // F1 glVertex3f( 0.5, -0.5, 0.5 ); // F2 glVertex3f( -0.5, -0.5, 0.5 ); // F3 glVertex3f( -0.5, 0.5, -0.5 ); // F4 glEnd(); //////////// // Floor // ////////// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glLoadIdentity(); // Rotates the cuube around the x by 'rotate_x' glRotatef( rotate_x, 1.0, 0.0, 0.0 ); // Rotates the cuube around the y by 'rotate_y' glRotatef( rotate_y, 0.0, 1.0, 0.0 ); glColor3f(1.0, 1.0, 1.0); glBegin(GL_LINES); for( GLfloat i = -2.5; i &lt; 2.5; i += 0.25 ) { glVertex3f(i, -1.0, 2.5); glVertex3f(i, -1.0, -2.5); glVertex3f(2.5, -1.0, i); glVertex3f(-2.5, -1.0, i); } glEnd(); // Flushes the buffers glFlush(); // Draws what has just been done on the screen 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(); } </code></pre>
 

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