سؤال


I just finished developing my mini-game on Android and I got some questions about perfomance and generally "how to":

If I have 25+ objects on the creeen (just a polygon with 7-8 angles) its starts to lag hard and CPU usage is very high (15-20% without colision detection, up to 50% with collision detection on samsung note 10.1 tablet).
So I have ~25 polygons (asteroids) and they are always "flying". So to move them I make a Timer, right? On TimerTask I got something like this

public void move() {
    translate[1] += speed[1];
    translate[0] += speed[0];

    updateTranslateMatrix();    // updating translate matrix and then send it into vertex shader
    updateAABBCoords(); // update coordinates of Axis-aligned bounding box
    updateCoordsByTranslate(); // update coordinates of verticles (to be able to define exact collision (with bullets and starship))
}

Is there something unnecessary in this method?
Also I want to ask if it is OK to run this timer every 17 ms? Or 17 ms is too often? I feel smoothness of movement only at 17 ms, may be I am doing it wrong? And same Timer interval I got on starship/bullets movement and on Collision detection.
Help me, please. I feel like missing something huge and fundamental, because 50% CPU on note 10.1 in that simple Asteroids game is not normal.

هل كانت مفيدة؟

المحلول

One key issue, I believe, is that you are assigning an individual Timer to every object instead of employing a general purpose game loop. A vast majority of games use a game loop which runs continuously and can be split, typically, into two components: Update and Render.

An article on basic game loop design for Java/Android

An answer to a relevant question on game loop design on gamedev.stackexchange.com

When using a game loop you can not guarantee being able to update exactly every 17ms or some other arbitrary duration. Instead, the Update and Render methods receive a DeltaTime parameter which is the time since the last frame.

This is then used when, for example, getting new object positions by multiplying it with the object's velocity. Doing so allows for smooth interpolation of position and ensures that object speed is not affected by CPU speed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top