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

2012/4/11

Turn Object to direction of mouse

1
2
09blackn 09blackn

2012/8/14

#
i have a class called man and i want him to follow the mouse only when (W) is pressed can anyone help Thanks :)
SPower SPower

2012/8/14

#
if (Greenfoot.isKeyDown("w")) {
     MouseInfo m = Greenfoot.getMouseInfo();
     if (m != null) {
          turnTowards(m.getX(), m.getY());
     }
}
09blackn 09blackn

2012/8/14

#
getting this error" cannot find symbol - method turn towards (int,int) i have invoked it but keep getting this error
SPower SPower

2012/8/14

#
You need Greenfoot 2.2.*. This code also has to be in a subclass of Actor (if you didn't do that yet :) )
Gevater_Tod4711 Gevater_Tod4711

2012/8/20

#
Hi, thats not so easy I think: Well to programm a mous folower is not the problem. It's more the math you need. You first need this method:
    public void turnTo(int x, int y) {
        setDirection(x - getX(), y - getY());
    }

public void setDirection(int deltaX, int deltaY) {
        int direction;
        if(deltaX == 0 && deltaY == 0) {
            direction = 0;
        }
        else if(deltaX == 0 && deltaY < 0) {
            direction = 270;
        }
        else if(deltaX == 0 && deltaY > 0) {
            direction = 90;
        }
        else if(deltaX > 0 && deltaY == 0) {
            direction = 0;
        }
        else if(deltaX < 0 && deltaY == 0) {
            direction = 180;
        }
        else {
            int w = (int) Math.toDegrees(Math.atan((double)deltaY / (double)deltaX));
            if(deltaX > 0) {
                direction = w;
            }
            else {
                direction = 180 + w;
            }
        }
        setRotation(direction);
    }
now your actor will turn to the coordinates you give to the method turnTo(). now you just need to know where the mouse is. Thats not the problem: just add this at the top of your act method:
MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse!=null){
            x = mouse.getX();
            y = mouse.getY();
        }
you need the variables x and y. then you have the coordinates of the mouse and still have to give them to the turnTo method. That should work.
SPower SPower

2012/8/20

#
@Gevater_Tod4711 Yes, it is that simple. Look at the Actor doc when you have Greenfoot 2.2 or higher, and you'll see the turnTowards method.
Gevater_Tod4711 Gevater_Tod4711

2012/8/20

#
@SPower Oh I didn't knew this method existed. I downloaded the new Version just before a view days and I didn't see this method. Thanks
Kampf-Keks-Studios Kampf-Keks-Studios

2014/3/15

#
For those of you, who has not so specific knowlage: turnTowards(Greenfoot.getMouseInfo().getX(),Greenfoot.getMouseInfo().getY()); //Why be difficult, when it can be simple
danpost danpost

2014/3/15

#
Kampf-Keks-Studios wrote...
For those of you, who has not so specific knowlage: turnTowards(Greenfoot.getMouseInfo().getX(),Greenfoot.getMouseInfo().getY()); //Why be difficult, when it can be simple
Still have to check and make sure the MouseInfo object returned is not 'null' or exceptions will ensue.
You need to login to post a reply.
1
2