This site requires JavaScript, please enable it in your browser!
Greenfoot back
Matt
Matt wrote ...

2014/3/18

Preventing Collision

Matt Matt

2014/3/18

#
public void avoidCollision(int n){

        Actor a = getOneObjectAtOffset(getX(), getY() + 80, Car.class);
        Actor b = getOneObjectAtOffset(getX() , getY() - 80, Car.class);
        Actor c = getOneObjectAtOffset(getX() + 80, getX(), Car.class);
        Actor d = getOneObjectAtOffset(getX() - 80, getX(), Car.class);
        
        if(a == null){
            move(n);
        }else{
            a.move(n);
            move(0);
        }
        
        if(b == null){
            move(n);
        }else{
            b.move(n);
            move(0);
        }
        
        if(c == null){
            move(n);
        }else{
            c.move(n);
            move(0);
        }
        
        if(d == null){
            move(n);
        }else{
            d.move(n);
            move(0);
        }
    }
I am trying to prevent the collision of cars, however I dont quite no why my code is having no effect? I would think if a car, was 80 pixels in any direction a car would stop?
danpost danpost

2014/3/18

#
With how you have it coded, a Car would probably stop for a car in range. However, you are making 4 checks with 4 separate 'if' statements. A car could find cars in range at three of those location (and NOT move), but if there is no car at the fourth, what will happen? ... the car will move. Actually, there is no sense in using 'move(0)'. This is equivalent to pressing the 'L' (lobby) button on an elevator when the elevator is already at level 'L' (the lobby level). It does not prevent other buttons pressed from instructing the elevator elsewhere. Basically, since a computer only does what it is told -- you do not need to tell it not to do something. In other words, re-writing what you had (although it does not work for you), this would be equivalent:
public void avoidCollision(int n){
    Actor a = getOneObjectAtOffset(getX(), getY() + 80, Car.class);
    Actor b = getOneObjectAtOffset(getX() , getY() - 80, Car.class);
    Actor c = getOneObjectAtOffset(getX() + 80, getX(), Car.class);
    Actor d = getOneObjectAtOffset(getX() - 80, getX(), Car.class);

    if (a == null)  move(n); else a.move(n);

    if (b == null) move(n); else b.move(n);

    if (c == null) move(n); else c.move(n);

    if (d == null) move(n); else d.move(n);
}
Come to think of it, if one car stops and tells a near car to go, then the near go would stop and tell this car to go, do not these instructions pretty much cancel out. Simply put: for any car, when in range of another car, the other car tells it to move, otherwise it tells itself to move. Hence, the appearance of no effect. How about using 'getObjectsInRange':
public void avoidCollision(int n)
{
    if (getObjectsInRange(80, Car.class).isEmpty()) move(n);
}
Matt Matt

2014/3/18

#
Oh, okay ... I see the what you mean about my original code. My problem with getObjectsInRange, is the cars could be simply on the other side of the road and they would be stopped by this method? I was thinking with my oringinal method, it might be because it should stop if a car is between getY() + 0 to 100? and not on the 100th pixel only
danpost danpost

2014/3/18

#
Maybe each car needs to know more about what the other car is doing before deciding what to do. Like, is it coming towards me, going away from me, or traveling along side of me? or, is it getting closer to me or not? if getting closer, where will are paths intersect and who will get there first and how soon? you know, maybe things like these. If your cars only travel left, right, up and down, then you can ignore cars whose difference in rotation is 180 or 0 AND whose xs and ys are different by more than 79. All in all, you need an 'else' part of the 'if' I provided above to make whatever determination you deem fit (not to let it know it cannot move; but to let it know if it CAN move and then to move if it is allowed).
davmac davmac

2014/3/19

#
There is another issue here:
        Actor a = getOneObjectAtOffset(getX(), getY() + 80, Car.class);  
        Actor b = getOneObjectAtOffset(getX() , getY() - 80, Car.class);  
        Actor c = getOneObjectAtOffset(getX() + 80, getX(), Car.class);  
        Actor d = getOneObjectAtOffset(getX() - 80, getX(), Car.class); 
If you use getOneObjectAtOffset(...), you should not be adding getX() and getY() to the offset. Also, for actors c and d, the second parameter should probably have been getY() rather than getX() anyway. A correct version of the above would be:
        Actor a = getOneObjectAtOffset(0, 80, Car.class);  
        Actor b = getOneObjectAtOffset(0, - 80, Car.class);  
        Actor c = getOneObjectAtOffset(80, 0, Car.class);  
        Actor d = getOneObjectAtOffset(-80, 0, Car.class); 
Matt Matt

2014/3/20

#
I'm thinking perhaps getOneObjectAtOffset wouldn't be the most appropriate method? I've also tried
        Actor c = getOneIntersectingObject(Car.class);
        if(c == null){
            move(n);
        }
        else{
            c.move(n);
            move(0);
        }
and
/         Car car = isCarInRange();
    // 
    //         if(car != null && car.getRotation() == getRotation()){
    //             move(n);
    //             car.move(0);
    //         }
    //         else{            
    //             move(n);
    //         }
and
// boolean okToMove = getIntersectingObjects(null).isEmpty(); // if (okToMove) { // move(n); // }
But I just can't seem to get anything to work? Am i missing something obvious
danpost danpost

2014/3/20

#
Well, using 'getOneIntersectingObject' or 'getIntersectingObjects' would be pretty much out of the question; if the cars are intersecting, then it is too late because the collision has already occurred. Also, you already saw what happens when you use 'else { c.move(n); move(0) }', which really accomplished nothing. You are finding that there is a lot more involved in controlling traffic than just a simple stop and go depending on nearby cars. Like I said above, speed, distance, direction and time all play a role. Imagine you are stopped at an intersection, what do you do before you cross it? You look for 'nearby cars' that are heading in your 'direction'; then, you gauge their 'distance' and 'speed' to see if you have 'time' to cross the intersection safely. Only when this state occurs for 'all' cars will you then proceed through the intersection. That was an instance when your speed was zero. The same checks apply for when you are moving, except you add another dimension to the equation -- you will no longer be concerned with just one location (the intersection itself), but multiple locations (the piece of 'road' ahead that you will be passing into). I might be explaining it to be more involved than it would within your scenario. My point is that there is more to consider than just whether a car is near.
danpost danpost

2014/3/20

#
I programmed one using 'getOneObjectAtOffset at several locations and, if a car was found to be at any of those locations, its direction of movement was checked to see if it was a concern. Only if no cars were of concern was the car allowed to move.
You need to login to post a reply.