• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Enemy craft not behaving quite like I'd like...

## Recommended Posts

Lohrno    222
I want my enemies to head toward my players'' ship and fire, and what they seem to be doing is circling, or firing in the opposite direction, or displaying erratic behavior. I have code like this:
	if (atan(sin(targetz-z)/cos(targetx-x)) > 0.1f ) heading +=0.2f;
else if (atan(sin(targetz-z)/cos(targetx-x)) < -0.1f ) heading -=0.2f;

Target being the players'' ship. When it is not either of these conditions, it should head in the players'' general direction, and fire. Is there something wrong with this code? -=Lohrno

##### Share on other sites
RPGeezus    216
There is a faster way to do what you want.

You don''t actually care about the angle between the player and the enemy, only if the enemy is facing the player. For this reason you can be more crude in your calculations.

Think about it like this: If you were to draw a ''radar'' screen showing the positions of all of the enmies relative to the player, where where would an enemy appear on the radar if it were directly in front of the player? The answer, depending on how you did your radar, would be due north on the radar screen (x = 0, y > 0). To determine which direction the player should turn to face an enemy, do the opposite of X on the radar.

If I recall correctly this can be calculated with less computation than what you currently have.

You should add a special check to see if the target is behind the entity doing the chasing. If you don''t you will experience bizzare consequences (like the ones you''ve desribed).

Best of luck,
Will

##### Share on other sites
Lohrno    222
Even if the player''s angle and position changes?

-=Lohrno

##### Share on other sites
Lohrno    222
Okay, I figured out what happened...all the ships were 90 degrees rotated for some reason, (PI/2), and instead of atan, I decided to use atan2 which calculates the angle correctly(IE if you''re at 180 degrees it gives you 180, not 0;

Also, the formula for angle is not atan(sin(y)/sin(x)) but just atan(y/x)

-=Lohrno

##### Share on other sites
RPGeezus    216
The players angle and position, in the radar sense, is always relative to the other enemies.

so:
tx = enemyX - playerX;
ty = blah blah blah

rotate tx,ty by players angle + 90 degrees.

Thats all there is to it. No Atan. If resulting x > 0, angle++. x < 0, angle--. x = 0 (or within some threshold) & y > 0, fire!

Whatever. It sounds like you got what you wanted to work. Good luck.

Will

##### Share on other sites
Lohrno    222
Oh...You meant like using code like this:

if(player.x>enemy.x && Player.z > enemy.z) heading+=0.1f; ?
(Do this for all 4 quadrants)

I dont remember why but it didn''t work for me too well. I like the Atan better because I can add a little fudge factor with the angle so they''re not 100% accurate.

Thanks though.

-=Lohrno

##### Share on other sites
RPGeezus    216

You don''t need to check all of the quadrants. if x > 0 angle++, if x < 0 angle--.

You can still add a fudge factor.

ie: if x > 6 angle then angle + 4,
or
ie: angle += x / 2;

You can also make the shots less (or more) accurate)

ie: if ( x > -3 and x < 3) shoot!

The key is to rotate the player about the enemy by the enemies angle + 90 degrees. And make sure the coordiantes are relative to the enemie.

To be honest, the reason I went this route was because the platform I was using didn''t have atan() and I couldn''t afford the memory for a table. lol. I use this method in a game I will be releasing within the next month.

Best of luck,
Will