This post is a portion of Part 13 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- 13.0 – Game States
- 13.1 – Implementing Game States
- 13.2 – Better Game State Implementation
- 13.3 – Finite State Machine Artificial Intelligence
You’re all going to like this one. We can extend the concept of states to also include the behaviors of in game objects. Doing this is called Finite State Machine (FSM) Artificial Intelligence. Granted, we aren’t getting very deep into the concepts of AI in this video, and we are definitely stunted since we aren’t using OOP, but it is a good primer nonetheless. I hope you enjoy it as much as I enjoyed making it.
Full source can be found here.
I loved this tutorial it was awesome i cant wait to try to impliment that into the game im doing and maybe even during the IDLE state create a idolThreshold so that the enemy could lets say walk in a random angled y and x for a small threshold
thanks again for this
k so my brain officially hurts (probably because ive gone through all the 2d and OOP devel lessons in 5 days .. but! lol.. i cant figure out how to make the enemy box walk in a random direction in a smaller threshold until the player enters the chasing threshold.. please help
Really awesome tutorial, thanks for this as I’ve learned alot. Now I’m just studying everything over.
don’t rush the videos or you won’t really learn anything. Its not about getting everything done now but in time. Trust me, I rushed this stuff before and gave up on programming cause I couldn’t get things done. Btw you should try to implement code in a similar way but differently if you can.
back on topic, great examples and I’m glad to have made it through. Though the math throws me off just a bit since I havent been to college yet due to it being full. I sucked at math cause my teacher never helped the right way. But I guess I can improve, some guy at a starbucks learned calculus on his on 🙂
This. Great comment. Thank you
I’ve also run into the problem of rushing programming in the past. The expression slow and steady just seems like an understatement when it comes to learning programming in general and even more so in c++.
You will not be able to make a game in just a few weeks. Think of this for many high powered main stream games it takes a group of 1000 people 5 years to make them. You are only one person.
Thank you for everything you’ve taught me. I’ve actually gone back at some of the tutorials I’ve gone with before and laughed at how hard I thought they were before and actually found ways that I can improve them. You rock!
Great video, cant wait to code some heat seeking missiles with this 😀
I originally voted ‘3D game development’ for your next series, but after watching this I am definately voting for some more AI instead!
One question about the enemy following algorithm:
If you are above and to the right of the enemy at what I think is about a 45 degree angle, he seems to go faster than at any other angle. Do you know why this is?
it acts like that because the adding of sin and cosine functions at 45° reaches the maximum value (appliable to 45°+ 90°*k, being k and integer, e.g: 45, 135, 225, 315, 405,)
the adding occurs when we update the position of the badguy in this two statements:
bady += (2 * sin(angle));
badx += (2 * cos(angle));
so what we have is
cosine(45°) = ?2/2
sin (45°) = ?2/2
and the sum of it is equal to
?2/2 + ?2/2 = 2,83 taking only two decimals
If we compare those values with lets say, the badguy is directly on front of you, so the angle would be 0° the values would be
cosine(0°) = 1
sin (0°) = 0
and the adding of those would be equal to 1, so the speed would be ultimately inferior.
I hope i didn’t dizzy you, also sorry for possible bad english, its not my first language
fuck, those “?” are square root symbols, lol
i take the opportunity to thank to Mike, thanks a lot for this tutorials, they really inspired me to really get into this, you’re an excellent teacher, and one can feel the passion you have while listening to you explaining and watching you code!
?2/2 + ?2/2 = 1,41* taking only two decimals, sorry for that
the math just takes time, like the programming lol. the best tool you have is flow charts and pseudocode. just sayin’
I was trying something different with the code, and all of a sudden, I changed the signals plus for minus in the folowing piece of code, in the state of CHASING:
float angle = AngleToTarget(badx, bady, playerx, playery);
bady -= 3 * sin(angle);
badx -= 3 * cos(angle);
And how wonderful math is, the result was the inverse, like plus and minus are opposed, so the “dog”, that was running after you, now its running AWAY from you…. The math is something extraordinary!
Needing some help with this. I am wanting to implement this finite state machine AI into the game we’ve been creating in these tutorials. But I can’t for the life of me figure it out. Is there any chance you could have a look and tell me what I’m supposed to do, to get this working?
I’ve spent an entire day trying to find stuff to help me but had no luck 🙁