views:

492

answers:

2

Hi there,

I'm making a little iphone game, and I would get some clues. Let's imagine:

Two background sprites moving pretty fast from right to left, and moving up and down with accelerometer.

I guess I can't use animations here, cause the movement of the background is recalculated at each frame. So I use a schedule with an interval of 0.025s and move my sprites at each clock with a :

sprite.position = ccp(x, y);

So here is my problem: the result is laggy, with only these two sprites.

I tried both declaring sprites in the header, and getting them with CCNodes and Tags. It's quite the same.

So if someone can give me a hint on what is the best way to do that, that would be so nice. I wonder if the problem can't be the fact that sprites are moving very fast, but i'm not sure.

Anyway, thanks for your time.

J.

+3  A: 

0.025 equals 40 fps, that could be a bit steep depending on the rest of your game loop. It’s good to think about the relationship between framerate and percieved speed. When you increase framerate, you make the movement smoother, but only as long as you know how to calculate the motion. If you move the objects with by a fixed distance with each tick of the time, they will stutter, because the timer is not precise. (Think about it: 40 fps means 25 ms per frame. When the timer gets off by five milliseconds – not very much, is it? –, you change the frame time by 20 percent. It is not unusual for the time events to get off by 10–20 ms.)

This means that you have to calculate the real time delta from the last frame and move the objects according to it. A bit like this:

const int kDesiredModelFramerate = 50; // fps
const double kDesiredFrameTime = 1.0/kDesiredModelFramerate;

double now = CFAbsoluteTimeGetCurrent();
double delta = now - lastFrameTime;
double correction = delta/kDesiredFrameTime;
[object setX:object.x + step*correction];

This looks complicated, but isn’t. When it took 40 ms from the last frame, you know you are one frame “late”, since to keep the desired framerate of 50 fps you’d like to keep each frame under 20 ms. Therefore the correction will be 0.04/0.02 = 2 and the usual movement step will get multiplied by 2 to make up for the lag. Now you can change the framerate as you wish – the percieved speed of the movement will stay the same, only the smoothness of the motion will change.

<voice class="Simon Peyton Jones">Does that make sense?</voice>

There is a great article about game loops where you can read about this in more detail. It’s a must read for game programmer. The exact way to realize all this in Cocos2D can vary, but the point remains the same. Getting this issue should help you get things moving smooth and fast.

zoul
Very well. You made yourself clear and I now see where I must lead.This article is very helpful indeed.Thank you!
Johnny Oin
+1  A: 

If you are using Cocos2D and need to find elapsed time from start of "game", it is better to use time_elapsed as shown below. Many other methods I have tried return a very high-valued DOUBLE variable, which causes more problems in double-to-float conversion accuracy.

Make changes in the "tick" function in the Cocos2D CCLayer scenes

-(void) tick: (ccTime) dt
{
  static float time_elapsed = 0.0;
  time_elapsed += dt;

  ...

  float velocity = 5.0f * cosf(time_elapsed * 2 * M_PI) ;
  jPrism->SetMotorSpeed( velocity );
}
zeroinverse