Getting rid of jittery movement

General FreeBASIC programming questions.
Post Reply
Game Coder
Posts: 14
Joined: Apr 17, 2017 2:37

Getting rid of jittery movement

Post by Game Coder »

This code makes all of the movement on my screen kind of jittery. I'm very unsure of what to do here, I'm not sure if it's the global movement speed or the frame regulation code (Shoutout to dodicat! It seems to work super well!) but I'd really appreciate a helping hand here. I'm kind of a little bit frazzled, I've never had this much issue tracking down a low FPS issue like this before >.<

This is the main important code:

Code: Select all

dim shared MOVEMENT_AMOUNT as double

type App
   private:
      dim Fps as integer
      dim Max_Fps as integer = 200
      dim Game as Engine
   public:
      declare Function FrameCounter() As Integer
      declare Function Regulate(ByVal MyFps As Integer,ByRef fps As Integer) As Integer
      declare sub Main()
end type

'=============================================================================== Dodicat
Function App.FrameCounter() As Integer
    Var t1=Timer,t2=t1
    Static As Double t3,frames,answer
    frames+=1
    If (t2-t3) >= 1 Then t3 = t2 : answer = frames : frames = 0
    Function= answer
End Function
'==============================================================================
Function App.Regulate(ByVal MyFps As Integer, ByRef fps As Integer) As Integer
    fps=FrameCounter()
    Static As Double timervalue,lastsleeptime
    Dim As Double delta
    Var k=fps-myfps,i=1/myfps
    If Abs(k)>1 Then delta+=i*Sgn(k)
    Var sleeptime=lastsleeptime+(i-Timer+timervalue)*2000+delta
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function
'===============================================================================

sub App.Main()
   this.Game.Init()
   do 'The main rendering loop
      screenlock 'locks the screen so you can put stuff on it
      cls 'clears the screen
      this.Game.Render() 'Renders the game onto the screen
      print "FPS:"; this.fps                                                                                                                                                                 
      screenunlock 'Unlocks the screen
      sleep this.Regulate(this.Max_FPS, this.fps) 'sleeps for a specified amount of time
      
      if FPS > 0 then 'Sets the global movement speed
          MOVEMENT_AMOUNT = 1/FPS
      end if
      this.Game.Update() 'Updates the engine
   loop until inkey = chr(255) + "k" or multikey(SC_ESCAPE) 'loops until the escape key is hit or the close button is hit
   
end sub

dim Self as App
Self.Main()
This is the movement amount section specifically:

Code: Select all

sleep this.Regulate(this.Max_FPS, this.fps) 'sleeps for a specified amount of time
if FPS > 0 then 'Sets the global movement speed
	MOVEMENT_AMOUNT = 1/FPS
end if
And this is how it would be used in a movement:

Code: Select all

leftRight = multikey(LEFT_KEY) + -1*multikey(RIGHT_KEY)
upDown = multikey(UP_KEY) + -1*multikey(DOWN_KEY)
moveX = leftRight*MOVEMENT_AMOUNT*this.PlayerSpeed
moveY = upDown*MOVEMENT_AMOUNT*this.PlayerSpeed
Thank you guys so much! I'll be sharing the GitHub for this project pretty soon, and *maybe* a build for it as well ^.^
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Getting rid of jittery movement

Post by MrSwiss »

Hi,

