March 05, 2021, 07:42:02 AM

Author Topic: [bmx] Simple Path Following by zoqfotpik [ 1+ years ago ]  (Read 477 times)

Offline BlitzBot

[bmx] Simple Path Following by zoqfotpik [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Simple Path Following
Author : zoqfotpik
Posted : 1+ years ago

Description : Just a path following algorithm.  This is the trig version and not the simpler slope version but most applications need the degree value anyway.

Code :
Code: BlitzMax
  1. ' Simple Pathfinding
  2. Strict
  3. Graphics 640,480
  4.  
  5. ' Target X, Target Y
  6. Global tx:Float = 500
  7. Global ty:Float = 200
  8.  
  9. ' Current Position X, Y
  10. Global px:Float = 320
  11. Global py:Float = 240
  12.  
  13. ' Variables related to slope between points
  14. Global slope:Float
  15. Global rise:Float
  16. Global run:Float
  17.  
  18. ' X and Y portions of movement increment
  19. Global xstep:Float
  20. Global ystep:Float
  21.  
  22. ' This is the slope in degrees, converted by ATan() or Arctangent
  23. Global deg:Float
  24.  
  25. Type coord
  26. Field x:Int,y:Int
  27. End Type
  28.  
  29. ' List of path points
  30. Global path:TList = New TList
  31.  
  32. While Not KeyDown(KEY_ESCAPE)
  33.  
  34. ' Change position of target seeker
  35. If MouseHit(1)
  36.         px = MouseX()
  37.         py = MouseY()
  38. EndIf
  39.  
  40. ' Add a New Path Point
  41. If MouseHit(2)
  42.         Local newcoord:coord = New coord
  43.         newcoord.x = MouseX()
  44.         newcoord.y = MouseY()
  45.         path.addlast(newcoord)
  46. EndIf
  47.  
  48. Local target:coord = New coord
  49.  
  50. ' Get an object off the path point list and cast it to Coord
  51. target = Coord(path.first())
  52.  
  53. If target <> Null
  54.         tx = target.x
  55.         ty = target.y
  56. EndIf
  57.  
  58. SetColor 255,255,255
  59. Local thispathpoint:coord = New coord
  60. For thispathpoint = EachIn path
  61.         Plot thispathpoint.x,thispathpoint.y
  62. Next
  63.  
  64. ' Basic right triangle algebra
  65. run = tx - px
  66. rise = ty - py
  67. slope = rise / run
  68.  
  69. ' Get the degree value of the slope
  70. deg = ATan(slope)
  71. If px > tx deg = deg + 180
  72.  
  73. ' Get trigonometric slope value
  74. xstep = Cos(deg)*2
  75. ystep = Sin(deg)*2
  76. ' There is a simpler way of doing all that stuff which doesn't involve trig or the angle
  77. ' but it doesn't give you the desired degree facing and you probably will need that anyway
  78.  
  79.  
  80. ' Update the seeker point's position
  81. px = px + xstep
  82. py = py + ystep
  83.  
  84. ' Are we close enough to the target path point?  If so, remove it
  85. If distance(px,py,tx,ty) < 5
  86.         path.remove(target)
  87. EndIf
  88.  
  89. Print deg
  90. SetColor 255,255,0
  91. Plot px, py
  92. SetColor 255,0,0
  93. Plot tx,ty
  94. Flip
  95. Wend
  96.  
  97. Function distance:Float(x1:Int, y1:Int, x2:Int, y2:Int)
  98. Local d:Float=Sqr((x1 - x2)^2 + (y1 - y2)^2)
  99. Return d
  100. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal