Chasing but limiting the turning angle
BlitzMax Forums/BlitzMax Beginners Area/Chasing but limiting the turning angle
| ||
I've written a simple chasing behavior for my enemie objects. Basically the player may change direction in incriments of three degrees so i can't just tell the enemies to face and move towards the player. What I've tried to do is make the enemy find the angle to the target x and y (tx,ty) and choose weither to add or subtrack three degrees from the current direction. It seems that when the direction to the player is 0, the eneies start weaving toward the left and ignore the direction to the player. I need to find if direction-3 is closer than direction+3 to the target direction then choose the right action. Method chase(tx#,ty#) ' since the chaser can omly change direction in incriments of three ' find which direction is closer to the target angle direction+3 or direction-3 If direction-point_direction(x,y,tx,ty)<0 direction:+3 End If If direction-point_direction(x,y,tx,ty)>0 direction:-3 End If x:+Cos(direction)*speed y:+Sin(direction)*speed End Method I've made a small file that you can compile to see: ' lets make some chasing missles Strict Graphics 640,480 Function point_direction#(x1#,y1#,x2#,y2#) Local direction#= ATan2(y1-y2,x1-x2)+180 Plot 320,240 Return direction EndFunction Type missle Field x#,y#,direction#,speed# Global missle_list:TList = New TList Function create (x#,y#,direction#,speed#) Local temp:missle = New missle temp.x=x temp.y=y temp.direction = direction temp.speed = 5 ListAddLast(missle_list,temp) End Function Method update() Local target_x#=MouseX() Local target_y=MouseY() Local max_angle#=3 Local target_direction#=point_direction(x,y,target_x,target_y) If direction< target_direction direction:+max_angle EndIf If direction>target_direction direction:-max_angle EndIf x:+Cos(direction)*speed y:+Sin(direction)*speed DrawOval(x-5,y-5,10,10) End Method End Type missle.create(Rand(640),Rand(480),0,3) While Not KeyDown (KEY_ESCAPE) For Local i:missle = EachIn(missle.missle_list) i.update() Next DrawText(point_direction(320,240,MouseX(),MouseY()),12,12) Flip Wend |
| ||
If direction-point_direction(x,y,tx,ty)<0 direction:+3 End If If direction-point_direction(x,y,tx,ty)>0 direction:-3 End If You are checking for greater than 0 or less than 0, never 0. try this function, I've used it and it works fine Function RotateToPoint(originalAngle:Int,targetAngle:Int,turnspeed:Float) Local angleDiff angleDiff = originalAngle - targetAngle If angleDiff < 0 Then angleDiff = 360 + angleDiff If angleDiff > 359 Then angleDiff = 360 - angleDiff If angleDiff > 180 Then Return turnspeed If angleDiff < 181 Then Return -turnspeed End Function Just pass your original angle, target angle and how fast you want them to turn and it returns the angle change required. |
| ||
Thanks a million . :) |