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

2013/10/5

Help with avoiding obstacles.

JTSCADET JTSCADET

2013/10/5

#
I want a robot to avoid obstacles to get from one side if the world to the other. My idea was to have the robot follow his right until he eventually finds it. The first step i have is to get the robot to check to see if he has a wall to his right side. I've only used Greenfoot for about a month so I'm not sure of all the details about returning values and true/false stuff. Anyway, here's what i have for now.
public void followRight()
    {
      if (checkRight()==true)
      {
          fwd();
      }
      else
      { turnRight();
        fwd();
      }
    }
        
    public boolean checkRight()
    {
        if (getDirection()==NORTH)
        {
            return northWall();
        }
        if (getDirection()==SOUTH)
        {
            return southWall();
        }
        if (getDirection()==EAST)
        {
            return eastWall();
        }
        if (getDirection()==WEST)
        {
            return westWall();
        }
    } 
    
    public int getDirection()
    {
        return direction;
    }
    
    public boolean northWall()
    {
        Actor wall = getOneObjectAtOffset(1, 0, wall.class);
            if(wall!=null)
             {
                 return true;
             }
            else 
             {
                 return false;
             }
    }
    
    public boolean southWall()
    {
        Actor wall = getOneObjectAtOffset(-1, 0, wall.class);
            if(wall!=null)
             {
                 return true;
             }
            else 
             {
                 return false;
             }
    }
    
    public boolean eastWall()
    {
        Actor wall = getOneObjectAtOffset(0, 1, wall.class);
            if(wall!=null)
             {
                 return true;
             }
            else 
             {
                 return false;
             }
    }
    
    public boolean westWall()
    {
        Actor wall = getOneObjectAtOffset(0, -1, wall.class);
            if(wall!=null)
             {
                 return true;
             }
            else 
             {
                 return false;
             }
    }
What do I need to do to get this working?
JTSCADET JTSCADET

2013/10/5

#
Okay well I found a way around my problem sorry if this wasted your time.
You need to login to post a reply.