# Best way to push units away

Get help using Construct 2

### » Mon Apr 15, 2013 7:30 pm

Hi guys

As in all rts like games i need my units to push away from one another, so they dont's stack if not too many. I wanna do it the right way, so the collision between them must be smooth, meaning units should keep certain distance between them, unless they are pushed closer by other units or obstacles.

I've searched the forums but couldn't find post about this (maybe i dont know how to put it in words), and i think its common matter so maybe a little brainstorming is in order.

Performance is an issue too, since i plan to have many units per layout.

B
12
S
3
G
1
Posts: 120
Reputation: 1,829

### » Mon Apr 15, 2013 8:25 pm

Easiest way: Check distance.

Actor 1 <= distance(Actor 1.X, Actor 1.Y, Actor 2.X, Actor 2.Y) do Actions;

You can change actors by families, but only the currently involved objects will be checked and affected.
B
92
S
18
G
12
Posts: 1,166
Reputation: 18,090

### » Mon Apr 15, 2013 9:52 pm

I have a similar problem I think...

I have a lot of enemies that converge/follow/move in packs and I'd really like them to space themselves while moving.

I've tried the solid push out thing but it's a little jumpy. It'd be great to find a solution for it without having to really complicate my enemy movement.
B
5
Posts: 14
Reputation: 403

### » Mon Apr 15, 2013 11:10 pm

You are right @TELLES0808, comparing distances seems better than collision checks. I guess an For Each condition is on order so this happens every time units are too close.

That leaves me with the second problem - the action.

The units are supposed to gradually push away from each other. Even more it's supposed to have in mind all other units around it. Frankly I have no clue how to achieve this.

Any ideas?
B
12
S
3
G
1
Posts: 120
Reputation: 1,829

### » Mon Apr 15, 2013 11:14 pm

When you want them overlap each other, but not for much time:
Use the action -> move at angle -> move 200 * dt pixels at angle 180 + (angle(object1.X,object1.Y,object2.X,object2.Y)

And they will go to the opposite side.

Else,
Prevent his movement:
Check the the distance between each other with the Distance(...) > desired radius

And do the logic inside this bracket.TELLES08082013-04-16 00:53:00
B
92
S
18
G
12
Posts: 1,166
Reputation: 18,090

### » Tue Apr 16, 2013 7:41 am

How would you do this is you had multiple instances of the same object?
B
21
S
3
Posts: 450
Reputation: 2,108

### » Tue Apr 16, 2013 8:02 am

Put the objects in a Family together. Then do a check for

distance(Instance.X, Instance.Y, Family.X, Family.Y)TL222013-04-16 08:14:19
B
52
S
17
G
7
Posts: 305
Reputation: 8,354

### » Tue Apr 16, 2013 10:05 am

Thanks TELLES0808. Looking at it now, at work, where i cant start construct2 to try it out, is hard for me to understand, but i think i got the idea.
Tonight ill try it out.
B
12
S
3
G
1
Posts: 120
Reputation: 1,829

### » Tue Apr 16, 2013 11:49 pm

I think i got it running.
Since the unit is in the family, it seems to detect the distance to itself. So here is what i did

For each Unit
distance(Unit.X, Unit.Y, Family.X, Family.Y) < 20
Unit.UID not= Family.UID

I got a lot of work on the movement of units, so its really smooth and logical, but i think i got the main idea.

Thanks
B
12
S
3
G
1
Posts: 120
Reputation: 1,829