Advertisement
Dr_Davenstein

Line/Circle Collision/Response

Oct 31st, 2023
1,920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "fbgfx.bi"
  2.  
  3. const as integer scr_w = 800, scr_h = 600
  4.  
  5. screenres scr_w, scr_h,32,,FB.GFX_HIGH_PRIORITY
  6.  
  7. type vec2f
  8.     x as single
  9.     y as single
  10. end type
  11.  
  12.  
  13. declare function vec2f_perp( byref a as vec2f, b as vec2f ) as vec2f
  14. declare function cPointline( byref va as vec2f, byref vb as vec2f, byref vPoint as vec2f ) as vec2f
  15. declare sub vec2f_normalize( byref v as vec2f )
  16. declare function vec2f_len( byref v as vec2f ) as single
  17. declare function vec2f_dot( byref a as vec2f, byref b as vec2f ) as single
  18. declare function vec2f_dist( byref va as vec2f, byref vb as vec2f ) as single
  19.  
  20. dim as vec2f Pl, Pld, Cent, Ero
  21. dim as single Radius, tDist
  22. dim as double deltaTime = timer, curTime = timer
  23. dim as integer i, ip1, Num_Verts
  24.  
  25. pl = type(scr_w\2, scr_h\2)
  26. Radius = 32
  27.  
  28. read Num_Verts
  29. dim Lines(Num_Verts) as vec2f
  30.  
  31. for i = 0 to ubound(Lines)
  32.     read Lines(i).X, Lines(i).Y
  33. next
  34.  
  35.  
  36.  
  37. curTime = timer
  38. do
  39.    
  40.     deltaTime = timer-curTime
  41.     curTime = timer
  42.    
  43.     var moveSpeed = 350*deltaTime
  44.    
  45.     if multikey(FB.SC_LEFT) then PlD.X -= moveSpeed  
  46.     if multikey(FB.SC_RIGHT) then PlD.X +=  moveSpeed
  47.     if multikey(FB.SC_UP) then PlD.Y -=  moveSpeed
  48.     if multikey(FB.SC_DOWN) then PlD.Y +=  moveSpeed    
  49.    
  50.     PlD.X += (-PlD.X*.95)*deltaTime
  51.     PlD.Y += (-PlD.Y*.95)*deltaTime
  52.    
  53.     Pl.X+=PlD.X*deltaTime
  54.     Pl.Y+=PlD.Y*deltaTime
  55.    
  56.     screenlock
  57.  
  58.     line(0,0)-(scr_w-1, scr_h-1),0,bf
  59.    
  60.     for i=0 to ubound(Lines)-1 step 2
  61.         ip1 = i+1
  62.         Cent = vec2f_perp( Lines(i), Lines(ip1) )
  63.         Ero = cPointline( Lines(i), Lines(ip1), PL )
  64.         tDist = vec2f_Dist( Pl, Ero )
  65.        
  66.         if tDist<Radius then
  67.             Pl.X+=Cent.X*(Radius-tDist)
  68.             Pl.Y+=Cent.Y*(Radius-tDist)
  69.         end if
  70.        
  71.         line(Lines(i).X, Lines(i).Y)-(Lines(ip1).X, Lines(ip1).Y),&hffffff00
  72.         line(Ero.X, Ero.Y)-(Ero.X + (Cent.X*Radius), Ero.Y + (Cent.Y*Radius)),&hffffffff
  73.     next
  74.    
  75.    
  76.     circle (Pl.X,Pl.Y),Radius, &hffffff00
  77.     pset (Pl.X,Pl.Y), &hffffff00
  78.     screensync
  79.     screenunlock
  80.    
  81.     sleep 30,1
  82.    
  83. loop until multikey(FB.SC_ESCAPE)
  84.  
  85.  
  86.  
  87. function cPointline( byref va as vec2f, byref vb as vec2f, byref vPoint as vec2f ) as vec2f
  88.    
  89.     dim as vec2f tVector1
  90.     dim as vec2f tVector2
  91.     dim as vec2f vReturn
  92.     dim as single d
  93.     dim as single t
  94.    
  95.     tVector1.X = VPoint.X - Va.X
  96.     tVector1.Y = VPoint.Y - Va.Y
  97.    
  98.     tVector2.X = Vb.X - Va.X
  99.     tVector2.Y = Vb.Y - Va.Y
  100.    
  101.     vec2f_normalize( tVector2 )
  102.    
  103.     d = vec2f_dist( vA, vB )
  104.     t = vec2f_dot( tVector2, tVector1 )
  105.    
  106.     if t<=0 then return Va
  107.    
  108.     if t>=d then return Vb
  109.    
  110.     vReturn.X = Va.X + (tVector2.X * t)
  111.     vReturn.Y = Va.Y + (tVector2.Y * t)
  112.    
  113.     return vReturn
  114.    
  115. end function
  116.  
  117.  
  118. function vec2f_dist( byref va as vec2f, byref vb as vec2f ) as single
  119.    
  120.     dim as single dx
  121.     dim as single dy
  122.    
  123.     dx = va.X - vb.X
  124.     dy = va.Y - vb.Y
  125.    
  126.     return sqr(dx^2+dy^2)
  127.    
  128. end function
  129.  
  130.  
  131. sub vec2f_normalize( byref v as vec2f )
  132.    
  133.     dim as single vLen
  134.    
  135.     vLen = vec2f_len ( v )
  136.    
  137.     v.x /= vLen
  138.     v.y /= vLen
  139.    
  140. end sub
  141.  
  142.  
  143. function vec2f_len( byref v as vec2f ) as single
  144.    
  145.     dim as single tLen
  146.    
  147.     tLen = sqr(v.x^2 + v.y^2)
  148.    
  149.     if tLen = 0 then tLen = 1
  150.    
  151.     return tLen
  152.    
  153. end function
  154.  
  155.  
  156. function vec2f_dot( byref a as vec2f, byref b as vec2f ) as single
  157.    
  158.     return a.x*b.x + a.y*b.y
  159.    
  160. end function
  161.  
  162.  
  163. function vec2f_perp( byref a as vec2f, byref b as vec2f ) as vec2f
  164.    
  165.     dim as single vLen
  166.     dim as vec2f d
  167.    
  168.     d.x = b.x - a.x
  169.     d.y = b.y - a.y
  170.    
  171.     vLen = vec2f_len( d )
  172.    
  173.     return type( d.y  / vLen, -d.x / vLen )
  174.    
  175. end function
  176.  
  177.  
  178.  
  179. data 21
  180.  
  181. data 50,70
  182. data 10,240
  183.  
  184. data 10,240
  185. data 100,340
  186.  
  187. data 100,340
  188. data 125,400
  189.  
  190. data 125,400
  191. data 300,450
  192.  
  193. data 300,450
  194. data 450,470
  195.  
  196. data 450,470
  197. data 600,370
  198.  
  199. data 600,370
  200. data 550,300
  201.  
  202. data 550,300
  203. data 625,250
  204.  
  205. data 625,250
  206. data 500,10
  207.  
  208. data 500,10
  209. data 320,50
  210.  
  211. data 320,50
  212. data 50,70
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement