-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|468|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- Socoder -> Blitz Max -> cars turning Posted : Thursday, 09 September 2010, 16:52 Teasy ;------------------------------------------------------------------------------- ; independent functions ; return relative angle difference (takes angle wrapping into account) ; - max angle difference: 359 degrees Function AngleDifference( CurrentAngle , TargetAngle ) If TargetAngle - CurrentAngle > 180 Then TargetAngle = TargetAngle - 360 If CurrentAngle - TargetAngle > 180 Then CurrentAngle = CurrentAngle - 360 Return CurrentAngle - TargetAngle End Function ; easy to use trigonometry conversion functions (cartesian/polar) ; - with a '0 degrees means up' orientation Function VectorX# ( Distance# , Angle# ) Return Sin ( Angle ) * Distance End Function Function VectorY# ( Distance# , Angle# ) Return Sin ( Angle - 90 ) * Distance End Function Function VectorDistance# ( X# , Y# ) Return Sqr ( X * X + Y * Y ) End Function Function VectorAngle# ( X# , Y# ) Return -ATan2 ( X , Y ) + 180 End Function ;------------------------------------------------------------------------------- ; fundamental preparation Const sw = 320 Const sh = 240 Graphics sw,sh , 0,3 SetBuffer BackBuffer() SeedRnd MilliSecs() ;------------------------------------------------------------------------------- ; variable declaration ; tank Local x#,y# ; current position Local a ; current angle ; path Local nx,ny ; new position Local vd,va ; vector [distance/angle] from current to new position Local dif ; relative difference between current and target angle ;------------------------------------------------------------------------------- ; variable initialization ; center tank and aim up x = sw/2 y = sh/2 a = 0 ; choose a new (random) position to move to ; - in a somewhat center part of the screen nx = Rand( sw/4,sw*3./4-1 ) ny = Rand( sh/4,sh*3./4-1 ) ;------------------------------------------------------------------------------- ; main loop Repeat ; calculate distance and angle ; - from current position to new position vd = VectorDistance( nx-x , ny-y ) va = VectorAngle( nx-x , ny-y ) ; slowly rotate the tank ; - in the direction of the new position ; - by checking if left or right is nearer dif = AngleDifference( a , va ) a = a - Sgn( dif ) * 3 ; <-- rotation speed is 3 pixels per frame ; keep angle in legal range If a < 0 Then a = a + 360 If a >= 360 Then a = a - 360 ; if distance is small If vd < 20 ; choose a new (random) position to move to nx = Rand( sw/4,sw*3./4-1 ) ny = Rand( sh/4,sh*3./4-1 ) ; if first mouse button pressed ElseIf MouseHit(1) ; make the location of the mouse the new position to move to nx = MouseX() ny = MouseY() End If ; randomly alter target position nx = nx + Rand( -1,1 ) ny = ny + Rand( -1,1 ) ; move tank x = x + VectorX( 1 , a ) ; <-- tank speed is 1 pixels per frame y = y + VectorY( 1 , a ) ; draw tank DrawTank x,y , 20,40 , a ; show target position marker Color 255,192,128 Oval nx-10,ny-10 , 20,20 , False ; show bounding rectangles for screen and target position area Color 128,96,64 Rect 0,0 , sw,sh , False Rect sw/4,sh/4 , sw/2,sh/2 , False Color 224,224,192 Text 0,0 , "Mouse button 1: Set target position" Flip Cls Delay 5 Until KeyHit(1) End ;------------------------------------------------------------------------------- ; custom functions Function DrawTank( cx,cy , w,h , angle ) ; Param cx,cy ; center position ; Param w,h ; dimensions (width,height) ; Param angle ; (0=up) Local x[5],y[5] ; points for outer shape Local d[5],a[5] ; points as vector (distance,angle) Local p ; point number iterator ; top left x[1] = -w/2 y[1] = -h/2 ; top right x[2] = +w/2 y[2] = -h/2 ; bottom left x[3] = -w/2 y[3] = +h/2 ; bottom right x[4] = +w/2 y[4] = +h/2 ; tip of the turret x[5] = 0 y[5] = -h/2-h/3 ; for all points For p = 1 To 5 ; convert point to vector d[p] = VectorDistance( x[p],y[p] ) a[p] = VectorAngle( x[p],y[p] ) ; rotate vector a[p] = a[p] + angle ; and convert vector back to point x[p] = VectorX( d[p],a[p] ) y[p] = VectorY( d[p],a[p] ) ; move point to desired (screen) offset x[p] = x[p] + cx y[p] = y[p] + cy Next ; draw points (shape) Color 64,96,128 Line x[1],y[1] , x[2],y[2] Line x[2],y[2] , x[4],y[4] Line x[4],y[4] , x[3],y[3] Line x[3],y[3] , x[1],y[1] ; draw turret Color 96,144,192 Oval cx-h/4,cy-h/4 , h/2,h/2 , False Color 128,192,255 Line cx,cy , x[5],y[5] End Function --v Posted : Tuesday, 12 October 2010, 12:58 Teasy Feel free to ask for more help if needed