Sign in to follow this  

2D - Make enemy aim at hero

This topic is 2088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys

For an assignment at school, I'm coding the first level of Contra in c++.

I've coded a huge part of the level and elements atm, but I'm having trouble finding an algorithm for the AI.

This is the spirtesheet I use for the soldier. As you can see, it looks in 3 directions (left and right not included).

[img]http://i44.tinypic.com/2md1hs2.jpg[/img]

My question is now: How can you make the soldier aim at the hero?
Do you have to substract the Y-positions from each other and then divide that number by 3 and get the right direction out of that number?
It would be awesome if somebody could give me a working solution for this.

Underneath, you find some pictures of the real game, just to give you guys an idea of how it should be.

[img]http://i39.tinypic.com/35co6j7.png[/img]

[img]http://i42.tinypic.com/2m4yr05.png[/img]

Thanks in advance!

Share this post


Link to post
Share on other sites
Hi MattProductions!

I have some suggestion for you.

With position of hero and enemy, you can identify vector from enemy to hero (Dir vector). Depend on type of enemy, it aims to hero with appropriate angle. For example:

1. Enemy is cannon: it aims to Dir vector.

2. Enemy is soldier: it has 8 options to aim (N, E, W, S, NE, NW, SE, SW). The direction it aims to is the vector which distance from soldier to it is shortest.
...

[img]http://i1014.photobucket.com/albums/af261/congnt/GameDev/gamedev-contra-direction.png[/img]

Share this post


Link to post
Share on other sites
The most bland and simplest way you can do this, is check the Y position of the player against that of the enemy, if the player is higher, aim up, between a marge in front of you, straight, lower, aim down. This is however rather primitive and does't help you with shooting itself, only setting the right frame.

If you also want to shoot at the exact direction, you must do some vector math.

Share this post


Link to post
Share on other sites
Thanks dudes.

I was thinking the same thing as Reloadead, so I'll try that.
The soldier doesn't have to shoot in the exact direction of the hero.
It just has to shoot (like the bitmap shows, in 1 direction per bitmap).

Share this post


Link to post
Share on other sites

This topic is 2088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this