Hey folks,
Hopefully this will be a no-brainer for some of you but I am a stumped regarding an issue with a prototype game I am writing. Let me attempt to explain the problem as well as the desired solution:
The Setup:
I built and displayed a small map using the Dusk engine. It is small and fits entirely on screen so no scrolling or camera movement is required (see attached image).
All the walls are static physics bodies with no bounce factor. The pink rectangle at the top is a dynamic physics body with an initial linear velocity set to move at a velocity of +64 on x and 0 on y, so it’s moving right towards the corner (as the annotated arrows on the image indicate). This rectangle is a non-controlled entity so it’s job is (for the time being), to move autonomously around the map using the walls, and available openings as the bounds for its available movement. The only governing rule in this environment is that the rectangle cannot turn around and go the opposite direction unless the opposite direction is the only available recourse. Otherwise, it must continue moving forward.
I have a global collision listener setup and am accurately and successfully detecting when the pink rectangle collides with the top right corner (wall) or any wall for that matter.
The Problem (my interim solution):
Currently, my collision handler is determining the subjects of the collision and in the event that rectangle collides with wall (or corner here), I am treating this like a dead end and then manually determine an alternate available direction using the rectangle’s current location and surrounding tiles. In this case, it would be to move downwards. I then alter the rectangle’s linear velocity to set it in a downward direction.
Desired Solution:
The good news is that my method works— but I’m not certain this is the optimal way to accomplish this. Ideally, I would like a way to utilize the physics engine in determining the rectangle’s proper course and move around the corner either rotating or reflecting automatically without manual interjection. If you extrapolate this concept around the entire environment, what I would like is that, in any position and any direction on a valid path, the rectangle continue to navigate around while resolving dead ends and/or corners on its own.
Possible? I am open-minded to any alternative approaches or at least to understand any other options available. I can also post code if necessary or provide more information.
Thanks,
Mike S.