Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pathfinding AI in an ECS game

Started by Dominik2000 Dec 15, 2014 at 8:52 PM 3 replies 9.7k views
Original Post
Dominik2000
Dominik2000

Hello,

when I have an ECS system, where the entities are only ids, the components PODs and the systems (in my case managers, because systems are low level) have all the logic.

How can I implement Pathfinding. My thought is, that I have a component PathfindingComponent, where some points are defined, where the vehicle (as example) can drive. That means, if I have a straight street, I have 2 points, the begin and the end of the street piece, in my PathfindingComponent.

If a street is added to the scene, my PathfindingManager will be notified, and see there is a component, and add the point from the component to his store. Now the AI comes in say, the vehicle have to drive to a specified street tile, and add a new waypoint (the entity id) to the WaypointComponent of the vehicle. The pathfinding now knows, where to drive an can look for a path.

Is this ok? Are there any errors in my thinking?

One problem occurs I think: What if I have a street tile which has parking slots. One for a bus and 4 for cars. These 5 points are also in the pathfinding system, but now, the AI cannot say, drive to the street tile with the entity id, the AI have to say where exactly on the street tile. How can this be solved? Are the parking slots saved in an array in another component, or should the AI exactly say where to drive?

I hope it is clear, what I mean, if not, ask and I will try to describe it better.

Dominik

haegarr
haegarr

To start with, ECS is, as any other tool, not the panacea. Paths and waypoints as abstractions as well as roads and the terrain as their ground are not favorable candidates for entities or components. If you want to add a Pathfinding component to an entity, the entity should be a character or vehicle and Pathfinding should be a behavioral AI extension. Parameters of Pathfinding may define restrictions on what paths can be used by properties (i.e. maximum slope, useable road surface, etc), and what the goal waypoint is. Then the system responsible for pathfinding execution can be called using a Pathfinding component as parametrization.

In such an architecture the paths can be any of the usual representations, e.g. a navigation mesh. The goal of pathfinding is ever a location, perhaps in form of a waypoint. A parking slot is just another waypoint. If the AI should be enabled to distinguish parking slot waypoints from lane waypoints, just mark each waypoint with a belonging enum value. You can also add direction vectors, e.g. to denote oneway streets or preferred parking directions. However, all this doesn't belong to "game object" for which ECS is intended.

Just my 2 Cents.

Dominik2000
Dominik2000
OK that seems logical and better, thank you I try to force every part of the game to ecs but that is not very good I think.

Another question is, how to calculate such an navigation mesh? Based upon the description file of the entity?
TheChubu
TheChubu

I'd just figure out what kind of data do I need in each entity for AI to work. Then make an "AI" component and drop all of that data there, regardless of what it is. Then the "AIManager" or "AISystem" should figure out what to do with it (ie, see what the entity's current sate is, say, trying to reach X place, then simulate whatever steps needed for that frame, pathfinding, trigger movement animations, etc).

Not all the data can be stored in components though, the system will need a map of the current navmesh for example, that's the system's data, not a component.

Components store data per entity, everything else goes into the system and only exposed to other systems that might need them. At least that's the guideline I follow when dealing with this kind of thing.

Setting the navmesh/waypoints is part of whatever setup functions the AI system exposes. You could try to model the navmesh/waypoints as entities themselves, but unless you come up with a really simple way to do so, I wouldn't bother.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"   My journals: dustArtemis ECS framework and 
Postie
Postie

In my current project I have both Pathfinding and an Entity Component System. Entities that need to be able to perform actions have a command component that handles queuing and execution of commands such as "Move to X,Y". When a movement command begins executing, it first runs an A* search to find the path to the destination, and from then on it just moves from tile to tile as appropriate.

My A* implementation knows nothing about Entities or Components, it just deals with tiles and movement costs.

[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.