Pathfinding and Solids - People sprites getting stuck

0 favourites
  • 9 posts
From the Asset Store
Units do not overlap each other and use different ways if there are several free ways.
  • Hey there,

    I am using the pathfinding behaviour on a sprite (that is a person) and I have buildings that are solid objects. Sometimes my people are set to wander around which means they will pick a random X and Y co-ordinate on the layout and the pathfinding behaviour will calculate a path for them to make their way to that point. When they reach that point they calculate another random co-ordinate and go there again. If the person collides with a building, they will get stuck like they are trying to go to a co-ordinate which is inside the building but they can't do it, nor does it stop and calculate a path to a different co-ordinate. My understanding of the pathfinding behaviour was that it cannot go to a co-ordinate if it cannot reach it like solids or am I misunderstanding that?

    What am I doing wrong?

    Thanks for your help!

  • That's correct, it's probably one of two things, you don't have Solids set as obstacles or you haven't regenerated the obstacle path once the buildings were created. If you create any new obstacles during run time the pathfinding objects won't see them unless you regenerate the map.

  • If your trigger for calculating a new random target is reaching the destination, your npc will never get a new path if it never reaches its destination.

    Rather than pick a random x/y coordinate to path to, what you want is to pick a random VALID x/y to path to. There are a few ways to do this.

    You can put your target selection in a loop to repeat for a new target if the destination is not reachable.

    Or you can place a helper sprite with the custom movement behavior with push out solids-nearest set, and use that as your target for pathfinding.

  • Hey guys,

    Thanks so much for your quick response and advice, it's amazing!

    What you said makes perfect sense to me. I've just placed a "Regenerate Obstacle Map" action on an event after the building is finished. The only problem I have encountered now is that when a person finishes constructing a building, they are in the middle of the building when it becomes a solid, and even though I have set my person sprite to not collide with anything, and turn the person's solid behaviour off, they normally just walk out of the building (due to finding a new path with their 'wandering' status, but since adding that action, they get stuck inside the building and don't go anywhere. It's like the pathfinding behaviour just stops working after running the "Regenerate pathfinding obstacle map" action.

    Still it may be caused by the fact that my pathfinding sprite is whacked into the middle of a solid with no escape, so I had an idea; I tried using the 8 direction behaviour to get them to 'walk out of' the building but they just appear next to the building in the top left which is somewhat a solution because it enables them to return to their normal pathfinding behaviour and they don't get stuck but I was trying to get them to walk out of the building instead. I may have to delay making the building a solid until all people sprites are no longer overlapping the building after construction is complete.

  • how about regenerating the obstacle map on completing the construction, but disabling the solid on the npc while overlapping with a building?

  • how about regenerating the obstacle map on completing the construction, but disabling the solid on the npc while overlapping with a building?

    Like I said previously, I was actually doing that, but it seems like the solid behaviour is still causing a problem with my NPC. I think our responses clashed a bit because I made a response just before you did. xD

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I got it!

    I couldn't just check to see if anyone was overlapping the building or not straight away, I had to create some more variables:

    1) I created a boolean called "Built" - When the person finishes constructing the building, this boolean will be set to true.

    2) I created a variable called Cooldown_Construction_Brand_New with the initial value set to 3 - every second if the value of this is higher than 0, and the boolean "Built" is true, then subtract 1 from this variable.

    Once the Cooldown_Construction_Brand_New variable is 0, Person is not overlapping building and building is not a solid, set solid behaviour to Enabled and then regenerate pathfinding obstacle map.

    As for the person: I don't need the 8direction behaviour (since setting 8direction down would be bad if the building was against some sort of obstacle in that direction and they would not be able to leave it properly). The person will just walk out of the building in whatever direction they deem possible to get out of with their pathfinding behaviour and 'wander' status that I have set and because the building has not become a solid yet due to the cooldown delay, there are no issues with pathfinding, so after they are no longer overlapping buildings, I can set their collision and solid behaviour back to true (that's what the Still_On_Construction_Site boolean is doing when it goes to false).

    Here's a screenshot of my events. Please tell me if you agree with my solution or if there's something better:

    Edit: I made some adjustments because I realised that the last event is not going to work at all, haha:

  • Yes you will come across these kind of issues if you choose to make a game that makes use of complex pathfinding. I have a rollercoaster tycoon type game with a ton of data but it's fun to handle. I would be careful of something that appears to be happening in that logic, it looks like the cooldown timer will be 0 when nothing is being built so it looks like the third event is regenerating the obstacle map every tick which is not good, unless you reset it to 3 somewhere. If you set it to 3 only on the start of construction then it seems to be an issue. Another problem I can see is that you don't seem to force the persons to leave, rather you set the building to solid based on the person leaving, so there could be a bug where you are waiting forever for them to leave and the building never becomes an obstacle for other people. Maybe you have an event further down which is if still on construction site then move them which would be useful.

    Edit: Ok I see while I was typing you added a trigger once. Do you have an event that forces the persons to move? At the moment it just checks if they are there.

  • Thanks again, Plinkie! Sorry about the confusion between my edits!

    Yes, I do. It's just not explained in these screenshots very well. You will notice that on the first block of the Construction Complete group that I set Person's Status value to 0. This checks:

    Conditions:

    Person Status = 0 (defined as wander status)

    Person Wander_Path_Cooldown = 0 (another cooldown system where a person will pause for up to couple of seconds when they reach their destination before moving to the next random coordinate),

    Person Not Pathfinding calculating path

    Person Not Pathfinding moving along path

    Actions:

    Person find path to (random(LayoutWidth), random(LayoutHeight)).

    Simple action for this status for the current stage of development.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)