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

2018/7/31

Math.hypot

Ticker175 Ticker175

2018/7/31

#
Hey guys am currently makeing a pacman game and have been told to use math.hypot to make the ghosts track pacman and chase him. i have never seen this command or even knew that "advanced" math was a thing in greenfoot any help on this command (in its use/parameters) would be greatly appreciated Any help on how to impliment it into my game would be even better
Super_Hippo Super_Hippo

2018/7/31

#
If you have Actor a and Actor b, then you can calculate the Euclidian distance between them with:
1
double distance = Math.hypot(a.getX()-b.getX(), a.getY()-b.getY());
It is the same as doing:
1
double distance = Math.sqrt(Math.pow(a.getX()-b.getX(), 2)+Math.pow(a.getY()-b.getY(), 2));
a² + b² = c² In this case: Δx² + Δy² = distance² distance = √(Δx² + Δy²) In your code, you will have a ghost (Actor a) and Pacman (Actor b). So you only need to get a reference to the Pacman object because the ghost doesn't manually get a reference to itself, you can just use getX() and getY().
Ticker175 Ticker175

2018/7/31

#
awesome thanks ill try that out and see how it goes
danpost danpost

2018/7/31

#
Ticker175 wrote...
any help on this command (in its use/parameters) would be greatly appreciated
See here.
danpost danpost

2018/7/31

#
Ticker175 wrote...
any help on this command (in its use/parameters) would be greatly appreciated
See here.
i have never seen this command or even knew that "advanced" math was a thing in greenfoot
Its not. It is a thing in java -- in its Math package (automatically accessible to all projects through java.lang.
Ticker175 Ticker175

2018/7/31

#
hey i just realized i have no idea how to reference any info that that code gives me do i use if???? Thanks Thomas
Ticker175 Ticker175

2018/7/31

#
danpost wrote...
Ticker175 wrote...
any help on this command (in its use/parameters) would be greatly appreciated
See here.
i have never seen this command or even knew that "advanced" math was a thing in greenfoot
Its not. It is a thing in java -- in its Math package (automatically accessible to all projects through java.lang.
thanks i had a hunch it was a package/plugin but that link is awesome ive bookmarked it and ill probably read up when i get a chance =) Thanks Thomas
Super_Hippo Super_Hippo

2018/7/31

#
What you want to do with that method is to check how close Clyde is to Pacman to determine if his target field will be Pac-Man's current field or his home corner.
Ticker175 Ticker175

2018/7/31

#
thanks i was mainly asking about blinky and how to acualy use the math.hypot to get the best direction to move and start tracking/chaseing pacman
Super_Hippo Super_Hippo

2018/7/31

#
Ah right. That was the problem. You are at a junction and don't know which way to go. To do it, move the ghost in each direction (one "field") and check the distance from each of the positions (max. 4) to Pac-Man (or the other relevant fields depending on the ghost type including home corner field -- using Math.hypot). Move it back to where it was and choose the direction in which the calculated distance was the shortest.
danpost danpost

2018/7/31

#
Super_Hippo wrote...
move the ghost in each direction (one "field") and check the distance from each of the positions (max. 4) to Pac-Man
It is not necessary to move the ghost to calculate the different distances -- you can just calculate where the ghost will be if moved in a direction.
Super_Hippo Super_Hippo

2018/7/31

#
I just checked in my code and I actually did it like this, too. However, I apparently wasn't aware of the hypot method when I created the scenario and used the long way instead.
Ticker175 Ticker175

2018/7/31

#
ok so i should have 4 if statements (right, left, up, down) that are triggered if that direction is available. and if more than one is triggered, use math.hypot to determine which would get me the closest and to check like the above 2 comments mention RIght:
1
double distance = Math.hypot(getX()+30 - pacX , getY() - pacY);
Left:
1
double distance = Math.hypot(getX()-30 - pacX , getY() - pacY);
Up:
1
double distance = Math.hypot(getX() - pacX , getY()-30 - pacY);
Down:
1
double distance = Math.hypot(getX() - pacX , getY()+30 - pacY);
Thanks Thomas
You need to login to post a reply.