enemy facing

BlitzMax Forums/BlitzMax Programming/enemy facing

Cruis.In(Posted 2013) [#1]
Hey guys, got some trouble with angles again.

2d space.

I am attempting to compute the angle of the player from an enemy. Taking into account the enemy's current rotation or facing.

The enemy can rotate 360.

the code is just being tested now to get the ai function of turning to face the player correct.


the rotation of the enemy entity is set to e.angle.

this works to rotate the enemy left, until he is facing you. When difference = 0 the enemy stops rotating.

I need to turn the enemy based on the smallest angle regards his forward facing direction and the angle of the player. So that he doesn't rotate 330 degrees left, when he could simply have rotated 30 degrees right, so to be facing the enemy.

I had it to the point where the enemy would rotate to face you as i moved to the left and right of his facing vector (crossed his face), he would start to turn to face either way in stead of going all around. Which is good. But somehow when I got around the back end, it would mess up when I got very nearly 180 degrees behind him, he would start rotating from the far side.

I've tried so many different combinations and I know I am over looking something simple, but I couldn't get it fully right.

I've checked if the difference was 180 or -180 or if it was between 180 and 360 and between 0 and 180.

I thought that made sense, since I am checking each half, and whichever half is closer, I either + or - the rotation of the enemy facing angle.

Any advice on what I have overlooked is appreciated thanks!

Last edited 2013


Cruis.In(Posted 2013) [#2]


The difference when I draw it on screen goes between 1-179 and -1 to -179

If I am on the above side the left of the enemy entity its positive, and if I am on the starboard side its negetive.

Now what above code does is work perfect, fully rotates to face the player, if I switch over, it always starts to turn from the smallest angle which is good.

Only thing is, it is assuming the aft end is the front, meaning the front isn't actually whats facing you when done lol.

I suspect it is to do with the whole -180, + 180 , since the enemy angle rotates from 0-360 and doesn't split in half like the calculated difference. If i could get the difference to read full 360, then check between 0-180 and 180-360, it would actually rotate till the front faced, and it would still have the working intelligence of which way is the shortest to go in terms of degrees.


Cruis.In(Posted 2013) [#3]
Just so you guys know that I am feverishly working to overcome my terrible understanding of math, I only ask for help when at wits end...but I keep trying. That's why I update my posts with my own thoughts, seeing my problem above with the 180 - 180 thing, I knew I had to get it to go from 0 - 360.

So I did this:



So we're getting the 360 now.

Then to get it to turn left or right I say turn right or left depending on if the angle is more than the upper half of 360, or the lower half of 360.



So now its all calculating 0-360 everyone is on the same playing field and it turns to face depending on which way is shorter.

I know my math is horrible, but it's hard for people who never grew up with that education in math. If you've lived it for years it comes natural to you, some people it doesn't. But I keep trying and haven't quit it.

I think I sorted this problem for now

Thanks for reading!

p.s
I have to report that although no enemy entities were harmed in the solving of this issue, the enemy entity has complained that he is very dizzy.

Last edited 2013


Nest(Posted 2013) [#4]
Just checked the code I have in my game, and it's:

rot = ATan2(target.x - x, y - target.y)+180

If your range is -180 to 180, you could have a +180 modifier to go from 0 to 360.


GfK(Posted 2013) [#5]
Just checked the code I have in my game, and it's:

rot = ATan2(target.x - x, y - target.y)+180
Not sure how that's working. You've got the X and Y parameters the wrong way around. It should be ATan2(y,x).

Last edited 2013


Nest(Posted 2013) [#6]
I don't think it really matters which way round you do it, provided your code is aligned to it.

Last edited 2013


GfK(Posted 2013) [#7]
I don't think it really matters which way round you do it, provided your code is aligned to it.
That isn't what your pre-edit said. It said it was because you preferred rotations to be clockwise, which using Atan2 correctly, they are.

If they aren't, you've got something wrong elsewhere in your code. Two wrongs don't make a right - they make two wrongs.


Nest(Posted 2013) [#8]
I edited it immediately because yes, I was wrong.

However, my code works perfectly fine as it is. Whether the rotation is clockwise or counter-clockwise and where the origin 0 degrees lies really has no "right" or "wrong" answer - it is down to personal preference. For example this code shows all the permutations (again, personal preference to +180 to keep it 0-360):

Graphics 640, 480
x=320
y=240
HideMouse()
Repeat
Cls
SetColor 0,255,0
DrawOval(x,y,5,5)
DrawOval(MouseX(),MouseY(),5,5)
DrawLine(x,y,MouseX(),MouseY())
SetColor 255,255,0
DrawText Int(ATan2(MouseX()-x,y - MouseY())+180),MouseX(),MouseY()
DrawText Int(ATan2(MouseX()-x,MouseY()-y)+180),MouseX(),MouseY()+10
DrawText Int(ATan2(x-MouseX(),y - MouseY())+180),MouseX(),MouseY()+20
DrawText Int(ATan2(x-MouseX(),MouseY()-y)+180),MouseX(),MouseY()+30
SetColor 255,0,255
DrawText Int(ATan2(MouseY()-y,x - MouseX())+180),MouseX(),MouseY() +40
DrawText Int(ATan2(MouseY()-y,MouseX()-x)+180),MouseX(),MouseY()+50
DrawText Int(ATan2(y-MouseY(),x - MouseX())+180),MouseX(),MouseY()+60
DrawText Int(ATan2(y-MouseY(),MouseX()-x)+180),MouseX(),MouseY()+70
Flip
Until KeyHit(KEY_ESCAPE) Or AppTerminate()


Also, here's my code in action (49 sec vid) ... you can see the rotation to the mouse of the player, and also the enemies facing the player when they target him.



I absolutely concede that you are a vastly superior and experienced programmer, but surely you can see my point?


Cruis.In(Posted 2013) [#9]
I've also seen instances where the reverse use worked. But it was because everything else was reversed to accommodate it.

What did you think of my process to solve my issue gfk? :)