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

2012/11/29

Scanning objects

Future Future

2012/11/29

#
Hi ! I am trying to make a robot that will scan the page and find where the bomb is and automatically push it to the redbricks square/rectangle... this is the code public void act() { //move(20); if (getX()==595) { turn(180); } else if (getX()==99) { turn(180); Greenfoot.stop(); } if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("right")) { turn(3); } if (Greenfoot.isKeyDown("up")) { move(5); } } public boolean foundBomb() { return getOneObjectAtOffset(0, 0, bomb_1.class) !=null; } } can anyone help please? I kind of figured out how to make it push the bomb to the square using the keyboard but it is not working properly, the main concern is how to make it scan the page using the act button and then it will do everything... can anyone help please?
Gevater_Tod4711 Gevater_Tod4711

2012/11/29

#
to find the bomb you should use the method turnTowards (link to the API). to let your roboter make the bomb move to a specific position you should make him move not directly to the bomb but to a position near to it, so that you then can make the bomb move to the field. to get this position you first have to know the angle from the bomb to the field using a tangens funktion:
public void moveToBomb() {
    //you have to know the position of the bomb and the field to do that or you use the reference to the bomb and the field;
    int x;
    int y;
    double angle = Math.toRadians(Math.atan((bomb.getY()-field.getY())/(bomb.getX()-field.getX()));
    x = (int) Math.round(bomb.getX() + Math.cos(angle) * 25);
    y = (int) Math.round(bomb.getY() + Math.sin(angle) * 25);
    turnTowards(x, y);
    //let the roboter move to the position using the act method; the rotation is right already so you just have to move;
}
Then if your roboter has reached this position let him turn to the field:
//again you need the reference to the field;
turnTowards(field.getX(), field.getY());
then just make him move there and he'll bring your bomb there.
Future Future

2012/12/27

#
I have tried to use the code that you gave me but I am not sure how to use it.. public void moveToBomb() { int x; int y; double angle = Math.toRadians(Math.atan((bomb.getY()-field.getY())/(bomb.getX()-field.getX()))); x = (int) Math.round(bomb.getX() + Math.cos(angle) * 25); y = (int) Math.round(bomb.getY() + Math.sin(angle) * 25); turnTowards(x, y); } i copied the code that you gave me . but im not sure how to put the cords of the bomb there.. whicch are X is 180 and Y is 100. if (moveToBomb() == true) { turnTowards(field.getX(), field.getY()); } is the second one but it gave me an error about it.. i dunno why either
Gevater_Tod4711 Gevater_Tod4711

2012/12/27

#
Well your error probably is because you havend got the references to the bomb and the field. Are the references bomb and field declared before? Or are they just initialised with null? In this case you have to get the right references. If there is only one bomb and only one field you could get them like this:
List<Bomb> bombs = getWorld().getObjects(Bomb.class);
List<Field> fields = getWorld().getObjects(Field.class);//Bomb and Field have to be the classnames. If they are named somehow else use the right names;
Bomb bomb; //the reference to your bomb;
Field field; //the reference to your field;
if (!bombs.isEmpty()) {
    bomb = bombs.get(0);//gets the first bomb from the list bombs;
}
if (!fields.isEmpty()) {
    field = fields.get(0);//the filst field of the list;
}
If you add this code to your method it should work.
You need to login to post a reply.