Advertisement
Dr_Davenstein

Tile Collision

Jan 19th, 2025
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
FreeBasic 5.42 KB | Gaming | 0 0
  1. #include "fbgfx.bi"
  2.  
  3. const as integer scr_w = 320, scr_h = 240
  4.  
  5. screenres scr_w, scr_h,32,,FB.GFX_HIGH_PRIORITY
  6.  
  7. type vec2f
  8.  
  9.     declare constructor()
  10.     declare constructor( tx as single, ty as single )
  11.  
  12.     x as single
  13.     y as single
  14. end type
  15.  
  16. type line_struct
  17.     p1 as vec2f
  18.     p2 as vec2f
  19. end type
  20.  
  21. type map_struct
  22.     as integer collision
  23.     as integer cl
  24.     linez(5) as line_struct
  25. end type
  26.  
  27.  
  28. declare function vec2f_perp( byref a as vec2f, b as vec2f ) as vec2f
  29. declare function cPointline( byref va as vec2f, byref vb as vec2f, byref vPoint as vec2f ) as vec2f
  30. declare sub vec2f_normalize( byref v as vec2f )
  31. declare function vec2f_len( byref v as vec2f ) as single
  32. declare function vec2f_dot( byref a as vec2f, byref b as vec2f ) as single
  33. declare function vec2f_dist( byref va as vec2f, byref vb as vec2f ) as single
  34.  
  35. dim as vec2f Pl, Pld, Cent, Ero
  36. dim as single Radius, tDist
  37. dim as double deltaTime = timer, curTime = timer
  38. dim as integer i, ip1, Num_Verts
  39.  
  40.  
  41.  
  42. '==box added shit==
  43. dim shared Map(150, 150) AS map_struct
  44.  
  45. map(0,0).collision = 1
  46. map(0,0).cl = 3
  47.  
  48. map(0,0).linez(0).p1 = vec2f(0,0)
  49. map(0,0).linez(0).p2 = vec2f(16,0)
  50.  
  51. map(0,0).linez(1).p1 = vec2f(16,0)
  52. map(0,0).linez(1).p2 = vec2f(16,16)
  53.  
  54. map(0,0).linez(2).p1 = vec2f(16,16)
  55. map(0,0).linez(2).p2 = vec2f(0,16)
  56.  
  57. map(0,0).linez(3).p1 = vec2f(0,16)
  58. map(0,0).linez(3).p2 = vec2f(0,0)
  59.  
  60.  
  61. map(1,0) = map(0,0)
  62.  
  63. map(1,1).collision = 2
  64. map(1,1).cl = 1
  65. map(1,1).linez(0).p1 = vec2f(16, 0)
  66. map(1,1).linez(0).p2 = vec2f(0, 16)
  67.  
  68. map(18,1).collision = 2
  69. map(18,1).cl = 1
  70. map(18,1).linez(0).p1 = vec2f(16,16)
  71. map(18,1).linez(0).p2 = vec2f(0,0)
  72.  
  73. map(5,5).collision = 1
  74. map(5,5).cl=0
  75. map(5,5).linez(0).p1 = vec2f(0,16)
  76. map(5,5).linez(0).p2 = vec2f(16,16)
  77.  
  78. map(10,10) = map(0,0)
  79.  
  80.  
  81. '==================
  82. for y as integer = 0 to 14
  83.     map(0, y) = map(0,0)
  84.     map(19, y) = map(0,0)
  85. next
  86.  
  87. for x as integer = 0 to 19
  88.     map(x, 0) = map(0,0)
  89.     map(x, 14) = map(0,0)
  90. next
  91.  
  92. pl = type(scr_w\2, scr_h\2)
  93. Radius = 8
  94.  
  95.  
  96. curTime = timer
  97. do
  98.  
  99.     deltaTime = timer-curTime
  100.     curTime = timer
  101.  
  102.     var moveSpeed = 350*deltaTime
  103.  
  104.     'PlD.X=0
  105.     'PlD.Y=0
  106.     'if multikey(FB.SC_LEFT) then PlD.X -= 1'moveSpeed
  107.     'if multikey(FB.SC_RIGHT) then PlD.X += 1' moveSpeed
  108.     'if multikey(FB.SC_UP) then PlD.Y -= 1' moveSpeed
  109.     'if multikey(FB.SC_DOWN) then PlD.Y += 1' moveSpeed
  110.  
  111.     ''PlD.X += (-PlD.X*.95)*deltaTime
  112.     ''PlD.Y += (-PlD.Y*.95)*deltaTime
  113.  
  114.     'Pl.X+=PlD.X'*deltaTime
  115.     'Pl.Y+=PlD.Y'*deltaTime
  116.  
  117.     if multikey(FB.SC_LEFT) then PlD.X -= moveSpeed
  118.     if multikey(FB.SC_RIGHT) then PlD.X +=  moveSpeed
  119.     if multikey(FB.SC_UP) then PlD.Y -=  moveSpeed
  120.     if multikey(FB.SC_DOWN) then PlD.Y +=  moveSpeed
  121.  
  122.     PlD.X += (-PlD.X*5)*deltaTime
  123.     PlD.Y += (-PlD.Y*5)*deltaTime
  124.  
  125.     Pl.X+=PlD.X*deltaTime
  126.     Pl.Y+=PlD.Y*deltaTime
  127.  
  128.  
  129.  
  130.     for x as integer = 0 to 20
  131.         for y as integer = 0 to 15
  132.  
  133.             if not map(x,y).collision=0 then
  134.  
  135.                 for c as integer = 0 to map(x,y).cl
  136.  
  137.                     dim as vec2f tt = map(x,y).linez(c).p1
  138.                     dim as vec2f tt2 = map(x,y).linez(c).p2
  139.  
  140.                     tt.x+=(x*16)
  141.                     tt.y+=(y*16)
  142.                     tt2.x+=(x*16)
  143.                     tt2.y+=(y*16)
  144.  
  145.                     Cent = vec2f_perp( tt, tt2 )
  146.                     Ero = cPointline( tt, tt2, PL )
  147.                     tDist = vec2f_Dist( Pl, Ero )
  148.  
  149.                     if tDist<Radius then
  150.  
  151.                         Pl.X+=Cent.X*(Radius-tDist)
  152.                         Pl.Y+=Cent.Y*(Radius-tDist)
  153.  
  154.                     end if
  155.  
  156.                 next
  157.  
  158.             end if
  159.  
  160.         next
  161.  
  162.     next
  163.  
  164.  
  165.     screenlock
  166.  
  167.     line(0,0)-(scr_w-1, scr_h-1),0,bf
  168.     circle (Pl.X,Pl.Y),Radius, &hffffff00
  169.     pset (Pl.X,Pl.Y), &hffffff00
  170.  
  171.     for x as integer = 0 to 20
  172.  
  173.         for y as integer = 0 to 15
  174.  
  175.             if not map(x,y).collision=0 then
  176.  
  177.                 for c as integer = 0 to map(x,y).cl
  178.  
  179.                     line( map(x,y).linez(c).p1.x+(x*16), map(x,y).linez(c).p1.y+(y*16) ) - ( map(x,y).linez(c).p2.x+(x*16), map(x,y).linez(c).p2.y+(y*16) ), &hffffff00
  180.  
  181.                 next
  182.  
  183.             end if
  184.  
  185.         next
  186.  
  187.     next
  188.  
  189.     screensync
  190.     screenunlock
  191.  
  192.     sleep 3,1
  193.  
  194. loop until multikey(FB.SC_ESCAPE)
  195.  
  196.  
  197.  
  198. constructor vec2f()
  199.  
  200. end constructor
  201.  
  202. constructor vec2f( tx as single, ty as single )
  203.  
  204. this.x = tx
  205. this.y = ty
  206.  
  207. end constructor
  208.  
  209. function cPointline( byref va as vec2f, byref vb as vec2f, byref vPoint as vec2f ) as vec2f
  210.  
  211.     dim as vec2f tVector1
  212.     dim as vec2f tVector2
  213.     dim as vec2f vReturn
  214.     dim as single d
  215.     dim as single t
  216.  
  217.     tVector1.X = VPoint.X - Va.X
  218.     tVector1.Y = VPoint.Y - Va.Y
  219.  
  220.     tVector2.X = Vb.X - Va.X
  221.     tVector2.Y = Vb.Y - Va.Y
  222.  
  223.     vec2f_normalize( tVector2 )
  224.  
  225.     d = vec2f_dist( vA, vB )
  226.     t = vec2f_dot( tVector2, tVector1 )
  227.  
  228.     if t<=0 then return Va
  229.  
  230.     if t>=d then return Vb
  231.  
  232.     vReturn.X = Va.X + (tVector2.X * t)
  233.     vReturn.Y = Va.Y + (tVector2.Y * t)
  234.  
  235.     return vReturn
  236.  
  237. end function
  238.  
  239.  
  240. function vec2f_dist( byref va as vec2f, byref vb as vec2f ) as single
  241.  
  242.     dim as single dx
  243.     dim as single dy
  244.  
  245.     dx = va.X - vb.X
  246.     dy = va.Y - vb.Y
  247.  
  248.     return sqr(dx^2+dy^2)
  249.  
  250. end function
  251.  
  252.  
  253. sub vec2f_normalize( byref v as vec2f )
  254.  
  255.     dim as single vLen
  256.  
  257.     vLen = vec2f_len ( v )
  258.  
  259.     v.x /= vLen
  260.     v.y /= vLen
  261.  
  262. end sub
  263.  
  264.  
  265. function vec2f_len( byref v as vec2f ) as single
  266.  
  267.     dim as single tLen
  268.  
  269.     tLen = sqr(v.x^2 + v.y^2)
  270.  
  271.     if tLen = 0 then tLen = 1
  272.  
  273.     return tLen
  274.  
  275. end function
  276.  
  277.  
  278. function vec2f_dot( byref a as vec2f, byref b as vec2f ) as single
  279.  
  280.     return a.x*b.x + a.y*b.y
  281.  
  282. end function
  283.  
  284.  
  285. function vec2f_perp( byref a as vec2f, byref b as vec2f ) as vec2f
  286.  
  287.     dim as single vLen
  288.     dim as vec2f d
  289.  
  290.     d.x = b.x - a.x
  291.     d.y = b.y - a.y
  292.  
  293.     vLen = vec2f_len( d )
  294.  
  295.     return type( d.y  / vLen, -d.x / vLen )
  296.  
  297. end function
  298.  
  299.  
  300.  
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement