How do I stop sprites overlapping

0 favourites
From the Asset Store
Build your map with these isometric objects and terrains
  • I came across a problem that when I have two enemy sprites following my character they overlap . I want to figure out how to make them not overlap but still follow the position of the character. It would make my day if somone helped me with this and posted the code in the replies. Thanks!!

  • Have you tried making them solids?

  • As thenormalgeek said: "Solid" behaviour. Right click > behaviours > solid

  • Does this work with Custom Movement? And is the way it pushes out of solids still kinda glitchy? I really need a good, dependable 'mob' behaviour for a top-down shooter, where enemies will chase the player, surround him if they catch him (rather than all clumping up on the same space) and not clip through solid objects and glitch around while they're doing it. If anyone could point me to someone who's achieved an AI like this I'd be very grateful

  • put an invisible barrier around enemy.

  • That sounds... hi-tech

    Would you mind telling me a bit more DUTOIT? AI is one of those areas where I suck seriously hard, I find it really frustrating.

  • I think what he means is pinning an invisible sprite to every enemy, and making that sprite have a bigger hitbox than the actual enemy sprite (the area which you don't want other enemies to enter). You should also place it in a container with the enemy. Then, whenever those sprites collide with something, you can do whatever your logic determines (such as making the enemies go away from each other and then continue to pursue the player), or if you want the easy way out you just give the invisible sprite the solid behavior.

    This way you achieve enemies never clipping with each other without actually having to affect their hitbox (which I assume you want unchanged for shooting them or whatever way you have of damaging/defeating them)

  • I think what he means is pinning an invisible sprite to every enemy, and making that sprite have a bigger hitbox than the actual enemy sprite (the area which you don't want other enemies to enter). You should also place it in a container with the enemy. Then, whenever those sprites collide with something, you can do whatever your logic determines (such as making the enemies go away from each other and then continue to pursue the player), or if you want the easy way out you just give the invisible sprite the solid behavior.

    This way you achieve enemies never clipping with each other without actually having to affect their hitbox (which I assume you want unchanged for shooting them or whatever way you have of damaging/defeating them)

    Exactly.

    If you are a sucker for punishment you can also run a few checks when enemy 10px from hero do this that etc

    But I would go the clear sprite / add container / pin it or set collision polygon wider than enemy and turn on solid behavior.

    But the clear sprite and collision gives you greater options with AI.

    Sidenote: I suck at AI too... lol

    I accidently hit back button and wiped my comment above. I said something like force field/invisible barrier, couldn't remember in case you wonder why I edited it

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Cool, I'll have a play around with that later. Does the invisible sprite also need to have the main movement routines (eg I'm currently using MoveTo + Line of Sight to chase the player when they can see him), or can you assign those to the main sprite, and just leave the pinned invisible ones as Custom Movement with Solid enabled and it'll work itself out?

    My main problem in the past has been the way sprites sort of 'pop' around the place when pushing out of solids, especially when clumped - it just looks glitchy and bad. A lot of my game is also in very tight tunnels which makes it doubly hard to find something that works.

  • Cool, I'll have a play around with that later. Does the invisible sprite also need to have the main movement routines (eg I'm currently using MoveTo + Line of Sight to chase the player when they can see him), or can you assign those to the main sprite, and just leave the pinned invisible ones as Custom Movement with Solid enabled and it'll work itself out?

    My main problem in the past has been the way sprites sort of 'pop' around the place when pushing out of solids, especially when clumped - it just looks glitchy and bad. A lot of my game is also in very tight tunnels which makes it doubly hard to find something that works.

    As far as I know, you just have to pin the invisible sprite to your enemy sprite, you don't even have to use custom movement on it, it will just follow the enemy sprite around. But as DUTOIT suggested I'd rather not use the solid behavior and set the AI myself whenever the invisible sprites collide. That's doubly true if you're gonna have tunnels in your game, because if you set the force fields to solid, then the enemies might not fit into the tunnels, or bounce around a lot when they're traversing them. Also I'm not sure how it would work out if both your enemy sprite and the invisible one are solid.

  • Cool, I'll have a play around with that later. Does the invisible sprite also need to have the main movement routines (eg I'm currently using MoveTo + Line of Sight to chase the player when they can see him), or can you assign those to the main sprite, and just leave the pinned invisible ones as Custom Movement with Solid enabled and it'll work itself out?

    My main problem in the past has been the way sprites sort of 'pop' around the place when pushing out of solids, especially when clumped - it just looks glitchy and bad. A lot of my game is also in very tight tunnels which makes it doubly hard to find something that works.

    No, use pin behavior and pin barrier box on start of layout to enemy.

    Try widening your collision polygon. Open sprite edit and bottom left is lots of dots, drag them out to widest points, try to make circle round sprite this is the barrier for solid. See how that looks before adding invisible sprite, make sprite image bigger (the clear space) and widen the circle till it looks about right... see if that works. I say circle because square will give that bumping look when colliding. circle rolls around nicely.

    Edit: monitz87 answered above perfectly, but try and play around with collision polygon first, see how it looks

  • This doesn't seem to work, at least not with the MoveTo behaviour - the enemies still just run right through each other. I could try and do it through CustomMovement instead, does that definitely work?

  • Did you adjust your collision polygon? did you give it enough space? Are you using solid behavior?

    Else, the other way works well, just a little more complicated. If you post a demo capx with behaviours set up and moveto (basically having enemies chacing hero) this will save time in creating a moch up and I will try and put a "force field around them" lol. Its night here now so will try tomorrow sometime.

  • Solid behavior or Pinning an invisible sprite to the player or change the angle of the player if it collides.

    Post your capx. Let us see whats going on.

  • This doesn't seem to work, at least not with the MoveTo behaviour - the enemies still just run right through each other. I could try and do it through CustomMovement instead, does that definitely work?

    I think we need more information. What exactly "doesn't work"? Which solution did you implement and how? AFAIK, you can change the target coordinates of MoveTo dynamically, so if the event which makes enemies follow the player is a base level event, you should just adjust the MoveTo target every time the force fields collide with each other.

    HOW you should adjust them depends on the AI you want to implement, but a simple suggestion would be to make them move in an arc using the player as the circle's center (the angle of the arc would be an inverse function of the distance between the player and the enemy, so if two enemies 'get too close' far away from the player, they just step aside and keep marching towards the player, but if the 'getting too close' is closer to the player, you get a bigger arc and so the enemies seem too surround the player)

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