Sunday, October 7, 2018

xna - How do you do AI path following within a 2d physics engine like farseer/box2d?


I'm in the process of moving a 2d top down game I've been working on into a proper rigid body physics engine like Farseer. Up until now, I had just hacked together my own physics code where needed.


I'm trying to learn the proper way of doing things here.


What is the proper way to make your AI follow a set path once you have made them rigid bodies inside of the physics engine?


If I have a path of navigation nodes on my map that I need the AI to follow, previously I would just move them along the path manually by calculating the next position they should be at for the next time step and manually setting them to that position.


But now they are rigid bodies and subject to collisions and any forces that may hit them and knock them off path.


So to make the AI move I believe I should now be applying impulses/forces to them? I should no longer be manually setting their position each frame.


So I think I need to go from a deterministic world where I force the AI to strictly follow a path to a non-deterministic world where they could get knocked about in any direction if hit and I simply nudge them towards the next node in the path to make them move.


Is that right? Is that how other people do it?


This raises some questions about how to then avoid your AI getting stuck on corners of scenery now that they aren't walking a precise path, how do you guys handle that type of thing?



Or is it better to somehow mix the two and still have your AI follow a fixed path by setting their position manually, and only react to other forces under certain circumstances you can easily control?


Thanks for any advice guys.



Answer



Steering behaviors work very well in combination with a physics-engine, as they are usually implemented in a way that they return a "steering force" which can then be applied to your physics-body.


To make a unit follow a path, you could use Seek to go from path-node to path-node (make sure to avoid overshoot) and then use Arrival at the last node in your path.


As for your concerns about getting stuck: Modelling the path-following using forces should actually be quite accurate. You're right that an object might get thrown off the path if it collides with another object, but since you'll calculate a steering-force in every update, the object should be on track again in no time. If the deviation from the path after a collision can potentially be huge, then I suggest you remember your last position whenever a collision occurs and then steer the object back to that last position before continuing the normal route.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...