January 15, 2021, 06:31:45 PM

Author Topic: [bmx] Guided missile trail by Jesse [ 1+ years ago ]  (Read 398 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Guided missile trail by Jesse [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Guided missile trail
Author : Jesse
Posted : 1+ years ago

Description : I have been tring to create a better guided missile logic but do to my lack of physics knowledge  I have hit a wall. what I am pleased with, is the trail it creates. it's a smooth trail that fades out. Any way I hope somebody finds use for it.  If somebody wants to improve the guiding logic I would  much apreciate if it is posted here.

Code :
Code: BlitzMax
  1. SuperStrict
  2. Framework BRL.GLMax2D
  3. Import BRL.Random
  4.  
  5.  
  6.  
  7. Const Acceleration#     = 00.5
  8. Const TopSpeed#                 = 05.0
  9. Const TurnAcceleration# = 01.0
  10. Const TurnMax#          = 10.0
  11. Const MaxPoints#                = 50.0
  12. Const MissileSpeed%     = 2
  13. Local Missiles% = 20
  14. Local Missile:TMisile[Missiles] ' create missiles
  15. Local streak:tstreak[Missiles]  ' create streak for missiles
  16.  
  17.  
  18. Graphics 800,600,32,60
  19. Global width% = GraphicsWidth()
  20. Global height% = GraphicsHeight()
  21.  
  22. SetBlend alphablend
  23. glEnable(GL_LINE_SMOOTH)        'Quick antaliasing hack
  24. glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)
  25. glLineWidth(3.0)
  26.  
  27. ' create missiles and streaks.
  28.  
  29. For Local i% = 0 To Missiles-1
  30.         Missile[i] = TMisile.create(Rand(700)+50,Rand(500)+50,Rand(360))
  31.         streak[i] =tstreak.create(Missile[i].x,Missile[i].y,Rand(255),Rand(255),Rand(255))
  32. Next
  33.  
  34. SeedRnd MilliSecs()
  35.  
  36. Repeat
  37.         SetColor 20,200,40
  38.         DrawText "Press (esc) to exit",300,30
  39.         For Local i% = 0 To Missiles-1
  40.                 For Local s% = 1 To MissileSpeed
  41.                         Missile[i].update(MouseX(),MouseY())
  42.                         streak[i].add(Missile[i].x,Missile[i].y)
  43.                 Next
  44.                 streak[i].draw()
  45.         Next
  46.         Flip()
  47.         Cls
  48. Until KeyHit(key_escape)
  49.  
  50.  
  51. '
  52. '                               Resources
  53. '-----------------------------------------------------------------------------------
  54. '
  55.  
  56. Type Tpoint
  57.         Field x#
  58.         Field y#
  59. End Type
  60.  
  61. ' used to draw a streak.
  62. 'Note:  does become jugged If points are separated To far And
  63. '               angles are set sharp.
  64.  
  65. Type Tstreak
  66.         Field PointList:TList
  67.         Field lastpoint:tpoint
  68.         Field active%
  69.         Field Red%,Green%,Blue%
  70.         Field Tint#
  71.         Function create:tstreak(x#,y#,Red%,Green%,Blue%)
  72.                 Local s:tstreak = New tstreak
  73.                 Local p:tpoint  = New tpoint
  74.                 If Not s.PointList Then
  75.                         s.PointList      = CreateList()
  76.                         s.lastpoint = New tpoint
  77.                         s.active    = True
  78.                 EndIf
  79.                 p.x = x
  80.                 p.y = y
  81.                 s.PointList.addlast(p)
  82.                 s.lastpoint.x = p.x
  83.                 s.lastpoint.y = p.y
  84.                 s.Red    = Red
  85.                 s.Green  = Green
  86.                 s.Blue   = Blue
  87.                 s.tint  = 1.0 /MaxPoints
  88.                 Return s
  89.         End Function
  90.         ' adds a segment to a streak.
  91.        
  92.         Method add(x#,y#)
  93.                 If PointList.count()<Int(MaxPoints)
  94.                         Local p:tpoint = New tpoint
  95.                         p.x = x
  96.                         p.y = y
  97.                         PointList.addlast(p)
  98.                         lastpoint.x = p.x
  99.                         lastpoint.y = p.y
  100.                 Else
  101.                         PointList.remove(PointList.first())
  102.                 EndIf
  103.         End Method
  104.        
  105.         '
  106.        
  107.         Method draw()
  108.                
  109.                 If Not PointList.count() Return        
  110.                 If PointList.count() > 1 Then
  111.                         Local Alpha# = 0.0
  112.                         Local p1:Tpoint = Tpoint(PointList.first())
  113.                         SetColor Red,Green,Blue
  114.                         For Local p2:tpoint = EachIn PointList
  115.                                 If p2<>p1 Then
  116.                                         SetAlpha alpha
  117.                                         DrawLine(p1.x,p1.y,p2.x,p2.y,False)
  118.                                         p1 = p2
  119.                                 EndIf
  120.                                 alpha :+Tint
  121.                         Next           
  122.                 EndIf
  123.         End Method
  124. End Type
  125.  
  126.  
  127. Type TMisile    
  128.         Field Red%
  129.         Field Green%
  130.         Field Blue%
  131.         Field x#,y#
  132.         Field DirectionX#,DirectionY#
  133.         Field XS#,YS#
  134.         Field Done:Int
  135.         Field Degree#
  136.         Field Direction#
  137.         Field TurnSpeed#
  138.         Function create:TMisile(x#,y#,dir#)
  139.        
  140.                 Local s:TMisile = New TMisile
  141.                 s.x = x
  142.                 s.y = y
  143.                 s.Direction = Dir
  144.                 s.Done = False
  145.                 Return s
  146.                
  147.         End Function
  148.        
  149.         'Used To Draw Missile (currently unused)
  150.        
  151.         Method draw() ' used to draw missile
  152.         ' use from field in type
  153.         ' x, y -- is the current missile position also used for head of streak
  154.         'direction -- is the angle the missile is facing in degrees.
  155.         End Method
  156.        
  157.         ' Target chassing logic
  158.        
  159.         Method Update%(nx%,ny%)
  160.                
  161.                 'Set acceleration
  162.        
  163.                 XS:+ Cos(Direction)*Acceleration
  164.                 YS:+ Sin(Direction)*Acceleration
  165.                
  166.                 Local CurrentSpeed# = Sqr(XS*XS + YS*YS)
  167.                
  168.                 If CurrentSpeed > TopSpeed
  169.                         XS:+ (XS/CurrentSpeed)*(TopSpeed - CurrentSpeed)
  170.                         YS:+ (YS/CurrentSpeed)*(TopSpeed - CurrentSpeed)
  171.                 EndIf
  172.                
  173.                 X:+ XS
  174.                 Y:+ YS
  175.                                
  176.                 'Set Rotation
  177.                                                                                
  178.                 Local distance# = Sqr((x-nx)^2+(y-ny)^2) '
  179.                 Local TargetAngle# = (ATan2(y-ny,x-nx)+180.0) Mod 360.0        
  180.                 Local difference# = Abs(TargetAngle-Direction)
  181.  
  182.                 'turn toward target
  183.  
  184.                 If TargetAngle < Direction                             
  185.                         If difference > 180.0 TurnSpeed:+TurnAcceleration Else TurnSpeed:-TurnAcceleration
  186.                 ElseIf TargetAngle > Direction
  187.                         If difference > 180.0 TurnSpeed:-TurnAcceleration Else TurnSpeed:+TurnAcceleration
  188.                 EndIf
  189.                
  190.                 'If found stop turning
  191.                
  192.                 If difference < 1.0 TurnSpeed = 0.0                    
  193.                                
  194.                 'Limit TurnSpeed
  195.  
  196.                 If TurnSpeed >  TurnMax TurnSpeed =  TurnMax
  197.                 If TurnSpeed < -TurnMax TurnSpeed = -TurnMax
  198.                 Direction = (Direction+TurnSpeed+360) Mod 360
  199.                
  200.         EndMethod
  201. End Type


Comments :


PGF(Posted 1+ years ago)

 Neat.  More like constant speed point following.I have done missile turn and thrust to intersect a moving target with realistic physics including gravity of planets and stars.The missile control wants to reach the target object at a given relative speed so may actually be boosting at an angle or slowing down in order to achieve this goal.If I get some free time I'll create a demo.Anyway your trails are nice.


Jesse(Posted 1+ years ago)

 > More like constant speed point following.yea you are right. But that can be fixed easily. I am looking forward to seeing that demo.Thank you for your comments.P.S:  I purposely left out a test. that is why it gives a nonrepetitive circular turn.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal