Hi everyone:
lately I have been working on creating my own chasser missiles for a game. I think what I got is somewhat decent. The problem is, if I try to make it faster, it does not give me the responce I want. I want to make it move faster but with the same small radius turn. I have provided the code and executable. I apreciate a fix, alternative solution, or a better algorith.
SuperStrict
Framework BRL.GLMax2D
Import BRL.Random
Const Acceleration# = 00.5
Const TopSpeed# = 05.0
Const TurnAcceleration# = 01.0
Const TurnMax# = 10.0
Local Missiles% = 30
Local Missile:TMisile[Missiles] ' create missiles
Local streak:tstreak[Missiles] ' create streak for missiles
Graphics 800,600,32,60
Global width% = GraphicsWidth()
Global height% = GraphicsHeight()
SetBlend alphablend
glEnable(GL_LINE_SMOOTH) 'Quick antaliasing hack
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)
glLineWidth(3.0)
' create missiles and streaks.
For Local i% = 0 To Missiles-1
Missile[i] = TMisile.create(Rand(700)+50,Rand(500)+50,Rand(360))
streak[i] =tstreak.create(Missile[i].x,Missile[i].y,Rand(255),Rand(255),Rand(255))
Next
SeedRnd MilliSecs()
Local z% = 0 'A dirty speed hack
Repeat
SetColor 20,200,40
DrawText "Press (esc) to exit",300,30
For Local i% = 0 To Missiles-1
Missile[i].update(MouseX(),MouseY())
streak[i].add(Missile[i].x,Missile[i].y)
streak[i].draw()
Next
If z Flip() ' Dirty speed hack by skipping a sync cycle
Cls
z=1-z 'dirty speed hack
Until KeyHit(key_escape)
'
' Resources
'-----------------------------------------------------------------------------------
'
Type Tpoint
Field x#
Field y#
End Type
' used to draw a streak.
'Note: does become jugged If points are separated To far And
' angles are set sharp.
Type Tstreak
Field list:TList
Field lastpoint:tpoint
Field active%
Field Red%,Green%,Blue%
Function create:tstreak(x#,y#,Red%,Green%,Blue%)
Local s:tstreak = New tstreak
Local p:tpoint = New tpoint
If Not s.list Then
s.List = CreateList()
s.lastpoint = New tpoint
s.active = True
EndIf
p.x = x
p.y = y
s.list.addlast(p)
s.lastpoint.x = p.x
s.lastpoint.y = p.y
s.Red = Red
s.Green = Green
s.Blue = Blue
Return s
End Function
' adds a segment to a streak.
Method add(x#,y#)
If list.count()>50 active = False; Return
Local p:tpoint = New tpoint
p.x = x
p.y = y
list.addlast(p)
lastpoint.x = p.x
lastpoint.y = p.y
End Method
'
Method draw%()
If Not list.count() Return -1
If list.count() > 1 Then
Local Alpha# = 0.0
Local p1:Tpoint = Tpoint(list.first())
SetColor Red,Green,Blue
For Local p2:tpoint = EachIn list
If p2<>p1 Then
SetAlpha alpha
DrawLine(p1.x,p1.y,p2.x,p2.y,False)
p1 = p2
EndIf
alpha :+.02
Next
EndIf
If Not active list.remove(list.first())
End Method
End Type
Type TMisile
Field Red%
Field Green%
Field Blue%
Field x#,y#
Field DirectionX#,DirectionY#
Field XS#,YS#
Field Done:Int
Field Degree#
Field Direction#
Field TurnSpeed#
Field GoLeft%
Field GoRight%
Function create:TMisile(x#,y#,dir#)
Local s:TMisile = New TMisile
s.x = x
s.y = y
s.Direction = Dir
s.Done = False
s.Red = 200
s.Green = 0
s.Blue = 0
Return s
End Function
'Used To Draw Missile (currently unused)
Method draw() ' used to draw missile
' use from field in type
' x, y -- is the current missile position also used for head of streak
'direction -- is the angle the missile is facing in degrees.
End Method
' Target chassing logic
Method Update%(nx%,ny%)
'Set acceleration
XS:+ Cos(Direction)*Acceleration
YS:+ Sin(Direction)*Acceleration
Local CurrentSpeed# = Sqr(XS*XS + YS*YS)
If CurrentSpeed > TopSpeed
XS:+ (XS/CurrentSpeed)*(TopSpeed - CurrentSpeed)
YS:+ (YS/CurrentSpeed)*(TopSpeed - CurrentSpeed)
EndIf
X:+ XS
Y:+ YS
'Set Rotation
Local distance# = Sqr((x-nx)^2+(y-ny)^2) '
Local TargetAngle# = (ATan2(y-ny,x-nx)+180.0) Mod 360.0
Local difference# = TargetAngle-Direction
'turn toward target
If TargetAngle < Direction
If Abs(difference) > 180.0 TurnSpeed:+TurnAcceleration Else TurnSpeed:-TurnAcceleration
ElseIf TargetAngle > Direction
If Abs(difference) > 180.0 TurnSpeed:-TurnAcceleration Else TurnSpeed:+TurnAcceleration
EndIf
'If found stop turning
If Abs(difference) < 1.0 TurnSpeed = 0.0
'Limit TurnSpeed
If TurnSpeed > TurnMax TurnSpeed = TurnMax
If TurnSpeed < -TurnMax TurnSpeed = -TurnMax
Direction = (Direction+TurnSpeed+360) Mod 360
EndMethod
End Type