Android Question Loop and timer question for Arkanoid Game

Carcas

Member
Licensed User
Longtime User
Hi

(sorry for my poor English) :p

While I was creating a game Arkanoid, I realized that this did not work well on some devices.
The ball slows down and speeds up suddenly for no apparent reason
The real reason for my concern is that this strange behavior does not always happen but it happens only in a few moments. Especially when you first start after installation.

I have access to these three devices:
ASUS NEXSUS 7 (very Good)
SAMSUNG S2 (good)
HUAWEI Y300 (bad)

I thought that the reason depended on the speed of the device
so I decided to do flow analysis to adjust timer and speed according to the speed of the devices


the body of the code is something like this

B4X:
Sub TimerHome_Tick      'interval 30ms

    lasttime=DateTime.Now
        'Step ball
        'Calc Collision
        'destroy bricks
        '
        '......
    Dim c As Int
    c=DateTime.Now-lasttime
    label1.Text=c ' time in ms
End Sub

the results were
NEXSUS 15-22 ms but some time 30-40
S2 15-22 but some time 25-45
HUAWEI 20-35 but some time 40-150

I thought then that something was wrong in my code such that there were exceptions that would slow down the loop. Then I decided to do a test on loop.

B4X:
Sub Process_Globals
    Dim timer1 As Timer
End Sub
Sub Globals
    Dim label1 As Label
    Dim maxtime As Int
    Dim lasttime As Int
End Sub

Sub Activity_Create(firsttime As Boolean)
    timer1.Initialize("timer1",1000)
    label1.Initialize("label1")
    Activity.AddView(label1,50%x,50%y,15%x,10%y)
    label1.TextColor=Colors.Red
    timer1.Enabled=True
    maxtime=0
End Sub
Sub timer1_Tick
    lasttime=DateTime.Now
    For i = 0 To 1500
        For j = 0 To 1500
            'delay
        Next
    Next
    Dim c As Int
    c=DateTime.Now-lasttime
    label1.Text=c
End Sub

I set the timer to 1 second to give time to the cycle to finish

Surprise!
There are still oscillations even without a body : (


You can try it yourself

Some of you I know how to explain the reason for this strange behavior?
And how to remove?
I understand the delay of 1-2 ms but not ms 20-30-40

This depends on the other tasks running on the device?

Set the timer to 100ms is not nice for arkanoid. Effect GameBoy first : P
 

Informatix

Expert
Licensed User
Longtime User
Hi

(sorry for my poor English) :p

While I was creating a game Arkanoid, I realized that this did not work well on some devices.
The ball slows down and speeds up suddenly for no apparent reason
The real reason for my concern is that this strange behavior does not always happen but it happens only in a few moments. Especially when you first start after installation.

I have access to these three devices:
ASUS NEXSUS 7 (very Good)
SAMSUNG S2 (good)
HUAWEI Y300 (bad)

I thought that the reason depended on the speed of the device
so I decided to do flow analysis to adjust timer and speed according to the speed of the devices


the body of the code is something like this

B4X:
Sub TimerHome_Tick      'interval 30ms

    lasttime=DateTime.Now
        'Step ball
        'Calc Collision
        'destroy bricks
        '
        '......
    Dim c As Int
    c=DateTime.Now-lasttime
    label1.Text=c ' time in ms
End Sub

the results were
NEXSUS 15-22 ms but some time 30-40
S2 15-22 but some time 25-45
HUAWEI 20-35 but some time 40-150

I thought then that something was wrong in my code such that there were exceptions that would slow down the loop. Then I decided to do a test on loop.

B4X:
Sub Process_Globals
    Dim timer1 As Timer
End Sub
Sub Globals
    Dim label1 As Label
    Dim maxtime As Int
    Dim lasttime As Int
End Sub

Sub Activity_Create(firsttime As Boolean)
    timer1.Initialize("timer1",1000)
    label1.Initialize("label1")
    Activity.AddView(label1,50%x,50%y,15%x,10%y)
    label1.TextColor=Colors.Red
    timer1.Enabled=True
    maxtime=0
End Sub
Sub timer1_Tick
    lasttime=DateTime.Now
    For i = 0 To 1500
        For j = 0 To 1500
            'delay
        Next
    Next
    Dim c As Int
    c=DateTime.Now-lasttime
    label1.Text=c
End Sub

I set the timer to 1 second to give time to the cycle to finish

Surprise!
There are still oscillations even without a body : (


You can try it yourself

Some of you I know how to explain the reason for this strange behavior?
And how to remove?
I understand the delay of 1-2 ms but not ms 20-30-40

This depends on the other tasks running on the device?

Set the timer to 100ms is not nice for arkanoid. Effect GameBoy first : P
This problem is explained in my tutorial about making games, with the solution (chapter "Mind the step").
 
Upvote 0

Carcas

Member
Licensed User
Longtime User
only loops....

actually the timer is more accurate if inserted in a class I tried ;)

I thought that the loops have the same running time but then I read it right here:

http://www.b4x.com/android/forum/threads/how-to-make-games.32593/#content

so I set a time step to make constant the speed of the sprite. Now the performance is much improved but still not perfect

I will try to use a timer out of the main and I let you know

Thank you :)
 
