Super thanks,
Grey AlienI have not figured it out how to make it correct, would you mind helping me??
I hope you have the strength with me.
Here is what i achieved so far...well the images is a problem. Look in the Function "UpdateTank1(t.Tank)" there the problem is ???.
Graphics 640,480
SetBuffer BackBuffer()
Type Tank
Field x#,y#,rot%
Field speed%, moveable
End Type
Global tTank1.Tank = CreateTank.Tank(True,200,200)
Global tTank2.Tank = CreateTank.Tank(False,320,240)
Global iNumRotation = 36, fAngle#
Dim fXSinTable#(iNumRotation), fYCosTable#(iNumRotation)
Dim hImages(iNumRotation)
CreateImages("Bitmaps\tank1.bmp",iNumRotation)
PrecomputeSinCosTables()
; Main.^^^^^^^^^^^^^^^^^^^^^^
While Not KeyHit(1)
Cls
UpdateTank1(tTank1)
UpdateTank1(tTank2)
Flip
Wend
CleanUpImages()
EndGraphics()
End
;**************************************************************
;**** Functions ***********************************************
Function CreateTank.Tank(bMove, iX, iY)
t.Tank = New Tank
t\moveable = bMove
t\x# = iX
t\y# = iY
Return t
End Function
Function PrecomputeSinCosTables()
Local iAngle = 0
; run through the full rotation cycle, and use Sin and Cos
; at Multiplyer increments, ie. 360/iNumRotation = 10
Local iMultiplyer = 360 / iNumRotation
For iAngle = 0 To iNumRotation - 1
fXSinTable#(iAngle) = Sin(iAngle * iMultiplyer)
fYCosTable#(iAngle) = -Cos(iAngle * iMultiplyer)
Next
End Function
;******************************************************************
Function CreateImages(sImage$,iNumRotation)
AutoMidHandle True
Local hTemp = LoadImage(sImage$)
If hTemp = 0 Then RuntimeError "Can't find image - load error!!"
MaskImage hTemp,255,0,255
Local iMultiplyer = 360 / iNumRotation
; rotate image to generate all directions
For iLoop = 0 To iNumRotation - 1
; first copy the original image into the current frame
hImages(iLoop)=CopyImage( hTemp )
; rotate the frame the appropriate number of degrees
RotateImage hImages(iLoop), iLoop * iMultiplyer
Next
FreeImage hTemp : hTemp = 0
End Function
;**********************************************************************
Function UpdateTank1(t.Tank)
If t\moveable Then
If KeyDown(205) Then t\rot = t\rot + 1 ; Right
If KeyDown(203) Then t\rot = t\rot - 1 ; Left
If t\rot > 35 Then t\rot = 0
If t\rot < 0 Then t\rot = 35
t\x# = t\x# + (fXSinTable#(t\rot)); * Speed)
t\y# = t\y# + (fYCosTable#(t\rot)); * Speed)
If t\x# <= 0 Then t\x# = 0
If t\y# <= 0 Then t\y# = 0
If t\x# > 620 Then t\x# = 620
If t\y# > 460 Then t\y# = 460
Text 10,60,"Tank1 rotation: " + t\rot
DrawImage hImages(t\rot),t\x#, t\y#
Else
Local iTile = UpdateTrigonometry()
Text 10,30,"Tank2 Tilenumber: " + iTile
DrawImage hImages(iTile), t\x, t\y
End If
End Function
;***************************************************************************
Function UpdateTrigonometry()
Local iTile, fUnit#
; add 180, or else you got an value between -180 and 180.
fAngle# = ATan2(tTank2\y# - tTank1\y# , tTank2\x# - tTank1\x#) + 180
Text 10,10,"Angle: " + fAngle#
Text 10,20,"cos: " + Cos(fAngle#)
fUnit# = fAngle# / 360
iTile = Ceil( fUnit# * iNumRotation ) - 1
Return iTile
End Function
;****************************************************************************
Function CleanUpImages()
For iLoop = 0 To iNumRotation-1
FreeImage hImages(iLoop)
Next
End Function