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.
Rect
class is used bycavas.drawBitmap
. So, unless they want to re-implement that as well, they're stuck using the regularRect
s at least for the final draw call. That said, they could make aFloatRect
(or whatever) class that has agetRect
method that rounds the float values and returns aRect
. Not sure if this is what you meant, but thought I'd elaborate. – Richard Marskell - Drackir Sep 11 '12 at 17:24float
and then just putting an(int)
cast infront of them in thecanvas.drawBitmap
. – Green_qaue Sep 11 '12 at 17:29MAX_FPS = 60
and so far my game never goes below that. but it can definently get below 60. – Green_qaue Sep 11 '12 at 18:16((int)2.4f, (int)3.5f, (int)4.4f, (int)5.5f)
. Then it becomes:(2,3,4,5)
. Then let's say you want to move it by x+2.6f. So werect.offset((int)2.6f, 0)
. Technically, it should be at(2.4+2.6,3.5,4.4+2.6,5.5)
or(5,3.5,7,5.5)
but due to losing the fraction, it's at(4,3,6,5)
. Rounding might help... – Richard Marskell - Drackir Sep 11 '12 at 19:13