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

2017/3/30

Change in Direction help

Asiantree Asiantree

2017/3/30

#
if ((xSpeed < 0 && getX() <= getImage().getWidth() / 2) || (xSpeed > 0 && getX() >= getWorld().getWidth() - (getImage().getWidth() / 2))) 
        {
            xSpeed = -xSpeed;
        }
        if ((ySpeed < 0 && getY() <= getImage().getHeight() / 2) || (ySpeed > 0 && getY() >= getWorld().getHeight() - (getImage().getHeight() / 2))) 
        {
            ySpeed = -ySpeed;
        }  
I am to produce some code that will detect two other actors of a different type (Fries class, in this case) and turn towards the one that is closest. Once that one has been touched, the Ant will turn towards the other Fries. How can I go about changing my xSpeed and ySpeed to adjust the change in direction when looking at a certain actor.
danpost danpost

2017/3/30

#
Once you get the closest Fries object, 'fries', you can do:
double rads= Math.atan2((double)(getY()-fries.getY()), (double)(getX()-fries.getX()));
xSpeed = Math.cos(rads);
ySpeed = Math.sin(rads);
Asiantree Asiantree

2017/3/30

#
How can I detect a Fries object from within the Ant class?
if (getWorld().getObjects(Fries.class).size() != 0)
        {
            double rads = Math.atan2((double)(getY() - fb.getY()), (double)(getX() - fb.getX()));
            xSpeed = Math.cos(rads);
            ySpeed = Math.sin(rads);
        }
Something like this?
danpost danpost

2017/3/30

#
Asiantree wrote...
Is there a way I can access a non-static method from a static context? No. Non-static methods can only be accessed by way of an object created from the class (or an extension of that class).
how can I pull the Fries Y location when no main method is present in Greenfoot, based on this code? And how can I detect a Fries object from within the Ant class?
You can get a list of all fries object in the world by using from any Actor subclass:
List objs = getWorld().getObjects(Fries.class);
Provided the list is not empty, you can get access the x and y coordinates of the first Fries object in the list using:
int actorX = ((Actor)objs.get(0)).getX();
int actorY = ((Actor)objs.get(0)).getY();
By creating some local variables, one for the closest distance and one for a reference to the closest object, you can iterate through the list to find the closest one:
int closeness = 10000;
Actor closest = null;
for (Object obj : getWorld().getObjects(Fries.class))
{
    Actor actor = (Actor) obj;
    int dist = (int)Math.hypot(getX()-actor.getX(), getY()-actor.getY());
    if (closest == null || dist < closeness)
    {
        closest = actor;
        closeness = dist;
    }
}
if (closest != null)
{
    double rads= Math.atan2((double)(getY()-closest.getY()), (double)(getX()-closest.getX()));
    xSpeed = Math.cos(rads);
    ySpeed = Math.sin(rads);
}
Asiantree Asiantree

2017/3/30

#
Do I need two Fries objects present to test this? I have tested with one being present and no changes in direction were made for the Ant.
danpost danpost

2017/3/30

#
Asiantree wrote...
Do I need two Fries objects present to test this? I have tested with one being present and no changes in direction were made for the Ant.
Please show your revised class code.
Asiantree Asiantree

2017/3/30

#
public void act() 
    {
        badCalorie = badCalorie - 5;
        goodCalorie = goodCalorie - 1;
 
        exactX += xSpeed;
        exactY += ySpeed;
        setLocation ( (int) exactX, (int) exactY );
        
        if ((xSpeed < 0 && getX() <= getImage().getWidth() / 2) || (xSpeed > 0 && getX() >= getWorld().getWidth() - (getImage().getWidth() / 2))) 
        {
            xSpeed = -xSpeed;
        }
        if ((ySpeed < 0 && getY() <= getImage().getHeight() / 2) || (ySpeed > 0 && getY() >= getWorld().getHeight() - (getImage().getHeight() / 2))) 
        {
            ySpeed = -ySpeed;
        }
       int closeness = 10000;
       Actor closest = null;
       for (Object obj : getWorld().getObjects(Fries.class))
       {
           List objs = getWorld().getObjects(Fries.class);
           int actorX = ((Actor)objs.get(0)).getX();
           int actorY = ((Actor)objs.get(0)).getY();
           Actor actor = (Actor) obj;
           int dist = (int)Math.hypot(getX() - actor.getX(), getY() - actor.getY());
           if (closest == null || dist < closeness)
           {
               closest = actor;
               closeness = dist;
            }
        }
       if (closest != null) {
           turnTowards(closest.getX(), closest.getY());
           double rads= Math.atan2((double)(getY() - closest.getY()), (double)(getX() - closest.getX()));
           xSpeed = Math.cos(rads);
           ySpeed = Math.sin(rads);
        }
For the Ant class.
danpost danpost

2017/3/30

#
Remove lines 22 through 24. Those were incorporated into the final code at lines 20, 25 and 26 (the 'get' part is inferred within the loop).
Asiantree Asiantree

2017/3/30

#
Hmm, still no change in direction. In fact, the Ant moves away from the one Fries that is present in the world.
danpost danpost

2017/3/30

#
Maybe I got the order wrong. Try changing line 35 to this (quite sure I did, now that I look at it):
double rads = Math.atan2((double)(closest.getY()-getY()), (double)(closest.getX()-getX()));
Asiantree Asiantree

2017/3/30

#
Yes, that seemed to do the trick! Thank you very much danpost. But one last thing, how can I get rid of the first Fries object and then have the Ant go for the next one? In other words, where should I stick the removeObject method?
danpost danpost

2017/3/31

#
Asiantree wrote...
Yes, that seemed to do the trick! Thank you very much danpost. But one last thing, how can I get rid of the first Fries object and then have the Ant go for the next one? In other words, where should I stick the removeObject method?
"if touching, remove" anywhere in act.
Asiantree Asiantree

2017/3/31

#
Remove the variable "closest?" Sorry, just want to be exact in the code.
Super_Hippo Super_Hippo

2017/3/31

#
Well, you could remove the object which the variable holds:
//after line 33
if (intersects(closest))
{
    getWorld().removeObject(closest);
    return;
}
However, it could be better if this would be checked earlier:
//after line 17
if (isTouching(Fries.class)) removeTouching(Fries.class);

//or just

removeTouching(Fries.class);
Asiantree Asiantree

2017/4/2

#
Now, how can I get rid of other Ants that may be present using this code inside the Ant's act method?
You need to login to post a reply.