PDA

View Full Version : Bouncing Ball/Point animation using openGL



ChucklesDarling
08-19-2014, 07:21 AM
Hi, Im new to opengl and only know a little bit of c++, so I was wondering how would I go about creating a simple animation of a ball/point etc. bouncing up and down in a window. I already have the window created, so its just the actual movement of the point im stuck on.
Thanks in advance!

ChucklesDarling
08-19-2014, 09:17 AM
This is the code I have so far, not sure why the point isn't moving?

// includes
#include "stdafx.h"
#include <stdio.h>
#include <math.h>

int xpos = 0;
int xvel = 1;


// renderScene() - render the scene

void renderScene()
{
// clear the back buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// render code here...

glPointSize(10);
glBegin(GL_POINTS);


glColor3f( 1.0f, 1.0f, 1.0f ); //white
glVertex2f(0.0, 0.0);

xpos = xpos + xvel;

if(xpos >= 500)
{
xvel = -xvel;
}
if( xpos <= -500)
{
xvel = -xvel;
}

glEnd();

// swap the buffers of the current window
glutSwapBuffers();

}

carsten neumann
08-19-2014, 09:33 AM
Please use
around source code to preserve formatting, thanks!

You are drawing the point at a fixed position (0, 0) instead of at the location you compute.

ChucklesDarling
08-19-2014, 02:17 PM
Oh okay, will do!

I've now got movement, thanks! but it is really quick as if there's a few points, so is there a way to reduce the speed?

Sorry, these are probably really basic things!

carsten neumann
08-19-2014, 02:33 PM
If you want to do it 'properly', your animation should be made independent of the rendering speed. Right now every time you render an image the object is moved by a fixed amount, instead move the object by a distance that depends on how much time has expired since the last frame.
The other option is to just reduce the amount you move the object each frame, i.e. make xvel smaller (make it a floating point value, so it can have values between 0 and 1).

ChucklesDarling
08-19-2014, 03:06 PM
I gave the other option ago because I'm not sure how to do the first part, but as soon as I change to a floating point number, the point becomes stationary.

carsten neumann
08-20-2014, 02:08 AM
Did you change the type of xpos as well? Otherwise you add a float (that probably is < 1) to an integer and store as integer, so that looses all the decimal digits.

ChucklesDarling
08-20-2014, 05:41 AM
That was exactly the problem. Thank you! Slowed down to the perfect speed now. I just need to implement motion blur to the animation now and that'll be my first animation using opengl and c!
I've read up on the accumulation buffer, but is it a hard job to get motion blur? I have't seen any examples that I understand fully.

carsten neumann
08-20-2014, 07:01 AM
Hmm, never used the accumulation buffer and I don't think it gets much use and love these days. There should be examples on the net that show how it can be used for motion blur.
Examples how motion blur can be done without accumulation buffer, see for example NVidia motionblur sample (http://docs.nvidia.com/gameworks/index.html#gameworkslibrary/graphicssamples/opengl_samples/motionblursample.htm). However, note that it makes use of shaders and other technology that is used in 'modern' OpenGL, so you may have to get familiar with those things first. Then again you may want to do that anyway ;)

ChucklesDarling
08-20-2014, 07:12 AM
Haha no worries! Thank you for your help! :D