I havn’t been writing for a couple of days because I haven’t had time to. I have done some programming though. My teammate and I had a meeting yesterday and we decided on a few things. He is working on the config-file which is good because that is at this time way over my head. I am a maths freak.

I am working on the AI. We want to have the Three states: Idle, the enemy is just hanging around, Attacking, the enemy wants you for breakfast, and Fleeing, the enemy has realized he is the one about to become breakfast.

The Idle state is supposed to be a moving pattern. I will try to find some nice algorithms for patterns, worst case I will just “hard code” some Points for the enemy to change his velocity (yes, a velocity is a vector that has a direction) at, but I will try to avoid that. I began som work on changing his velocity when he reaches the border of the window and I can get him to go somewhere else.

The Attacking state just came to my mind as I was doing some collision logics and got stuck. And my god how easy it was to get the enemy to chase the player! what I did was I instanciated a new Vector2 object: direction_to_player. I gave its position the delta-position between the player’s position and the enemys position (seperating the x- and the y-axes) and just did the old distance formula, derived from the pythagorean theorem, D^2 = (x2 – x1)^2 + (y2 – y1)^2 and took the Square root out of that((x2, y2) beeing the player’s position). Now there would be a value which was the same value as the distance between the enemy and the player would have. But how to get the correct direction? well I had to normalize the vector2 so that it would get a direction. The unit vector came in to my mind. Furthermore I then realized there were two functions to do this in the Vector2 class, one called lenght and one called normalize. I had only forgotten about those, but they came in really handy. It does need a little polishing work but he’s chasing!

to get the enemy to move i simply used the += operator to change its position with the normalized Vector2 vectors.

All the details about exactly how I implemented it I have both on paper scribbles and in my code so, moving on..

The Fleeing state: This is my next problem. I Think I will start simple. I suppose I could have the enemy check for distance between him and the player and if he is within a certain distance he will move in the opposite normalized vector direction. If that will not work for some reason maybe I’ll try to have an invisible circle around the enemy and if the player’s position is inside of that circle I will have him going in another direction.. I will try the first idea first.

There is something odd about the collisions, but I don’t think this will be difficult to correct. Whenever the player is not currently colliding, the enemy can go through the walls. But when the player is standing on a platform, the enemy cannot pass through… I belive it is because currently all level collisions are in the same place, I need to have them seperate.

Ok, that’s it for today’s blogging.

good night


One thought on “AI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s