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

2018/3/20

Strange navigation of tank

Timmy_11 Timmy_11

2018/3/20

#
I converted the normal polar coordinates used in moving a actor to Cartesian coordinates now the tank will work until it hits a certain angle then the direction will completely change here is my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Executer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Executer extends Animal
{
    public Executer()
    {
        getImage().setTransparency(0);
    }
    double ex = 280;
    double ey = 280;
    double rx;
    double ry;
    public void move(int r) //takes in rotation
    {
        rx = Math.cos(r);
        ry = Math.sin(r);
        ex += rx;
        ey += ry;
        Tank tank = (Tank)getWorld().getObjects(Tank.class).get(0);
        tank.setLocation((int) ex, (int) ey);
    }
    public void back(int r) //takes in rotation
    {
        rx = Math.cos(r);
        ry = Math.sin(r);
        ex -= rx;
        ey -= ry;
        Tank tank = (Tank)getWorld().getObjects(Tank.class).get(0);
        tank.setLocation((int) ex, (int) ey);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tank extends Animal
{
    
    /**
     * Act - do whatever the Tank wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        navigate();
    } 
    public void navigate()
    {
        if (Greenfoot.isKeyDown("w"))
        {
            Executer execute = (Executer)getWorld().getObjects(Executer.class).get(0);            
            execute.move(getRotation());
        }
        //if (Greenfoot.isKeyDown("s"))
        //{
        //    Executer execute = (Executer)getWorld().getObjects(Executer.class).get(0);
        //    execute.back(getRotation());
        //}
        if (Greenfoot.isKeyDown("a"))
        {
            turn(-1);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            turn(1);
        }
    }
}
Timmy_11 Timmy_11

2018/3/20

#
I would have used smooth mover but couldn't find a place to down load it
danpost danpost

2018/3/20

#
Each value of sin and cos functions represent two angles around a circle. You are not getting good results for half of the directions.
Timmy_11 wrote...
I would have used smooth mover but couldn't find a place to down load it
You should be able to directly import the SmoothMover class by way of the menubar -- Edit>Import class... . You can use my QActor class as an alternative. I have it available to be used as a support class. Just download the Asteroids scenario and open it is greenfoot to access the class codes.
davmac davmac

2018/3/20

#
Each value of sin and cos functions represent two angles around a circle. You are not getting good results for half of the directions.
While that's true for individual values, as a pair they uniquely identify an angle. I think the issue is that Math.sin() and Math.cos() expect an angle in radians, but the value they passed (which comes from getRotation()) is in degrees. If the angle was converted to radians, it should probably be ok:
        rx = Math.cos(Math.toRadians(r));
        ry = Math.sin(Math.toRadians(r));
danpost danpost

2018/3/20

#
davmac wrote...
While that's true for individual values, as a pair they uniquely identify an angle.
Thank you for that polite reminder.
Timmy_11 Timmy_11

2018/3/20

#
Thank you so much!!!! My tank now navigates reliably.
You need to login to post a reply.