0

I'm having troubles with my current code and I wish someone help me to develop a simple one or at least give some ideas (pseudocode).

Basically I have n drones and I want to create a "safe area" around them.

So I want to if drone2 is inside drone1's safe area I want stay outside. But I want to create for N drones (drone.length)

For example: If drone2 approaches drone1 from the +x side i want him to stay in position (x+1,y) so they dont collide

Image of the Safe Area

Code:

For each Drone I have:
int Xmin
int Xmax 
int Ymin 
int Ymax
string ip (example "192.168.1.10"
Point position (x,y)

for(int i = 0 ; i<drone.length;i++)
{

   //i think I need to check drone's IP to know the current drone being iterated
    If(drone[i].position //is inside every drone[EXCEPT i]’s safe area
              {
                  //debug: Which drone’s safe area is drone[i].position inside?
              }
}

Can someone help me please? Thank you so much

  • is this actual code or pseudo code? – Simon Price Feb 13 '17 at 21:07
  • 1
    Well it's the code I was trying to develop but I got stuck –  Feb 13 '17 at 21:08
  • 1
    FYI this is "Collision Detection". For you it appears you want some sort of 2 dimensional version, possibly for circles but maybe just squares. [Here](http://stackoverflow.com/questions/3540472/collision-detection-implementation) is the first result I came across. – Quantic Feb 13 '17 at 21:10
  • I know how to see if it's inside. Just check if it's inside xmin,xmax etc... I dont know is the rest. –  Feb 13 '17 at 21:13
  • How do I check if If(drone[i].position //is inside every drone[EXCEPT i]’s safe area? –  Feb 13 '17 at 21:14
  • //i think I need to check drone's IP to know the current drone being iterated –  Feb 13 '17 at 21:15
  • You could go with something like this: `if (drones.Where(d => d.IP != drones[i].IP).Any(d => /*Method to determine if the point is inside d's safezone*/))` – Ian H. Feb 13 '17 at 21:22
  • by method you mean a function? thx –  Feb 13 '17 at 21:24
  • Hi @Tiago Silva, creating a new user and asking the same question over again isn't going to get you anywhere. You need to break your problem down into smaller questions... – Callback Kid Feb 13 '17 at 21:51

1 Answers1

-1

Ok I think I have got this,

For this problem instead of using squares i am going to use Pythagoras theorem which states. A=sqrt(B^2+C^2) This will give the distance between each drone.

So in psudocode i would do this:

xDangerDrones=[]
yDangerBots=[]
Drones=[] // A list of all your drones
for drone in drones:
 for drone2 in drones:
  if(not (drone==drone2 and check if drone and drone 2 are in lists xDangerDrones and yDangerBots)):
    distance=sqrt(abs(drone.x-drone2.x)^2+abs(drone.y-drone2.y)^2)
    if distance>YOUR RADIUS HERE:
      if (abs(drone.x-drone2.x)>abs(drone.y-drone2.y)):
        if (drone.y>drone2.y)
          yDangerBots.append(drone)
        else:
          yDangerBots.append(drone2)
      else
         if (drone.x>drone2.x)
          xDangerBots.append(drone)
        else:
          xDangerBots.append(drone2)
for xDangerDrone in xDangerDrones:
  drone move right

for yDangerDrone in yDangerDrones:
  drone move up

Edit

Full Java code as promised:

Drone.java

import java.util.Random;
import java.util.UUID;

/**
 * Created by Asim Poptani on 2/14/2017.
 */
public class Drone {
    private UUID uuid;
    private int positionX;
    private int positionY;

    Drone()
    {
        Random random=new Random();
        uuid= UUID.randomUUID();
        positionX=random.nextInt(100);
        positionY=random.nextInt(100);
    }
    public UUID getUuid() {
        return uuid;
    }



    public int getPositionX() {
        return positionX;
    }

    public void setPositionX(int positionX) {
        this.positionX = positionX;
    }

    public int getPositionY() {
        return positionY;
    }

    public void setPositionY(int positionY) {
        this.positionY = positionY;
    }
}

And Main.java

import java.util.HashSet;

public class Main {



    public static void main(String[] args) {

        final int emergencyRadius=5;
        HashSet<Drone> moveUp = new HashSet<>();
        HashSet<Drone> moveDown = new HashSet<>();
        HashSet<Drone> moveLeft = new HashSet<>();
        HashSet<Drone> moveRight = new HashSet<>();

        HashSet<Drone> drones= new HashSet<>();
        for (int generator=0;generator<10;generator++)
        {
            drones.add(new Drone());
        }

        for (Drone drone:drones)
        {
            for (Drone drone2:drones){
                if (
                        !(
                                drone==drone2
                                        ||
                                        moveUp.contains(drone)
                                        ||
                                        moveUp.contains(drone2)
                                        ||
                                        moveDown.contains(drone)
                                        ||
                                        moveDown.contains(drone2)
                                        ||
                                        moveLeft.contains(drone)
                                        ||
                                        moveLeft.contains(drone2)
                                        ||
                                        moveRight.contains(drone)
                                        ||
                                        moveRight.contains(drone2)
                        ))
                {
                    double distance=Math.sqrt(Math.abs(drone.getPositionX()-drone2.getPositionX())^2+Math.abs(drone.getPositionY()-drone2.getPositionY())^2);
                    if (distance<emergencyRadius)
                    {
                        if(Math.abs(drone.getPositionX()-drone2.getPositionX())>Math.abs(drone.getPositionY()-drone2.getPositionY()))
                        {
                            if(drone.getPositionY()>drone2.getPositionY())
                            {
                                moveUp.add(drone);
                                moveDown.add(drone2);
                            }
                            else {
                                moveUp.add(drone2);
                                moveDown.add(drone);
                            }
                        }
                        else {
                            if(drone.getPositionX()>drone2.getPositionX())
                            {
                                moveLeft.add(drone);
                                moveRight.add(drone2);
                            }
                            else
                            {

                                    moveLeft.add(drone2);
                                    moveRight.add(drone);

                            }
                        }
                    }
                }
            }
        }
        for (Drone drone:moveUp)
        {
            System.out.print("\nmove up ");
            System.out.print(drone.getUuid());
        }
        for (Drone drone:moveDown)
        {
            System.out.print("\nmove down");
            System.out.print(drone.getUuid());
        }
        for (Drone drone:moveRight)
        {
            System.out.print("\nmove Right ");
            System.out.print(drone.getUuid());
        }
        for (Drone drone:moveLeft)
        {
            System.out.print("\nmove left ");
            System.out.print(drone.getUuid());
        }
    }
}
audittxl
  • 41
  • 1
  • 12
  • yes this code will work with n number of drones. Append means add an element to a list. See here https://www.tutorialspoint.com/python/list_append.htm – audittxl Feb 13 '17 at 21:27
  • Why do you make the two first for loops? –  Feb 13 '17 at 21:28
  • This is so we can compare each drone with each other. Essentially think of searching! – audittxl Feb 13 '17 at 21:29
  • and the last two for? Which drone moves? and what about down and left? –  Feb 13 '17 at 21:33
  • Currently the program will make the left most drone go left and the top most drone go forward. If you want both drones to evade I.e. go in opposite directions then there is a simple way to do it give me until tomorrow and I will make you some proper Java code for you to understand. – audittxl Feb 13 '17 at 21:52
  • Thanks. I'm using C# but I think I can understand JAVA. Thanks. I will wait ;) –  Feb 13 '17 at 22:07
  • @ScumbagSteve Ok give me a few minutes to create the code – audittxl Feb 14 '17 at 18:39
  • @ScumbagSteve There you go – audittxl Feb 14 '17 at 19:54
  • 1
    Shouldnt it be if (distance –  Feb 14 '17 at 20:07
  • You have implemented naive collision detection; this algorithm works for a small number of objects but very quickly runs into difficulties as the number of objects grows large; imagine what happens when you have a thousand or a million possible objects. – Eric Lippert Feb 14 '17 at 20:22
  • One trick that can make this algorithm faster is: computing square roots is slow, but computing squares is fast. Instead of computing: `if (root(a*a + b*b) < c)` instead compute `if (a * a + b * b < c * c)`. It's a lot faster! – Eric Lippert Feb 14 '17 at 20:23
  • Also, `^` is not the exponentiation operator in Java or C#. – Eric Lippert Feb 14 '17 at 20:24
  • Also, you don't need to put an `Abs` on a square. Squares are always non-negative. – Eric Lippert Feb 14 '17 at 20:24
  • http://stackoverflow.com/questions/42235596/c-sharp-syncronizing-movement-using-queues –  Feb 14 '17 at 20:30
  • Hi @EricLippert Just a few things this program is meant for avoidance system for a few drones as i understand from the question therefore i have not spent time optimising the program. I agree with your comments and if you wish to you can change my program. Also what is with the -1? – audittxl Feb 14 '17 at 20:44