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

2016/8/5

shortest path

NIRO17 NIRO17

2016/8/5

#
there are ob1s are location in different places randomly. and robot1 is also palce in randomly..what i want to know is how can my robot identify the nearest path and go to the nearst object and grab it first and go to the next nearest object and finally go to the end location. here is my robot class

public class Robot1 extends Actor
{
    /**
     * Act - do whatever the Robot1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    
    {
         if (getWorld().getObjects(ob1.class).isEmpty())
    {
        chaseEnd();
    }
    else
    {
        chaseOb1();
    }
        
        // Add your action code here.
    }    
    
    
      public void chaseOb1(){ 

        turnTowards(ob1.objX1, ob1.objY1);
        Actor ob1=getOneIntersectingObject(ob1.class);
        getWorld().removeObject(ob1);

        move(2);
    }
    
      public void chaseEnd(){ 

        turnTowards(end1.endX2, end1.endY2);
        Actor end1=getOneIntersectingObject(end1.class);
        getWorld().removeObject(end1);
        move(2);
}
}

here is my object class

public class ob1 extends Actor
{
    /**
     * Act - do whatever the ob1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static int objX1, objY1;
    public void act() 
    {
         objX1=getX();
       objY1=getY();
        
        // Add your action code here.
    }   
}

and here is my world class

public class MyWorld extends World
{
        public void act(){
        if (getObjects(end.class).isEmpty() && getObjects(end1.class).isEmpty()) Greenfoot.stop();
    }
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1150, 600, 1); 
        prepaire();
    }
    
     public void prepaire(){
          Robot1 a1 = new Robot1();
          int aX1=Greenfoot.getRandomNumber(getWidth());
          int aY1=Greenfoot.getRandomNumber(getHeight());
          addObject(a1,aX1,aY1);

}}
danpost danpost

2016/8/5

#
My major concern is that I do not see any place in your code where any orb1 objects are being created and added into the world. As such, the act method of the ob1 class will never execute and the values of 'objX1' and 'objY1' will remain at zero forever. My next concern is that if you are to have more than one ob1 object in the world at any time, then the values of these fields will only point to the location of a specific one and its relative closeness to the Robot1 object will not be accounted for. As each one pointed to is removed from the world, the values will automatically point to some other ob1 object in the world until none are left, at which point, their values will continue to point to the location where the last one was removed from the world at. The thing about it is that as Actor objects, you can acquire their locations without the need for the added fields in your ob1 class. The 'getX' and 'getY' methods return the value of the 'x' and 'y' (private) fields provided by the Actor class which retain the coordinates of an actor in a world. I think it best that you refrain from using 'static' fields until you learn more about how to use them. Now, you want the robot to go to two different objects and then go to the end location. Well, you will need to keep track of the two objects reached. This will be done by adding instance reference fields, named 'objOne' an 'objTwo' for example. They should be of Actor or ob1 type (if both objects are ob1 objects). There values will start as 'null'. As long as the second field is 'null', go to the closest object. When an object is reached, if the first field is still 'null', set it to the object reached; else, set the second one to the object reached. If the second field is not 'null' (else of "as long as" part), proceed to end location. In pseudo-code, it would look like this:
/** In Robot1 class */
// instance fields
private Actor objOne, objTwo;

// in act or method it calls
if (objTwo == null) // if have not found two yet
{
    moveTowardObject(); // move toward closest
    Actor object = getObjectReached(); // object reached, if any
    if (object != null) // if an object was reached
    {
        if (objOne != null) // if first object already logged
        {
            objTwo = object; // log second object
        }
        else // first object not logged yet
        {
            objOne = object; // log first object
        }
    }
}
else // two objects already found
{
    moveTowardEnd(); // move toward end location
    Actor end = getEndReached(); // end reached, if any
    if (end != null) // if end reached
    {
        Greenfoot.stop(); // stop program
    }
}
Notice how the fields can be used to control each step of what the robot needs to do.
You need to login to post a reply.