I think that you are mixing things up. Speed of movement (of sprites/images) and speed of screen
refresh (dodicat's regulator).
The regulator is usually setup, to a specific ScreenFrameRate (number updates per second, as a
Long, IIRC). This means: once setup, it stays that way.
Movement of "figures", e.g. in a game, are controlled, by the size of the movement, also called
step size ...

Low FPS results usually, by slow and/or ineffective code (procedures might have to be re-coded,
for speed), some profiling might be needed, to figure out the "culprit".
Game Coder
Posts: 14
Joined: Apr 17, 2017 2:37

Re: Getting rid of jittery movement

Post by Game Coder »

No worries, I don't think I'm exactly mixing that up. I'm using delta movement, which means that it changes how far a sprite will jump in each frame in order to keep a constant speed despite the FPS. I get 198 FPS on this game easily, it would go faster if I had the FPS cap set higher. It's not slow/inefficient code, I'm very careful about that as I have been programming for several years. So no worries on that end ^.^

What I was asking is if I am going about the delta movement wrong, I can't quite tell what is causing the slow movement...
Game Coder
Posts: 14
Joined: Apr 17, 2017 2:37

Re: Getting rid of jittery movement

Post by Game Coder »

So I've messed around a lot with the game code and I've found that my numbers are all good. I'm beginning to wonder if this is a bug in freebasic? I sent it to a friend of mine as well and it doesn't seem that there is any problem there either.

Here is the github if you want to take a look:

https://github.com/ZaneCross/Blind_Game

Also, to be able to move around you'll have to move one of the key_layouts to the same folder as main.bas and rename it to just key_layouts.dat, I haven't added a default key layout system yet.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Getting rid of jittery movement

Post by sancho2 »

Could you zip up the necessary source files, rather than having to download the entire repository?
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Getting rid of jittery movement

Post by leopardpm »

I am not real familiar with how to navigate around github... having one zip file with all associated code and images would be a big help for others to easily get into what you are doing... include a compiled .exe would be nice as well

my first instincts for 'jittery' movement or 'stutter step' would be in how much delta you are using, and then perhaps looking at what code inside the display loop might have variable times to complete, as well as the code outside of the display loop... if things are actually jumping around, then it is definitely inside the loop I would think.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Getting rid of jittery movement

Post by St_W »

I'm not very familiar with game development and did only take a look on the code in your first post above, but wouldn't it be better to calculate the position of a moving object based on some (in-game) time/ticks counter? See the following two articles for some more information about timing in games:
http://www.koonsolo.com/news/dewitters-gameloop/
http://gameprogrammingpatterns.com/game-loop.html

So either you remember the start time + coordinates of the animation and have a function f(t)->x,y or you calculate the movement based on the passed time using a function f(old_x, old_y, delta_t)->x,y. The latter approach will probably be more suitable for direct user interaction as you've shown in your example.

btw @sancho2/leopardpm: GitHub provides a ZIP download that allows to download without a Git Client installed and the current snapshot is only 13KB. Anyway I think that any software developer should know how to use a VCS (like Git) thus I'd recommend to take a look and learn how to use it.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Getting rid of jittery movement

Post by leopardpm »

not seeing the jittery movement you are talking about... but also not seeing any colored tiles as I move around....
Game Coder
Posts: 14
Joined: Apr 17, 2017 2:37

Re: Getting rid of jittery movement

Post by Game Coder »

@leopardpm
You don't see it? I've been working at it for awhile and it's a lot smoother than it was when I first made this post, but I still see some jitter... Also, it isn't supposed to have any multi-color tiles yet, that's coming a bit later after I get the main engine set up.

@St_W
I did actually use that kind of movement, the MOVEMENT_AMOUNT variable is essentially the delta time, it is calculated using the current FPS.

Also, for anyone that doesn't want to use GitHub, here is a mediafire link to the zip:

http://www.mediafire.com/file/5mk5c23a7 ... d_Game.zip

Thanks again for helping, you guys are great ^.^
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Getting rid of jittery movement

Post by leopardpm »

Game Coder wrote:@leopardpm
You don't see it? I've been working at it for awhile and it's a lot smoother than it was when I first made this post, but I still see some jitter... Also, it isn't supposed to have any multi-color tiles yet, that's coming a bit later after I get the main engine set up.
oh, i thought I saw where you made different colored tiles in the code... so was expecting to see them...
Also, for anyone that doesn't want to use GitHub, here is a mediafire link to the zip:

http://www.mediafire.com/file/5mk5c23a7 ... d_Game.zip

Thanks again for helping, you guys are great ^.^
it wasn'tthat I didn't want to use it, I just didn't know how exactly... but since I can just download the entire source zipped up for me by clicking on 'clone/download', it works fine... sorry about my complaint, it was my own ignorance...
Game Coder
Posts: 14
Joined: Apr 17, 2017 2:37

Re: Getting rid of jittery movement

Post by Game Coder »

@leopardpm
Well, there should be one tile like around the middle of the map that is colored blue, but yeah. And it's cool, I should have posted a .zip link to begin with, I just figured if I made any changes to the code GitHub would be easier to track.

But yeah, I am incredibly confused about this jitter, I'm thinking about giving up on it cause it seems small anyway. but if anyone can tell me what may be going on that would be really awesome ^.^
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Getting rid of jittery movement

Post by leopardpm »

Game Coder wrote:@leopardpm
Well, there should be one tile like around the middle of the map that is colored blue, but yeah. And it's cool, I should have posted a .zip link to begin with, I just figured if I made any changes to the code GitHub would be easier to track.
even though I am not familiar with it, GitHub does seem to be a good way to keep track of a projects' status and changes, especially among multiple programmers each contributing. Since it provides an easy to access zip file as well, then perhaps using GitHub and (until folks get used to it here) just type a quick line on instructions to get to the zip file for novice GitHub users
But yeah, I am incredibly confused about this jitter, I'm thinking about giving up on it cause it seems small anyway. but if anyone can tell me what may be going on that would be really awesome ^.^
since you are not too far into your coding, maybe try some separate tests on different methods of doing the same thing.... also, going on to something else then coming back to the issue could provide you with a fresh perspective and be able to solve the issue in the future...
Post Reply