Jump to content


Decelerate movement


4 replies to this topic

#1 enigma

    Member

  • Members
  • PipPip
  • 58 posts

Posted 18 May 2007 - 09: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!

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 18 May 2007 - 11: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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 enigma

    Member

  • Members
  • PipPip
  • 58 posts

Posted 20 May 2007 - 11: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!

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 20 May 2007 - 04:47 PM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 enigma

    Member

  • Members
  • PipPip
  • 58 posts

Posted 22 May 2007 - 06:44 PM

Ok, thanks!!! ;)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users