(Softly) Moving from Point A to Point B

Previous topic - Next topic

Kitty Hello

Code (glbasic) Select


LIMITFPS -1

LOCAL x_start  =0
LOCAL x_end    = 400


remake:
LOCAL tstart = GETTIMERALL()
LOCAL duration = 2000 // [ms]



WHILE TRUE

DRAWRECT x_start,0,4,100,0xffffff
DRAWRECT x_end,0,4,100,0xffffff

// find a position of the left to right movement
// in the range [0.0 ... 1.0]
LOCAL pos = (GETTIMERALL()-tstart) / duration

IF pos > 1.0 THEN BREAK // ok, reached the end


// move at linear speed
LOCAL x
x = x_start + pos * (x_end-x_start)
PRINT "Linear", x, 0

// accelerating
x = x_start + SIN(pos*90) * (x_end-x_start)
PRINT "sin", x, 30

// decelerating
x = x_start + (1.0-COS(pos*90)) * (x_end-x_start)
PRINT "cos", x, 60

// accellerating and decelerating
x = x_start - (COS(pos*180)-1)/2 * (x_end-x_start)
PRINT "sin", x, 90

SHOWSCREEN


WEND

KEYWAIT
GOTO remake

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

bigsofty

Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

FutureCow

Anyone have this code? I want to compare it to my solution.

Kitty Hello

OK, I rewrote it quickly. Sorry, the lots of text for that tutorial is gone, but I think you get the idea.

FutureCow


Wampus


monono

#7
Another two:
Code (glbasic) Select
     //bouncing
  IF pos < 0.363636
x= (x_end) * (7.5625 * pos * pos ) + x_start
ELSEIF pos < 0.727273
pos = pos - 0.545455
x= (x_end) * (7.5625 *pos * pos + .75) + x_start
ELSEIF pos < 0.909091
pos = pos - 0.818182
x= (x_end) * (7.5625 * pos *pos + .9375) + x_start
ELSE
pos = pos - 0.954545
x= (x_end) * (7.5625 * pos * pos + .984375) +x_start
ENDIF
    PRINT "bounce", x, 120

     // elastic
IF pos*duration = 0
x = x_start + x_end -x_start
ELSE
p = duration * 0.3
s = p / 4
tt = -10 * (pos)
pp = (((pos * duration - s) * (2 * 3.14159) / p ) * 180.0) / 3.14159
x = (x_end) * (POW(2,tt)) * SIN( pp ) + x_start + x_end-x_start
ENDIF
   PRINT "elastic", x, 150
   


I found that in a freebasic forum. Sadly I lost the link. There was more movement smoothing math.