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
PStigerID PStigerID

2012/4/11

#
I have a Actor which should always turn towards the mouse (not move along with it, just turn). Can this be done using Greenfoot and how? Thanks PStiger
danpost danpost

2012/4/11

#
Sure it can! First check to see if the mouse has moved, and if it has (did not return 'null'), then save the MouseInfo to a MouseInfo variable (let us just call it 'mi', for MouseInfo); now, you can use mi.getX() and mi.getY() to get the latest location of the mouse pointer, and you have the Actors getX() and getY() methods to get its location. Use Math.atan2(xDifference, yDifference) to get the angle. Change the angle to degrees and turn your actor.
PStigerID PStigerID

2012/4/11

#
danpost wrote...
Use Math.atan2(xDifference, yDifference) to get the angle. Change the angle to degrees and turn your actor.
I understand everything untill the math.atan2 part and convert it into an angle. Would it be to much to ask you to quickly code this part for me? :P Thanks PStiger
danpost danpost

2012/4/11

#
1
2
3
4
5
6
7
8
9
10
11
12
public void turnTowards (int x, int y)
{
    double dx = x - getX();
    double dy = y - getY();
    double angle = Math.atan2(dy,dx)*180.0/Math.PI;
    setRotation( (int)angle );
}
 
public void turnTowards (MouseInfo mi)
{
    turnTowards(mi.getX(), mi.getY());
}
Razzo Razzo

2012/4/11

#
Trigonometry fur life (Makes math related gang sign)
PStigerID PStigerID

2012/4/11

#
Perfect. Thank you so much!!!!! PStiger
PStigerID PStigerID

2012/4/11

#
Sorry - but this is (after some testing) not quite working. I needed to change your code, danpost, because I need the object to turn, not to rotate (so that when i let it move it moves to that direction - see lines 6-9. If I change it back to
1
setRotation
it works perfectly. Just the
1
turnTo
doesnt work - it only lets me turn 180° :( Here is my current code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void act()
    {
        MouseInfo m = Greenfoot.getMouseInfo(); 
        if(m != null)
            turnTowards(m);
        if (Greenfoot.isKeyDown("w"))
            move(2);
        if (Greenfoot.isKeyDown("s"))
            move(-2);
    }   
public void turnTowards (MouseInfo mi) 
    
        turnTowards(mi.getX(), mi.getY()); 
    
public void turnTowards (int x, int y) 
    
        double dx = x - getX(); 
        double dy = y - getY(); 
        double angle = Math.atan2(dy,dx)*180.0/(Math.PI);
        turnTo( (int)angle ); 
    }
Thanks again PStiger
Razzo Razzo

2012/4/11

#
PStigerID > Are you looking to do something like I have done here? > http://www.greenfoot.org/scenarios/4204
PStigerID PStigerID

2012/4/11

#
YES!!!!! - but I cant download it to look at the code. :((
PStigerID PStigerID

2012/4/11

#
If you wouldn't mind showing me how you have done this - I could really use this Razzo!
Razzo Razzo

2012/4/11

#
Okay Lemme find the code
Razzo Razzo

2012/4/11

#
http://www.greenfoot.org/scenarios/4773 There you go, I released the source You can find the mouse code In the Player Class, where if(mouse != null) Good luck!
PStigerID PStigerID

2012/4/11

#
Thank you so much!!!!
Razzo Razzo

2012/4/11

#
If you liked it (Or Helped you) , Please +1 Like the Scenario :), It means alot to me!
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 :)
There are more replies on the next page.
1
2