How do I make an A.I. for this enemy?

0 favourites
  • 9 posts
From the Asset Store
Pixel Enemies for SHMUP consists of 45 enemy ship sprites to be used in your game.
  • Hello! Could you help me?

    I am making a singleplayer tank game (image below), and I need to apply a good solution for the enemy tank action. It needs to fire a projectile describing a parabola with a certain probability of hiting the player. It would be nice if it could walk randomly also. The projectile has physics behavior.

    I tried several things, but I havent get even close to it.

  • Hiya,

    I can't say I'm an expert on AI, but I think you could first calculate the distance between the tanks. This is easier when both tanks are on equal height.

    Then, you could do some testing with how much strength the projectile is shot. So, start off with x amount of strength. I think you can also use a bullet behavior for the projectile, it has gravity.

    So basically say: if distance between playertank and enemytank is exactly 100, it will fire with 20 gravity. If distance is 200, fire with e.g. 10 gravity, and try to apply enough gravity to the projectile bullet to exactly hit the other tank. I *think* if you can find the extremes you will be able to set the other gravity calculations for the projectile that are in between.

    With extremes I mean maximum possible distance between tanks and minimum.

    Then, you could apply a randomization to the gravity of the enemytank. So it won't always exactly hit you the first x tries. You could gradually decrease the AI randomization value with each turn; enemytank is learning and will aim better and better.

    Now this approach might take a lot of manual programming, I'm sure there's a way of automating although I can't tell you how on the spot.

    Also, with each turn, you can have a variable randomly (rounded) calculated to calculate odds. These odds with each turn will decide if the enemy will move, left, right, how many pixels. You can apply randomization to this as well.

    I'm just thinking about this as I type, hopefully it'll give you some ideas.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Here's a mockup; again, I'm no expert in AI, but hopefully it'll get you started:

    The enemy will fire and/or move 5 times. Just hit F5 to see randomization of movement and firing.

    Preview: mediaconceptfactory.com/games/capx/Tanks

    Capx: mediaconceptfactory.com/games/capx/Tanks.capx

    Edit: I forgot to mention; have not calculated in the distance measurement. But it starts with:

    System > Compare two values > distance(PlayerTank.X, PlayerTank.Y, EnemyTank.X, EnemyTank.Y)

    If e.g. this is greater, less, etc. than 'x' pixels. Then apply actions.

    - R

  • Hey Bro! Thank you very much for your tips! I'll try something in this line and I'll give a feedback as soon as possible.

    But, and if the tanks are not in same Y coordinates?

  • Ok, I used this action: apply impulse at angle > distance(Player1.X, Player1.Y, enemy1.X, enemy1.Y)/1750

    The number 1750 is result of several tests. This value hits the green tank perfectly.

    The problem is: if I move over the terrain (changing my X and Y), the yellow tank doenst hit the green everytime.

    My goal was to find a 'magical' number or expression that would hit the green tank everytime, then I would have just to change this number by a random interval around it. Did you get my idea?

    What do you think?

  • Hiya,

    Ah yes, I understand exactly what you mean. I can't come up with anything for that at this moment though. I myself am still a learning programmer, so I don't have any ready solution like that expression.

    The calculation of the distance seems like the starting point. It's just that when the X and Y are both taken into account, the problems start.

    But perhaps you could reference someone, for example I know dop2000 is a helpful and capable programmer. Or alternatively, wait for someone else to reply.

    I'll try to get back to you if I can think of something, but I'll also be busy trying to figure out some stuff for my own games :P

    Sorry I can't be of help.

  • You helped me a lot! Thanks, bro. Dop2000 always help me too. He's pro in this subject, I'm waiting his sugestion lol

    When I get a sofisticated solution, I'll share with you!

  • hey, , I got it!

    I found on construct forum an excellent .capx that makes exactly what I wanted. It's made by the great R0J0hound!

    Take a look: dropbox.com/s/9tftyryvn0pab3j/proj_path_phys_no_damping.capx

    I just set some different parameters and did an alteration on final velocity to make the target random in certain interval.

    set physics velocity t0 > vx + random (-30, 30) / vy + random (-30, 30).

    Thanks everybody! SOLVED.

  • Hey, that's great news!

    Saved that .capx from R0J0hound for future reference ;)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)