Platformer AI

I’m attempting to make a game (hard to figure out by the location of my post). It’s going to be a 2D platformer with some cool unique stuff, but I want moderately smart to advanced AI in it.

I’ve got LOS set up (simple enough; just basic raycasting), so now the enemy can look around and find the player, but when it finds the player, I want the enemy to pursue the player. Like most games with enemies chasing the player, it would go after the player, if the distance got too great or the player got out of sight for a while, stop and return to it’s post. All of this would be moderately easy to implement on a top-down game, but mine’s a platformer. I think it would be sufficient if I could get a non-grid-based pathfinding algorithm set up; I could create nodes, detect which node the player is in, go towards it (checking for absent ground that needs jumping over and such).

I searched for this, and the best example was this, but it’s probably a bit too difficult to implement :slight_smile:

All of the AI examples I’ve found have all been implemented in Java, C++, C# or another really powerful but difficult language… I have basic to moderate C++ knowledge, but there are some things (some class method implementation) that are difficult to replicate in Lua, plus… they’re hard to port :slight_smile:

Does anyone here have any pointers, Lua (or another scripting language like JavaScript or Python - anything closer than Java or C :slight_smile: ) sample codes, or ideas for how to implement this type of thing? Better yet, has anyone created a project with advanced AI and has any experience? Any pointers or code on making an object automatically move to another location in a platformer world would be helpful.

Thanks in advance!

  • Caleb

I am working on the exact same thing! There are not very many Corona or LUA based AI tutorials and I would like to get some directions, too :slight_smile:

I never did AI for a plattformer before, but I think you could use a standard grid based pathfinder as a starting point. The things you have to adjust are the rules the algorithm works with.
You have to tell it that the character can only move (jump) a limited amount of scares up from a platform. Also that jumping/falling (up/down movement) is faster than left/right movement.
Also you could give platform edges a higher checking priority, to speed up the algorithm.

These are only a few suggestions, but maybe they will help you. I would try to use a grid based system, cause it is relativly easy to combine with a tile based level and already worked out in lua.

Good idea, but my level isn’t grid based.

I’ve got ramps, circles, and other shapes in it.

  • C

Same in my situation, the level is not grid based at all.

Max / CineTek

Guess that idea was shot down pretty quickly :smiley:

Seriously, though, it was a good idea…

AI’s are tricky because they are very dependent on your individual app.  How does the AI control your objects?  How to play a card game is much different than how to hunt you down in a role playing game.  Then it’s a matter of how you’ve built your objects and how code could interact with your objects.

I just have a physical enemy (he’s actually just a rectangle at the moment - no art, yet :)) that needs to be able to navigate around a platformer world. I could get by with a pathfinding algorithm that works for non-grid based levels (just waypoints), too.

I’m just using :setLinearVelocity() for moving in the X-direction, and a simple applyForce function for jumping. My game is really simple, at the moment.

  • C

Caleb - I recommend checking out some books. I ported Matt Bruckland’s Raven Example (Quake but viewed overhead) from his C++ examples to Lua/Corona. This showcased a Trigger system that governed Bots searching for Power-Up’s/Health, etc…; a Messaging system so all Game Entities could easily communicate and A* Pathfinding.

The thing about AI is it’s very rarely language specific - as long as you can understand the basics of the source code you should be able to port the concept to any language.

I thoroughly recommend checking out Bruckland’s “Programming Game AI By Example” - got lots of great material in there.

I’ve been rolling my own AI for my games, and Rob is accurate in that it is so specific to individual apps, that no one solution would be best. As SegaBoy references, AI theory can apply across platforms; you’ll be hard pressed to find a Lua-specific solution. 

I’ve been doing a lot of work with Runtime loops and velocity checks to let my enemy objects know when a player is close, or if they have interacted with an obstacle. AI is notorious for breaking and/or not being mature enough for many games, and as a brief Google search will tell you, it has yet to be perfected. 

Here’s a short snippet of one piece of code for one enemy in my game:

   if GameSettings.enem1IsWalking == 1 and GameSettingsEnemies.enem1HitPoints > 0 then

        if (lox > 90 or lox < -90) then

            obj:setSequence(“running”)

            obj:play()

            ffQ = obj.Speed2

        elseif (lox < 90 or lox > -90) then

            obj:setSequence(“walking”)

            obj:play()

            ffQ = obj.Speed1

        end

    end

    if loy <= 3 and loy >= -3 then

        obj:setLinearVelocity(testx1, 0)

    end

    if lox <= 3 and lox >= -3 then

        obj:setLinearVelocity(0, testy1)

    end

This piece is demonstrating how one would set the sprite sequence depending on the distance of the enemy from the player. I’m sure you could adapt it to have it return your enemy to a specific location. Hope that helps!

Ok, thanks for the tips, everyone.

I think I’ll read the book (looks good), look into the Raven samples from the book, see if I can get the remotest hang of some of the “Difficult Language” code :), and, I suppose, just try to rig something up :smiley:

Will let everyone know how it goes.

  • Caleb

There’s many books on AI out there - most of them are naturally geared around C++; however you only need a basic understanding of the syntax to begin understanding the concepts and porting to Lua.

I’d also thoroughly recommend the AI Game Programming Wisdom series - as they tend to split AI concepts into game genres.

Ok… Lucky for me I’ve at least read the “C++ For Dummies” book :wink:

I’ll look into that series also…

  • C

I am working on the exact same thing! There are not very many Corona or LUA based AI tutorials and I would like to get some directions, too :slight_smile:

I never did AI for a plattformer before, but I think you could use a standard grid based pathfinder as a starting point. The things you have to adjust are the rules the algorithm works with.
You have to tell it that the character can only move (jump) a limited amount of scares up from a platform. Also that jumping/falling (up/down movement) is faster than left/right movement.
Also you could give platform edges a higher checking priority, to speed up the algorithm.

These are only a few suggestions, but maybe they will help you. I would try to use a grid based system, cause it is relativly easy to combine with a tile based level and already worked out in lua.

Good idea, but my level isn’t grid based.

I’ve got ramps, circles, and other shapes in it.

  • C

Same in my situation, the level is not grid based at all.

Max / CineTek

Guess that idea was shot down pretty quickly :smiley:

Seriously, though, it was a good idea…

AI’s are tricky because they are very dependent on your individual app.  How does the AI control your objects?  How to play a card game is much different than how to hunt you down in a role playing game.  Then it’s a matter of how you’ve built your objects and how code could interact with your objects.

I just have a physical enemy (he’s actually just a rectangle at the moment - no art, yet :)) that needs to be able to navigate around a platformer world. I could get by with a pathfinding algorithm that works for non-grid based levels (just waypoints), too.

I’m just using :setLinearVelocity() for moving in the X-direction, and a simple applyForce function for jumping. My game is really simple, at the moment.

  • C