Upvote 0

Carcas

Member
Licensed User
Longtime User
Yes please I have read. Very interesting.
I had not seen your reply
I lowered the timer to 25 ms and the ball is more fluid with the passage of time but still not perfect. I noticed delays of 1-2 milliseconds every 5-6 tick, the timer in the main.
Now I try to move an external timer to see if there are improvements.

The delays are greater after the first start of the application.

Could you tell me why?

thank you :)
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
As said by Erel, a timer is never accurate. A loop is more accurate, but introduces new (and major) problems. Until now, the best accuracy that I got was with the libGDX library (because of the sync of OpenGL with the hardware).
When the app starts, there's always a few seconds where you should avoid any animation (e.g. because your antivirus is checking the app). Usually, professional games display a static first screen with the logo of the company.
 
Upvote 0

Carcas

Member
Licensed User
Longtime User
Before leaving, there is a screen with a customView for the summary levels.

The problem, however, is only the HUAWEI Y300 which is a economic device. With the SAMSUNG S2 7 Nexsus the problem does not exist.

I tried an external timer to the main with a tic of 25 ms.

The delay is much lower than the timer in the main
class:
1 millisecond for 20-25 tic
main:
1-2 millisecond for 6-7 tics

I hope it is of help to make the most of fluidity.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Before leaving, there is a screen with a customView for the summary levels.

The problem, however, is only the HUAWEI Y300 which is a economic device. With the SAMSUNG S2 7 Nexsus the problem does not exist.

I tried an external timer to the main with a tic of 25 ms.

The delay is much lower than the timer in the main
class:
1 millisecond for 20-25 tic
main:
1-2 millisecond for 6-7 tics

I hope it is of help to make the most of fluidity.
Trying to get 60 fps (one tick every 16 ms) is impossible on some devices because either they are slow or their refresh rate is capped (some devices cannot display more than 40 fps).
 
Upvote 0

Carcas

Member
Licensed User
Longtime User
I am attaching the project of the ball in motion.
Even if I only have the ball the problem persists

I think something wrong in my algorithm

You can take a look?

thank you :)
 

Attachments

  • Test.zip
    8.4 KB · Views: 172
Upvote 0

Informatix

Expert
Licensed User
Longtime User
There are errors in your code:
- you don't compute a step value because "mis" is always equal to zero (lasttime should be moved at the end of your tick event).
- all variables implied in the move should be float, not int, because your computations return decimal values. With integers, you introduce unnecessary roundings that reduce the smoothness of the move.
 
Upvote 0

Carcas

Member
Licensed User
Longtime User
Thanks for the reply :)

actually I had already thought of rounding. :(

I did as you said. There are improvements but still not as I would like.

I started studying liGDX and the movement of the ball is really perfect :)

But I came another great doubt on the FPS but I will demand in the thred library.

Good Work ;)
 
Upvote 0
Top