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

2014/4/26

How does the move() Greenfoot method calculate where to move the player?

Entity1037 Entity1037

2014/4/26

#
I'm trying to make another collision engine game, however in order to do this I need to know how position is changed by the move() method based on the direction it's facing. If I try to make my game without detecting ahead of the player, it will make a LOT of glitches happen! I don't know where to view to code for the move() method. Could someone please help?
bourne bourne

2014/4/26

#
Greenfoot Source wrote...
public void move(int distance) { double radians = Math.toRadians(rotation); // We round to the nearest integer, to allow moving one unit at an angle // to actually move. int dx = (int) Math.round(Math.cos(radians) * distance); int dy = (int) Math.round(Math.sin(radians) * distance); setLocation(x + dx, y + dy); }
You can find the Greenfoot source code here: http://www.greenfoot.org/site/download_source
danpost danpost

2014/4/26

#
This is how the Actor class method does 'move(int)':
1
2
3
4
5
6
7
public void move(int distance)
{
    double radians = Math.toRadians(getRotation());
    int dx = (int) Math.round(Math.cos(radians) * distance);
    int dy = (int) Math.round(Math.sin(radians) * distance);
    setLocation(getX() + dx, getY() + dy);
}
Entity1037 Entity1037

2014/4/26

#
Ah, I totally forgot about trigonometry! Thank you!
You need to login to post a reply.