10.03.2019 - Round 1 results of our "A Love Letter For FreeBASIC" game dev competition have been published. Please be sure to check the results thread: http://games.freebasic.net/forum/index.php?topic=629.0. Don't forget that the competition is continuing with a round 2, lasting till 29th of April, 300 USD first prize. Stay tuned!

Author Topic: Smooth movement  (Read 3617 times)

Tusike

  • Novice
  • ***
  • Posts: 95
    • MSN Messenger - varnaipeti@hotmail.com
    • View Profile
    • Email
Smooth movement
« on: June 16, 2010, 08:15:17 AM »
Hey!

Can anyone tell me how I can run this simple program in which a ball moves around the screen, and make the movement smooth? Right now it like sometimes seems to go faster, then slower, all in all the movement is definitely not smooth. I've played games in which they had things move much faster, and still it appeared to be smoother.

@Kristopher: I know you're making a breakout game (how's that going on by the way?), so perhaps you can tell me how you handled the ball movement.

Thanks!

Here's the code:

Code: [Select]
screenres 1280, 800, 24,,1
const dt = .005
dim as double t = 0.0, currentTime, newTime, accumulator, deltaTime
dim as single frames, fps
dim as single bx, by, bvx, bvy
dim as integer br
bx = 500
by = 500
bvx = int(rnd * 300) + 150
bvy = int(rnd * 300) + 150
br = 10
currentTime = timer
accumulator = 0.0

do
    newTime = timer
    deltaTime = newTime - currentTime   
    currentTime = newTime   
    accumulator += deltaTime
   
    while (accumulator>=dt)
       
        bx += bvx * dt
        by += bvy * dt
       
        if bvx < 0 and bx - br < 0 then bvx *= -1: bx += bvx*dt
        if bvx > 0 and bx + br > 1279 then bvx *= -1: bx += bvx*dt
        if bvy < 0 and by - br < 0 then bvy *= -1: by += bvy*dt
        if bvy > 0 and by + br > 799 then bvy *= -1:by += bvy*dt
       
        t += dt
        accumulator -= dt       
    wend
    screenlock
    line (0, 0) - (1279, 799), rgb(20, 20, 80), BF   
    circle (bx, by), br, rgb(200, 80, 20),,,,F     
    screenunlock
    sleep 1
loop while inkey <> chr(27)
sleep

mambazo

  • Recruit
  • **
  • Posts: 35
    • View Profile
Re: Smooth movement
« Reply #1 on: June 19, 2010, 08:17:25 PM »
I don't think its possible on some setups.

Windows XP and gfxlib2 have always had the jitters for me. OpenGL seems to work well though, maybe its to do with hardware acceleration?

Tusike

  • Novice
  • ***
  • Posts: 95
    • MSN Messenger - varnaipeti@hotmail.com
    • View Profile
    • Email
Re: Smooth movement
« Reply #2 on: June 20, 2010, 12:41:38 AM »
I don't know OpenGL. But I did post this up on the freebasic forum, and it turned out adding "screensync" makes it work fine. Consider this thread closed.

-Tusike

KristopherWindsor

  • Forum Sage
  • *****
  • Posts: 363
  • The Thirsty Smiley
    • View Profile
    • Reddit/r/pics
    • Email
Re: Smooth movement
« Reply #3 on: June 23, 2010, 05:54:00 PM »
I could never get really smooth motion on Vista / 7, but most people probably wouldn't notice. Screensync didn't work for me, but really some computers looked smoother than others. Ideally you want exactly 60FPS, so in your case I think you want to sleep longer, but it still won't look perfect.
FWIW I used the GDI mode in Ultrabreaker due to other issues. The game isn't getting finished anytime soon; I'm interning at Facebook this summer. :D