Smooth Movement

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Smooth Movement

Postby Phil987 » Fri Jan 08, 2010 4:10 am

So I read a tutorial on creating bitmaps and drawing them to the screen and all that stuff.

In the tutorial for a bitmap to be moved around the screen it just did

Code: Select all
Object.setCoordX(Object.getCoordX() + 1);


That is if the object was moving to the right, but this was in a loop and the bitmap was constantly being moved to the right fairly smoothly. (Until it hit the edge then it went left);

What I want to do is control an object with the Dpad and move my object around. The only problem is that by using curX + 1 the object moves extremely slowly, and if I use something like curX + 5 or + 10 it moves fast but extremely choppy. So, is there a better way to move the bitmap around that is smoother?

(When the Dpad is pressed I'm moving the bitmap within the onKeyDown method)
Phil987
Freshman
Freshman
 
Posts: 2
Joined: Fri Jan 08, 2010 3:51 am

Top

Re: Smooth Movement

Postby Shaka » Fri Jan 08, 2010 9:28 am

Phil987 wrote:So I read a tutorial on creating bitmaps and drawing them to the screen and all that stuff.

In the tutorial for a bitmap to be moved around the screen it just did

Code: Select all
Object.setCoordX(Object.getCoordX() + 1);


That is if the object was moving to the right, but this was in a loop and the bitmap was constantly being moved to the right fairly smoothly. (Until it hit the edge then it went left);

What I want to do is control an object with the Dpad and move my object around. The only problem is that by using curX + 1 the object moves extremely slowly, and if I use something like curX + 5 or + 10 it moves fast but extremely choppy. So, is there a better way to move the bitmap around that is smoother?

(When the Dpad is pressed I'm moving the bitmap within the onKeyDown method)


Hiya!

Yeah you shouldn't do it like that because not only is it choppy but if you've assigned a velocity to the object it will update incorrectly if there are slowdowns.

Let's say you want the object to move x pixels/s, then you simply calculate how long time it's been since the last update and update the position according to that.

Some pseudo code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. onKeyDown (){
  2. float timeSinceLastUpdate = CurrentTime - LastUpdateTime;
  3. LastUpdateTime = CurrentTime;
  4. myObject.updatePosition(timeSinceLastUpdate);
  5. }
  6.  
  7. MyObject.updatePosition(float deltaTime){
  8. position += movementDirection * (velocity * deltaTime);
  9. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Hope this helps!
Shaka
Developer
Developer
 
Posts: 44
Joined: Fri Aug 28, 2009 3:01 pm

Postby Phil987 » Fri Jan 08, 2010 10:44 pm

Thanks! That helped a lot.

Only thing I noticed is that you have to update theTimeSinceLastUpdate outside of the onKeyDown method, otherwise it will only update when you press a key and that's no good.

My object moves at a constant rate now, but its still kind of choppy, I think it just has to do with emulator though. I'll hopefully find someone with a phone I can use and test it.

Anyway thanks again!
Phil987
Freshman
Freshman
 
Posts: 2
Joined: Fri Jan 08, 2010 3:51 am

Postby Shaka » Sat Jan 09, 2010 12:19 pm

Phil987 wrote:Thanks! That helped a lot.

Only thing I noticed is that you have to update theTimeSinceLastUpdate outside of the onKeyDown method, otherwise it will only update when you press a key and that's no good.

My object moves at a constant rate now, but its still kind of choppy, I think it just has to do with emulator though. I'll hopefully find someone with a phone I can use and test it.

Anyway thanks again!


Glad I could help. Yeah the emulator is extremely slow so it'll probably work better on an actual device.
Shaka
Developer
Developer
 
Posts: 44
Joined: Fri Aug 28, 2009 3:01 pm

Postby Skywhy » Mon Jan 11, 2010 12:47 am

Not getting into the nitty grit of things but when you use delta time for movement (as you should) you can throw in few extra tricks and interpolate the rendering point per every render frame for the entity achieving even SMOOTHER look. And this will be [i]smooth[/] on low fps too and extremely smooth on high fps.
Skywhy
Developer
Developer
 
Posts: 35
Joined: Tue Oct 20, 2009 7:55 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests