-1

i'm sure I can figure this out on my own but I feel like the way i'm writing it is already too expensive and coupled.

I am writing a simulator with a security robot and a intruder, I have a collision function, where in the 3rd if statement it checks if an intruder (rectangle of width 11) collides with an surveillance camera (arc) and if that happens then I want to activate my function for the security robot to chase the intruder.

<-- Important note: the checkShapeIntersection function is inside a timeLine event so its running constantly -->

private boolean checkShapeIntersection(Shape block) {
    //Name to check for intruder and surveillance collision
    String Surveillance = "Arc";

    boolean collisionDetected = false;
    for (Shape static_bloc : nodes) {
        if (static_bloc != block) {
            Shape intersect = Shape.intersect(block, static_bloc);
            if (intersect.getBoundsInLocal().getWidth() != -1) {
                collisionDetected = true;
                //Checks of intruder collides with an arc (surveillance), the 11th width is only for intruder rectangles
                if ( Surveillance.equals(block.getClass().getSimpleName()) && static_bloc.getBoundsInLocal().getWidth() == 11) {
                    //Activate Chase function
                    testRobot.chaseIntruder(static_bloc);
                    detected = true;
                }
            }
        }
    }

    if (collisionDetected) {
        block.setFill(Color.BLUE);
    } else {
        block.setFill(Color.RED);
    }

    return detected;
}

And inside my Security Robot Class

public void chaseIntruder(Shape intruder) {
    destinationsList.add(new Vector2D(intruder.getLayoutBounds().getMinX(),intruder.getLayoutBounds().getMinY()));
    this.updatePosition();
}
  • What you mean to continue after the loop? You do not create new thread with loop. Therefore the method is called once and after it is finished the evaluation continue after the line where you call the method. – Milkmaid Aug 02 '17 at 08:06
  • In the current way its written, my security robot only moves towards the intruder whilst the last if statement is met, as soon as the intruder leaves the arc the robot stops moving towards the intruder, however I want the robot to continue chasing the intruder until he catches him. – bigPoppa350 Aug 02 '17 at 08:08

1 Answers1

1

You probably want to use multi-threading here, in Java you can implement Runnable or Thread to achieve wanted functionality.

Here are some links with more info:

In a simple to understand explanation, what is Runnable in Java?

https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html

https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

Hope it helps.

anteAdamovic
  • 1,462
  • 12
  • 23