Android, how important is deltaTime?

Im making a game that is getting pretty big and sometimes my thread has to skip a frame, so far I'm not using deltaTime for setting the speed of my different objects in the game because it's still not a big enough game for it to matter imo. But its getting bigger then I planned, so my question is, how important is delta Time?

If I should use delta time there is a problem, since speedX and speedY are integers(they have to be for eclipse to let you make a rectangle of them), I cant add delta time very functionally as far as I understand, but might be wrong? Ive tried adding deltaTime to the code below, and sometimes my enemies just not move after spawn, they just stand there and run in the same place

Will add an some code for how I set / use speed:

     public void update(int dx, int dy) {
double theta = 180.0 / Math.PI * Math.atan2(-(y - controls.pointerPosition.y), controls.pointerPosition.x - x);

x +=dx * Math.cos(Math.toRadians(theta));
y +=dy * Math.sin(Math.toRadians(theta));
currentFrame = ++currentFrame % BMP_COLUMNS;
}

public void draw(Canvas canvas) {
int srcX = currentFrame * width;
int srcY = 1 * height;
Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
Rect dst = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(bitmap, src, dst, null);
}


So if someone with some experience with this has any thoughts, please share. Thank you!

Changed code:

    public void update(int dx, int dy, float delta) {
double theta = 180.0 / Math.PI * Math.atan2(-(y - controls.pointerPosition.y), controls.pointerPosition.x - x);
double speedX = delta * dx * Math.cos(Math.toRadians(theta));
double speedY = delta * dy * Math.sin(Math.toRadians(theta));
x += speedX;
y += speedY;
currentFrame = ++currentFrame % BMP_COLUMNS;
}

public void draw(Canvas canvas) {
int srcX = currentFrame * width;
int srcY = 1 * height;
Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
Rect dst = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(bitmap, src, dst, null);
}


with this code my enemies move like before, except they wont move to the right (wont increment x), all other directions work.

• If your Rect class only supports integers you should create your own. And this question isn't android specific. Commented Sep 11, 2012 at 17:17
• actually it is, because part of the question is if delta time is important for android games. Commented Sep 11, 2012 at 17:23
• @Matsemann As you can see in the OP's code, the Rect class is used by cavas.drawBitmap. So, unless they want to re-implement that as well, they're stuck using the regular Rects at least for the final draw call. That said, they could make a FloatRect (or whatever) class that has a getRect method that rounds the float values and returns a Rect. Not sure if this is what you meant, but thought I'd elaborate. Commented Sep 11, 2012 at 17:24
• think creating my own rects etc is abit more then I can handle, dont really want to have to do that. is there no other way? Commented Sep 11, 2012 at 17:26
• Commented Sep 12, 2012 at 12:10

Delta Time is used to make your game speed constant, independently of framerate. If your game starts to get "heavier", it will start to slow down in framerate on less powerful android systems. For example, if you made it to run on 60 FPS, an Android cellphone that can run it at 30FPS will have the game slowed down by 50%!

By including the DeltaTime in the game, you assure that the game speed remains constant, even when the FPS is low or keeps changing.

So yeah, unless you are 100% sure your game will run at full FPS on every system, I'd say DeltaTime is pretty damn important.

Also, about how to use DeltaTime properly:

To use it properly, you have to multiply it by a value that is being updated on every frame. For example, if you have your speed being increased by 10 units per second, you'd use something like this:

Speed += 10 * DeltaTime // Must be inside a function that is called every frame


You don't need delta time if the instruction is only going to be called once, only if it's something that is being called at every frame. DeltaTime is the ammount of time passed since the last frame (in seconds) and is usually a very small value, so multiplying it by something that is called only once will just make that value very close to zero.

• Yeah. agreed...just dont know how to add it when I use rectangles. Commented Sep 11, 2012 at 19:41
• Read my answer again, I added some extra info that might make you understand the concept and usage of DeltaTime better. Commented Sep 11, 2012 at 19:48
• right, this is what Ive tried.. But I might try adding a speedX and speedYvaiable, both float. Then adding deltatime, dx, dy and direction to these variables, then just do x=(int)speedX instead of the code I have now. that might work Commented Sep 11, 2012 at 19:56
• this almost works. the only problem is that as soon as my enemies need to move to the right (incremet x), they will just stand still. All other directions work except this one. Really weird Commented Sep 11, 2012 at 21:17

with this code my enemies move like before, except they wont move to the right (wont increment x), all other directions work.

add a Log trace after your speedX affectation:

double speedX = delta * dx * Math.cos(Math.toRadians(theta));
Log.e("","speedX: "+speedX);


and check if speedX is >0 when enemies are supposed to move to the right

If you start to notice the frame skips, especially as you're one of the people testing it and know it really well, then you should add deltaTime.

But if you don't notice the frame skips, then you don't need deltaTime.