Is there anyway I can detect collision from a certain position on an object?

0 favourites
  • 13 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • Hi there, I'm currently working on some enemy AI with the platformer behavior. When the AI is moving around on a solid, I want them to stop moving once they reach the edge of it, whether they walk towards the right or left end on top of that particular solid.

    I was wondering if it was somehow possible to make it so the AI could detect whether they're near the edge of any solid object using collisions (If not I'm open to other possible solutions).

    Cheers

  • I'm guessing you want something like this old platforming tutorial for C2? The game has enemies that move back and forth along their platform. The trick being to place invisible markers at either end of the platform, when the enemy collides with it the direction of movement it flipped. Useful technique, can be used for other AI systems as well in top down games.

  • I suppose that's one way of doing it, but it might not work for this type of game I'm working on. However I was wondering if there was possibly a way the AI could be more self-sufficient, and detect that they were near the edge of a solid platform themselves and stop somehow without using any additional objects.

    I was thinking of making it so the AI would compare its X coordinates to whereabouts they're standing on the platform, but I'm not sure if that could work.

  • Why not. That is the standard way of doing it. Figure out which platform they are on, then if abs(enemy.X) > platform.Width/2 reverse direction. may need a little offset, a few pixels so they do not fall off if they actually go over the edge

    abs(enemy.X-platform.X) > platform.Width/2

    With Construct you may need an instance variable on enemies to set to this calculation

  • Well you don't need marker objects, but they don't really add any notable overhead.

    If you want to just use the use the positions you can just compare the delta between the bounding box of the platform and the enemy like so.

    Enemy and Platform are both sprites in this example. Enemy has a single instance variable called direction and the platform behaviour. The platform behaviour has the default controls disabled so we can simulate the control input using events. The Platform object has the solid behaviour.

    The events visit each enemy character, and find the closest platform instance to them. Then checks the distance between the left bounding box edges, then the right bounding box edges. If it's close to the left edge we change direction to "right" if it's close to the right edge we change the direction to "left". Then finally we check the direction variable, and simulate the control for the desired direction.

  • I tried that in-game now and it works just as I wanted! Although my only gripe with it is that they won't stop if a second solid is directly next to the one they're on, but at least they're not walking into bottomless pits.

    Cheers a lot!

  • Here's how it looks in-game:

    media.giphy.com/media/TKWtzfoX7a0qMvKYSi/giphy.gif

  • Looks nice! Glad I could help :) Yeah I'm not surprised that there's issues with neighbouring platforms, it's a flaw of using the "pick nearest" condition. You can kind of work around it using careful level design. If they are only going to walk on a single platform you could add a "platform id" instance variable and pick the platform using that. So they would only look at the platform they are assigned to.

  • You could achieve your original goal by using raycasting. In C3 the "Line Of Sight" behavior now has a "Cast ray" action.

    You could cast 2 rays straight down from the middle of your AI agent, 1 ray on the left side and 1 ray on the right side. Then check if the rays are hitting your platforms.

    If either ray is not hitting, or the hit distance is greater than some threshold value, then that means the AI agent is standing next to a pit.

    This solution is even more flexible because you could allow the AI agent to fall down small pits (or walk down a staircase) but not down large pits by adjusting your threshold value.

    I hope that helps!

  • Check this dev blog out for inspiration. The game was made in Unity but the exact same thing can be achieved in C3.

    http://www.asteroidbase.com/devlog/7-learning-how-to-walk/

    Reading it made me think about ray casting in a different way. It doesn't fit exactly what your asking for but points towards many possibilities of AI's traversing terrains. I hope it inspires you too :)

  • Yeah, raycasts can be understood as "eyes" of an AI

  • Interesting technique. Something very similar is actually used by robot vacuum cleaners. They have a sensor on the leading edge facing down that checks the proximity of the floor. If it moves forward and the sensor reports that the floor is far away then it's reached an edge, and knows to move away.

    Fun fact that most models suffer issues with black rugs, if they use an IR photo-diode to check for proximity then it appears as an infinite abyss to it.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I tried using ray casts, and that actually works much better. Thanks a lot!

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