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

2018/3/19

I have an actor that moves on X-Axis while.....

Timmy_11 Timmy_11

2018/3/19

#
I have an actor that moves along the x axis while its direction is on an angle is there a way to fix that
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
Here is my Tank and World code

/**
 * 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"))
        {
            move(1);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            move(-1);
        }
        if (Greenfoot.isKeyDown("a"))
        {
            turn(-1);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            turn(1);
        }
    }
}
import greenfoot.*;  // (Actor, World, Greenfoot, GreenfootImage)

public class TankWorld extends World
{
    private GreenfootSound sound = new GreenfootSound("Background.wav");
    /**
     * Create the crab world (the beach). Our world has a size 
     * of 560x560 cells, where every cell is just 1 pixel.
     */
    public TankWorld() 
    {
        super(1000, 560, 1);
        setUp();
    }
    public void act()
    {
        
    }
    public void setUp()
    {
        Tank tank = new Tank();
        addObject(tank, 280, 280);
        TankTurret tankTurret = new TankTurret();
        addObject(tankTurret, 280, 280);
        Tracker track = new Tracker();
        addObject(track, 280, 280);
    }
}
Timmy_11 Timmy_11

2018/3/19

#
would it be something with the move???
Timmy_11 Timmy_11

2018/3/19

#
Should the tank move() be greater than 1???
danpost danpost

2018/3/19

#
Since the speed of the tank is one and you are limited to moving from pixel to pixel, the tank will move only horizontally or vertically (maybe along the great diagonals also). Only way to fix is to save location coordinates in some unit more fine than pixel units. You can make use of the SmoothMover class provided by greenfoot or something similar, like my QActor class, which provides smooth turning as well. You can find my class in my Asteroids w/Improved QActor Superclass scenario.
Timmy_11 Timmy_11

2018/3/19

#
what is the Math.atan2 is that inverse tan?
danpost danpost

2018/3/19

#
Timmy_11 wrote...
what is the Math.atan2 is that inverse tan?
Yes -- it takes a coordinate offsets of a point from the origin and returns the angle (or slope) in radians as a directional vector.
Timmy_11 Timmy_11

2018/3/19

#
would cos be Math.cos and sin be Math.sin then??
Yehuda Yehuda

2018/3/19

#
In Math class.
You need to login to post a reply.