PDA

View Full Version : Decelerate movement


enigma
05-18-2007, 02:53 PM
Hi! :)
I' ve a problem with the control of movements. I work with OpenGL and GLUT. In my application, i have a sphere and i move this on X axis with glTranslate() function. The function have these parameters:
glTranslatef(xRef,0.0,-8.0)
xRef is set up to 0.0f, and is increased of 0.1f when a key is pressed. All this in a keyboard control function (called by glutSpecialFunc()).
When i press a key, the sphere moves on x axis and he stop immediately.
I want obtaining an "decelerate" effect. When i press a key i want start the sphere movement... but with a decelerate stop effect and not with an stop immediate...
Sorry for my bad english :(
Thanks!

Reedbeta
05-18-2007, 04:42 PM
Instead of letting xRef change immediately when the user presses a key, set up some target variable like xTarget that changes on a keypress. Then let xRef move gradually toward xTarget over time, using a quadratic or cubic function for interpolation. For instance, 3x^2 - 2x^3 gives a good accelerating/decelerating effect.

enigma
05-20-2007, 04:54 AM
Hi :)
Thanks for response!
In a specialkeys() function, when a key is pressed i increase xTarget variable of 1.0, or decrease...
In animate() function (called by glutIdleFunc()) i must increase xRef toward xTarget by using 3x^2 - 2*x^3 function. But how i can tie x with the clock of the system? For clock i use glutGet(GLUT_ELAPSED_TIME)...
Thanks :)
Bye!

Reedbeta
05-20-2007, 09:47 AM
x ranges from 0 to 1, as does the output of that function, so you'll need to scale and bias the time and the output value to match and do the interpolation over the time interval you want.

enigma
05-22-2007, 11:44 AM
Ok, thanks!!! ;)