Recommended approach for detecting nearby sprites for AI

Imagine a top-down game like Frogger, there are moving platforms and the AI needs to know if there is an adjacent platform to jump to or if it’s just open space and it would die.

They can move in 4 directions. They need to check ahead in these 4 directions to find out if they will land on a platform or not. NOTE: A tile based collision solution is not appropriate, since the platforms can move freely pixel by pixel.

Credit to @JCash for giving me the tip that I can use the build in collision system to pay attention to the “enter” and “exit” messages to keep track of which platforms the player is “overlapping” to know if there is a platform under him after he lands from a “jump” in this top down game.

So the only way forwards I can see is to extend that to create 4 “sensors” around the AI, give them their own collision group each: sensor_up, sensor_down, sensor_left, sensor_right. And then I can use the collision message data to know which sensor got a hit (by checking message.own_group) and track overlapping platform separately for each sensor.

Is that a sensible approach for this situation? (no pun intended)

Seems like a good approach. You can probably do four ray casts too achieve the same effect.

I don’t understand the 4 directions though.
Imo it should be enough with 1 box each, and instead use the objects’ positions/velocities to determine which side was hit.

1 Like

Thanks, that’s good to know :slight_smile:

I’ve written off raycasts for a lot of cases within this particular project, since it ignores objects that contain the starting point of the ray, which is most of the time in my top down game since objects are often overlapping but shouldn’t “collide” until a certain condition is met, I.e. having “landed” after a jump, at which point they may already be overlapping an object they need to know about. Tracking enter/exit messages is the best solution for this project. I get the impression that Raycast is better for line of sight or hitscan bullets, that sort of thing, where ignoring the object in the start of the ray would be more desirable.

I feel like I’d be writing my own collision within that just to work out an overlap based on positions and sizes, if I understand what you’re saying.

For clarity, in this diagram, the red dot is the AI, and the green squares is where he needs to check for an overlap for the logs/turtle platforms to land on. They move in different directions. The way I’d usually deal with this is just simple AABB checks but I’m trying to make use of the built in collision for the time being.
image

2 Likes