Author Topic: A smoothly curving towards B  (Read 3925 times)

loganite

  • Guest
A smoothly curving towards B
« on: 2008-Jan-27 »
I read the excellent article on smoothly moving from point a to point b and thought some work I had done on vector printing using bezier splines might have a application here. The code follows below, it could be used for examples like :

Missles gaining height and then shooting off towards their target
Aliens outflanking players etc



Code: GLBasic [Select]
GETSCREENSIZE screenwidth,screenheight
sx=screenwidth*.2;sy=screenheight*.5   //start point
ex=screenwidth*.8;ey=screenheight*.5   //end point
bx=screenwidth*.5;by=screenheight*.5   //curve towards point
animTime=2000   //animation time of 2 seconds

startTime=GETTIMERALL()
WHILE TRUE
       
        MOUSESTATE bx,by,b1,b2

        elapsed = GETTIMERALL()-startTime
        IF elapsed > animTime   //restart the animation
                startTime = GETTIMERALL()
                elapsed = 0
        ENDIF
       
        T=elapsed/animTime   //how long along the path
        x= CalcQuadBezier(sx,bx,ex,T) //calculate position
        y= CalcQuadBezier(sy,by,ey,T)
       
        DRAWRECT x,y,10,10,255

       
        PRINT "Time:"+elapsed,0,20
        PRINT "< Curves Toward",bx,by
        PRINT "^ start point",sx,sy+10
        PRINT "^ end point",ex,ey+10
        SHOWSCREEN
       

WEND
END


//point0=start point, point1=bezier point, point2 = end point, 0<t<1 = where along the path
FUNCTION CalcQuadBezier: Point0,Point1,Point2,t

        pt = POW((1-t),2)*Point0 + 2*t*(1-t)*Point1 +t*t*Point2
        RETURN pt

ENDFUNCTION

Offline Quentin

  • Prof. Inline
  • *****
  • Posts: 915
    • View Profile
A smoothly curving towards B
« Reply #1 on: 2008-Jan-27 »
looks nice, could be very handy for controlling enemy ships or something like that

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10682
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
A smoothly curving towards B
« Reply #2 on: 2008-Jan-27 »
Genious!

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2580
    • View Profile
A smoothly curving towards B
« Reply #3 on: 2008-Jan-28 »
Excellent stuff! :)
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)

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5006
  • O Rly?
    • View Profile
A smoothly curving towards B
« Reply #4 on: 2008-Jan-28 »
Nice stuff, but pls use the "codeblock" for posting code, thx !
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard