1
\$\begingroup\$

I'm using slick2d for this process and trying to rotate a minigun to face the mouse. My problem is that the minigun in my game is actually rotating but no towards the mouse at all! Also, the minigun goes as far as 180 degrees and stops there.

Here is my minigun class that I have put inside another core class to be updated and rendered.

public class Minigun {

    private Animation Shoot;
    private SpriteSheet sheet;
    private int x = 200, y = 150;
    private double angleToTurn;
    private Input input;

    public Minigun(){
        try {
            sheet = new SpriteSheet(new Image("res/minigun.png"), 130, 34);
            Shoot= new Animation(sheet, 1, 0, 2, 0, true, 1, true);
            Shoot.setLooping(true);
            input = new Input(1);
        } catch (SlickException e) {
            e.printStackTrace();
        }

    }
    public void render(Graphics g){
        g.pushTransform();
        g.rotate(x + Shoot.getWidth() / 2 , y + Shoot.getHeight() /2,   (int) angleToTurn);
        Shoot.draw(x, y);
        g.popTransform();
    }
    public void update(){
        double mX = input.getMouseX();
        double mY = input.getMouseY();
        angleToTurn = Math.toDegrees(Math.atan2(mY - y, mX - x));
        System.out.println(angleToTurn);
    }
}

As you can see the minigun follows the mouse in the general direction of the x axis, but never actually does a 360 degrees turn to align properly.

\$\endgroup\$
1
\$\begingroup\$

I figured out the problem myself! After printing the coordinates of the mouse to the console I realized that where just something missing with my Input. Sure enough, I added GameContainer to the constructor of minigun and set input to equal GameContainer.getInput();. It works perfectly now :D

public Minigun(GameContainer container){
        try {
            sheet = new SpriteSheet(new Image("res/minigun.png"), 130, 34);
            Shoot= new Animation(sheet, 1, 0, 2, 0, true, 1, true);
            Shoot.setLooping(true);
            input = new Input(1);
            input = container.getInput();
        } catch (SlickException e) {
            e.printStackTrace();
        }
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.