December 04, 2020, 11:23:47 AM

Author Topic: [bb] 2D follow image/smooth waypoints by Ross C [ 1+ years ago ]  (Read 447 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : 2D follow image/smooth waypoints
Author : Ross C
Posted : 1+ years ago

Description : Modified from my 2d waypoint editor. You create waypoints by clicking. Press SPACE to start the image following the way points. Press Enter to stop. You can add waypoints at any time you wish.

Basically, if you say create an attack pattern for a spaceship in 2D, you don't want it to follow exactly. Instead you want it to curve round them. This is what this code does. Run and see for your self :)


Code :
Code: BlitzBasic
  1. ; **** 2D waypoint editor, modified. Original Code by Ross C *****
  2. ; ****                 Modified code by Ross C               *****
  3. ; ****     create smooth transitions between way points      *****
  4.  
  5.  
  6. Graphics 800,600
  7. SetBuffer BackBuffer()
  8.  
  9. Global max_path_points=100
  10.  
  11. Dim pathx#(max_path_points-1); set up 101 path points
  12. Dim pathy#(max_path_points-1); -1 because arrays start at zero
  13.  
  14. Global path_counter=-1
  15. Global current_path=0
  16. Global cx#=0
  17. Global cy#=0
  18. Global ex#=400
  19. Global ey#=100
  20. Global movement_started=0
  21.  
  22.  
  23. While Not KeyHit(1)
  24.         Cls
  25.        
  26.         If MouseHit(1) Then
  27.                         setpath(MouseX(),MouseY()); set path point with the mouses current coords
  28.         End If
  29.        
  30.         If KeyHit(57) And movement_started=0 And path_counter>0 Then; if spacebar pressed then move rectangle
  31.                 current_path=0; reset the current path to 0
  32.                 cx=pathx(0); set the rectangles coords to the first path point
  33.                 cy=pathy(0)
  34.                 movement_started=1; set flag for movement started
  35.                 ex=400
  36.                 ey=100
  37.         End If
  38.         If KeyHit(28) Then; if the enter key is pressed then stop movement
  39.                 movement_started=0; clear flag for movement
  40.                 cx=pathx(0); set rectangle coords to the first path point
  41.                 cy=pathy(0)
  42.                 current_path=-1; set current path to none exsistant
  43.         End If
  44.  
  45.        
  46.        
  47.        
  48.         draw_path_points(); function for drawing the pathpoints
  49.         If movement_started=1 Then move(); draw the moving rectangle and move it
  50.         Color 200,200,200; set drawing color
  51.         Rect MouseX()-1,MouseY()-1,2,2; draw mouse location with a rectangle
  52.         Text 0,0,"Click mouse to set path point. Press spacebar to start animation. Press enter to stop."
  53.         Text 0,10,"movement started="+movement_started
  54.         Flip
  55. Wend
  56. End
  57.  
  58. Function move()
  59.         ang#=0
  60.         If current_path<path_counter Then; make sure the current path point is less than the highest
  61.                 ang=ATan2(pathy(current_path+1)-pathy(current_path),pathx(current_path+1)-pathx(current_path))
  62.                 ;/ basically means find the angle between the current path point and the next one /
  63.                 cx=cx+Cos(ang)*3; increase the x coord of the rect following the path points based on the angle
  64.                 cy=cy+Sin(ang)*3; increase the y coord of the rect following the path points based on the angle
  65.                 Color 100,200,100; change drawing color
  66.                 Rect cx-3,cy-3,6,6; draw the rectangle central to it's coords
  67.                 If Abs(cx-pathx(current_path+1))<2 And Abs(cy-pathy(current_path+1))<2 Then
  68.                         ; / if the rects coords are with a 1 pixel distance of the path point then /
  69.                         current_path=current_path+1; increase the current path point number
  70.                         cx=pathx(current_path); set the rectangles coords to that of the next path point
  71.                         cy=pathy(current_path)
  72.                 End If
  73.         End If
  74.         angle=ATan2( cy - ey, cx - ex ); find the angl between the enemies and the waypoint follower
  75.         dist=Sqr( (ex-cx)*(ex-cx)+(ey-cy)*(ey-cy) ); find the distance between them
  76.         DebugLog dist
  77.         ex=ex+(Cos(angle)/1)*(dist/30); move the enemy at speed relative to the distance
  78.         ey=ey+(Sin(angle)/1)*(dist/30); move the enemy at speed relative to the distance
  79.         Rect ex-15,ey-15,30,30; draw the enemy
  80. End Function
  81.  
  82. Function setpath(x,y)
  83.         path_counter=path_counter+1; increase counter
  84.         If path_counter>max_path_points-1 Then; if counter is higher than the available number of path points then set it back
  85.                path_counter=max_path_points-1
  86.         Else; else
  87.                pathx(path_counter)=x; record the x and y positions
  88.                pathy(path_counter)=y
  89.         End If
  90. End Function
  91.  
  92. Function draw_path_points()
  93.         For loop=0 To path_counter; loop through all the path points
  94.                 Color 100,100,200
  95.                 Rect pathx(loop),pathy(loop),2,2
  96.                 If loop<path_counter Then; do this loop while the loop value is less than the highest path value
  97.                         Rect pathx(loop+1)-1,pathy(loop+1)-1,2,2; draw a rectangle at the path point so its central
  98.                         Color 50,50,150; change drawing color
  99.                         Line pathx(loop),pathy(loop),pathx(loop+1),pathy(loop+1); draw a line between the current point and the next one
  100.                 End If
  101.         Next
  102. End Function


Comments :


Rck(Posted 1+ years ago)

 A novel idea, at least to myself.


Pineapple(Posted 1+ years ago)

 Thanks for this, just what I was looking for, though, I've shortened it a little, and used types instead of arrays:-
Code: [Select]
Graphics 800,600
SetBuffer BackBuffer()

Type moveObject
Field x,y
Field px,py
End Type


While Not KeyHit(1)
Cls

If MouseHit(1) Then
setpath(MouseX(),MouseY(),400,100)
End If

move()
Color 200,200,200
Rect MouseX()-1,MouseY()-1,2,2
Rect 400,100,2,2
Flip
Wend
End


Function move()
For loopObjects.moveObject = Each moveObject
ang#=0
ang=ATan2(loopObjectspy-loopObjectsy,loopObjectspx-loopObjectsx)
loopObjectsx=loopObjectsx+Cos(ang)*3
loopObjectsy=loopObjectsy+Sin(ang)*3
Color 100,200,100
Rect loopObjectsx-3,loopObjectsy-3,6,6
If Abs(loopObjectsx-loopObjectspx)<2 And Abs(loopObjectsy-loopObjectspy)<2 Then
Delete loopObjects
End If
Next
End Function


Function setPath(x,y,futureX,futureY)
createPath.moveObject = New moveObject
createPathx = x
createPathy = y
createPathpx = futureX
createPathpy = futureY
End Function

Dabz [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal