Main Menu

Smooth Scrolling

Started by Graffo, March 29, 2008, 05:15:12 PM

Previous topic - Next topic

Graffo

Hello,

Using the examples in the help files I have written a basic rough scrolling demo using the left and right keys. The problem I have is that the scrolling isn't silky smooth and seems to have a little stuttering every so often. Was just wondering what I can do to sort this problem out?

Thanks for any help

Graffo

This is my program:


OpenScreen 1024,768,32,2
SetFPS 60
ScreenVsync On
CreateWorld 1
CaptureToWorld 1


For a=1 To 1000
x=Rnd(10000)
y=Rnd(1000)
Box x,y,x+30,y+30,1
Next a

xps#=1
yps#=1
accel#=0.5
topspeed#=6
speed#=accel#
lstky=0
CreateCamera 1
CaptureToScene

Do
If LeftKey()
If speed#<topspeed#
speed#=speed#+accel#
ElseIf speed#>topspeed#
speed#=topspeed#
EndIf
xps#=xps#-speed#
lstky=1
ElseIf RightKey()
If speed#<topspeed#
speed#=speed#+accel#
ElseIf speed#>topspeed#
speed#=topspeed#
EndIf
xps#=xps#+speed#
lstky=2
ElseIf UpKey()

ElseIf DownKey()

Else
If speed#>accel# And lstky=2
speed#=speed#-accel#
xps#=xps#+speed#
ElseIf speed#>accel# And lstky=1
speed#=speed#-accel#
xps#=xps#-speed#
Else
speed#=accel#
lstky=0
EndIf
EndIf
setcursor 0,0
CaptureToScene
ClsScene
CameraGrabWorld 1,1
PositionCamera 1,xps#,yps#

Box xps#+512,yps#+384,xps#+522,yps#+394,1
DrawCamera 1
Text 10,10,"xps# is      "+Str$(xps#)
Text 10,25,"yps# is      "+Str$(yps#)
Text 10,40,"Speed# is    "+Str$(speed#)
Text 10,55,"accel# is    "+Str$(accel#)
Text 10,70,"Top Speed is "+Str$(topspeed#)
Text 10,85,"FPS is       "+Str$(FPS())
Text 10,100,"Last key is "+Str$(lstky)


Sync
Loop






Draco9898

#1
I've cooked this up for you, have fun, I've also dropped it into the resource section

; PROJECT : Smooth Scrolling Camera
; AUTHOR  : Trevor aka Draco9898
; CREATED : 3/29/2008
; ---------------------------------------------------------------------
setfps 60: ScreenVsync 1
Global ScreenW#=800: Global ScreenH#=600
OpenScreen ScreenW#,ScreenH#,16,2

type tCam
LastX#,LastY#
X#,Y#
targx#,targy#
endtype
Dim Cam as Tcam

type tBoxx
X,Y,RgBRed
endtype
Dim boxx(45) as tboxx
boxx(1).x#=200
boxx(1).y#=200
boxx(1).RgBRed=0
For X=2 to 45
boxx(x).x=rnd(3000)
boxx(x).y=rnd(600)
boxx(X).RgBRed=rnd(255)
Next X


CreateCamera 1
CameraCLScolour 1,RGB(88, 145, 207)
Do
CaptureTOscene: ClsScene
`logic
`CAM MOVEMENT
if rightkey()=1
Cam.targX#=Cam.targX# +50
endif
if leftkey()=1
Cam.targX#=Cam.targX# -50
endif
if downkey()=1
Cam.targY#=Cam.targY# +50
endif
if upkey()=1
Cam.targY#=Cam.targY# -50
endif
`SCROLL CAMERA SMOOTHLY
Cam.LastX#=GetCameraX(1)
Cam.LastY#=GetCameraY(1)
X#=(Cam.TargX#-Cam.X#)/14
Y#=(Cam.TargY#-Cam.Y#)/14
Cam.X#=Cam.X#+X#
Cam.Y#=Cam.Y#+Y#
PositionCamera 1,cam.X,cam.Y

`BOXXes
For X=1 to 45
X#=Boxx(x).X#: Y#=Boxx(x).Y#
BoxC 0+X#,0+Y#,80+X#,80+Y#,1,RGB(boxx(X).RgBRed,0,0)
Next X

X#=10+Cam.X#: Y#=10+Cam.Y#
text X#,Y#,str$(Cam.TargX#)+ " "+ str$(Cam.TargY#)
DrawCamera 1
Sync
Loop
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin


Your problem is here,


SetFPS 60
ScreenVsync On


   If you're going to use Vsync, then you shouldn't also use SetFPS.      If you enable Vsync, PB (well the video driver) will try and display the frame at the monitors refresh rate.  The problem is that the monitor has it's own refresh rate, which might be anything  (ie higher/lower than our forced estimate rate from Setfps).   You can't force the monitor refresh rate, this might damage players monitor.     

   This mean that if use Vsync to hold the video refresh, and you want your game to run at a fixed rate of 60 frames per second, then you'll have to scale your movements accordingly.    Another issue that comes to mind, is the Video drivers often let people disable Vsync for the video card.   So on those machines enabling Vysnc won't have any effect.

Draco9898

Oops. :) Forgot About that after so many other coding/art projects
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

Graffo

Hello again,

Thanks very much for both your help. I tried removing the vsync command and just used the setfps 60 command but the trouble with this is that the scrolling isn't smooth anymore. Could you ellaborate a bit more about how to get smooth scrolling or what the problem might be with my code.

Thanks again

kevin

 Enable VSYNC
remove the Setfps line
and then read up on timer base movement

Graffo

Hello again,

Thanks very much for your reply. I read up on timer based movement and wrote a small program to move a box and I had the same stuttering problem. I have noticed it only happens when the program first starts. I then went back and adjusted the previous program I posted to remove the set fps and just use vsync which will i guess cause the program refresh at 60 hertz which is the same as my monitor refresh rate. I also added a line to print the FPS as well. I have noticed that during the first 8 seconds when the program starts the FPS fluctuates from 60 down to 54 then up and down again until it settles on 60 and stays there. After that the scrolling is silky smooth and looks cool. I was just wondering what could be causing this problem as I have tried it on two computers and get the same problem. Both computers are powerful so I don't think that's the problem and after the program settles at 60 FPS it stays there. At the moment I have put a delay in of 8 seconds which gets around the problem but this is not ideal. Any ideas?

Draco9898

There a problem with the amount of frames per second and how many times you can check a timer per second, this timer might be accurate down to any space of time, but you can only check it so many times per loop, making it frame dependent
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

philltec

I have a similar problem. Usually the game runs really quickly for the first couple of seconds, I noticed this as when I am just testing a game I don't always have intro screens. If I add one or two intro screens by the time I start playing the game it is running smoothly. Don't know how to fix this but it is not usually a problem once the game is finished with intro screens.