Reputation: 108
Recently I've been watching Flash video: "Lynda.com - ActionScript 3.0 Projects: Game Development" with Todd Perkins.
Last lesson was "creating a Flash Tank Game". I was following along and created the exact same game that was described in the lesson.
But in the lesson's example enemy tank had no descent AI. Enemy was moving and shooting randomly, and it didn't seem to be a lot fun playing this game.
I've decided to make AI to be more enhanced. I've managed to make enemy change direction of movement if it was hit by player.
If an enemy successfully hits the player it stops and continues to shoot player with the same gun angle and same position until he missed or got hit by player.
The problem is that formula for calculating right gun angle isn't correct enough. Enemy kind of shoots right, but he still misses a lot.
So, here is the formula that I've used:
enemyTargetRotation=Math.asin(Math.abs(enemy_mc.y - player_mc.y)/(Math.sqrt(Math.pow(player_mc.x - enemy_mc.x, 2)+ Math.pow((enemyWall_mc.height - stage.height),2)))/180*Math.PI);
enemyTargetRotation*=10000;
enemyTargetRotation+=180;
Upvotes: 1
Views: 361
Reputation: 9897
Why not just Math.atan2(y2 - y1, x2 - x1) / Math.PI * 180
? It should give shooting angle in degrees given x1,y1 coordinates of tank and x2,y2 coordinates of target.
Upvotes: 1