Menu

Strange error with ScannedRobotEvent? Help!

Help
erber
2010-11-30
2012-09-15
  • erber

    erber - 2010-11-30

    Hello internet

    in my

    public void onScannedRobot(ScannedRobotEvent e)
    

    event, I can't call certain values like

    getBearing()
    

    and

    getVelocity()
    

    .

    However, different

    ScannedRobotEvent
    

    values can be retrieved, like

    getEnergy()
    

    .

    Heres my whole ScannedRobotEvent, with the broken parts commented:

        public void onScannedRobot(ScannedRobotEvent e) {
            // radar locking code
            double life = getBearingRadians();
            System.out.println("his life is " + life);
            int i = 0;
            double turnBack = getHeadingRadians() + e.getBearingRadians() - getRadarHeadingRadians();
            setTurnRadarRightRadians(Utils.normalRelativeAngle(turnBack));
            boolean left, right;
    
            //make sure he is always moving 90 degrees from the enemy, and only shoots if the gun is pointing close enough to the radar
            //trouble with this, will often go into endless spin or jam aaginst walls D:
            // the if velocity isnt 0 should fix all of this, hopefully
    
            //if(i > 0){
                //if(ScannedRobotEvent.getBearing() >= oldPlace){
                    //right = true;
                //}
                //if(ScannedRobotEvent.getBearing() <= oldPlace){
                    //left = true;
                //}
            //}
    
            //double oldPlace = ScannedRobotEvent.getBearing();
    
            i++;
    
                if(getX() > 600 || getX() < 200 || getY() > 600 || getY() < 200){
    
                        dir = -dir;
    
                }
    
            if(getVelocity() == 0){
                setAhead(Double.POSITIVE_INFINITY * dir);
                System.out.println("starting to move again");
            }
    
            if(getRadarHeadingDegrees() > getGunHeadingDegrees() - 3 && getRadarHeadingDegrees() < getGunHeadingDegrees() + 3){
                //prerequisit for shooting, assuming radar is locked on enemy
                //if(ScannedRobotEvent.getDistance() > 500){
                    //pow = 1;
                //}
                //if(ScannedRobotEvent.getDistance() < 500){
                    //pow = 2;
                //}
                //if(ScannedRobotEvent.getDistance() < 200){
                    //pow = 3;
                //}
                //setFire(pow);
                //System.out.println("firing. power level: " + pow);
            }
            if(getRadarHeadingRadians() > getGunHeadingRadians()){
                setTurnGunRight(5);
                System.out.println("turning gun right");
            }
            if(getRadarHeadingRadians() < getGunHeadingRadians()){
                setTurnGunLeft(5);
                System.out.println("turning gun left");
            }
            if(getRadarHeadingDegrees() > getHeadingDegrees() + 90){
                setTurnRight(10 * dir);
                System.out.println("turning right");
            }
            if(getRadarHeadingDegrees() < getHeadingDegrees() + 90){
                setTurnLeft(10 * dir);
                System.out.println("turning left");
            }
            System.out.println("ran all scanned ifs");
    
        }
    

    Anyone else had this problem? Any info will be appreciated.

     
  • erber

    erber - 2010-11-30

    oh god this is hideous how do I clean it up? I don't see an edit button

     
  • Flemming N. Larsen

    It is a bit unclear to me, what the problem is. That is, what you cannot do,
    which you want to do?

    For example, in your code you write:

            // radar locking code
            double life = getBearingRadians();
            System.out.println("his life is " + life);
    

    But getBearingRadians() returns the bearing for your robot, not the scanned
    enemy. In addition, you get the life with the getEnergy() method, not
    getBearingRadians(). So if you want to write out the energy of the scanned
    enemy (his life), you should write "double life = e.getEnergy();" instead.
    Hence, I am a bit confused.

    Try to explain it with a smaller code example so it does not get too
    complicated. :-)

     
  • screamin

    screamin - 2010-12-03

    in my

    public void onScannedRobot(ScannedRobotEvent e)

    event, I can't call certain values like

    getBearing()

    AFAIK, there are no getBearing() or getBearingRadians() methods in the Robot
    class (or any of it's subclasses). It makes sense when you think about it. Why
    would you want to get bearing to yourself? :)

    Also, in Java, you must initialize all local variables. Otherwise it won't
    compile. For example

    void someMethod() {
        boolean left;
        if (/*some condition*/) {
            left = true;
        }
    }
    

    The Java compiler won't let you get away with something like that, and that's
    exactly what you're doing in your code.

    As for getVelocity() method -- it can be called anywhere within a class that
    extends Robot, of course. I do it all the time. :)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.