Tutorials :: Distance Finding using Pythagoras :: Ultima Island: A friendly online community focusing on game development. Games, Tutorials, Resources, Articles, and much much more...

Welcome Guest [Submit Content] [Log In][Register]
Working on a game project? Why not sign up and start your own upcoming project site for free!
Tutorials :: Distance Finding using Pythagoras
Distance Finding using Pythagoras
15 Comments, 1892 Views
 rating: (17 votes)
Distance Finding using Pythagoras

This tutorial will show you how to use Pythagoras' theorem to find out the distance between two objects in RPG Maker. This tutorial was tested in Rpg Maker 2003, but the pythagorean technique should also work perfectly with Rpg Maker 2000 and Rpg Maker XP. Intermediate knowledge of variables is required.

Pythagoras' Theorem
Pythagoras' theorm basically states that, for any right angled triangle, the square of the length of the hypotenuse is equal to the sqaured lengths of the other two sides of the triangle. A squared number is simply a number multiplied by itself, i.e. the square of 7 is 7 multiplied by 7 which is 49.


a squared + b squared = c squared


The hypotenuse is the side of the triangle which is opposite to the right angle.

Applying Pythagoras to RPG Maker
You may be wondering how Pythagoras can be used to obtain distances. Well the great thing about this relationship is that if you have the length of two of the sides of a right-angled triangle you can obtain the third length. For example if the hypotenuse of a triangle has a length 5, and one of the other sides has length 4, you can calculate that the length of the remaining side must be 3:

5 squared = 4 squared + ??? squared
5 squared - 4 squared = ??? squared
25 - 16 = ??? squared
25 - 16 = 9
9 = ??? squared
square root of 9 = ??? = 3


You may still be confused. Where are the triangles in RPG Maker? Well there are no triangles on the screen, but by manipulating some variables we can create our own. If there are two objects on the screen, we can create an imaginary triangle between them that (in our heads) will look something like this:



Our triangle is a right-angled triangle, with the right angle between two lines; one going vertically and the other horizontally. The blue line in the image represents the hypotenuse, and is the actual distance between our objects. So it is the hypotenuse that we need to find. We can do this using pythagoras' theorem once we have the lengths of the other two sides - and for this we just do some co-ordinate fetching since Rpg Maker co-ordinates work in X (horizontally) and Y (vertically).

The Code Required
This tutorial will teach you how to simply display the distance between the hero and an object in a message box when the confirm button is pressed. If you want to incorporate this distance-finding technique into any other systems you will have to do that yourself.

We will need five variables.

Okay, create a new map with an event called "Object" with some sort of graphic so that you can see where it is and set the hero starting position in the same map. Then create an invisible event on the map, have it set to Parallell process and below hero. Firsly put in the following (the translation may be slightly different):

[Code]:
<> Wait Until Key Pressed


Everything after this command will be executed once you press the confirm key. Before we can start working out any distances with the help of our good friend Pythagoras, we first need to know what the lengths of the sides at the right angle of the triangle are. One of these lengths is the same as the horizontal distance between the hero and the object, while the other is the same as the vertical distance of the object. We will collect the lengths of these sides in 2 variables: Trig X and Trig Y. Firstly, the horizontal length:

[Code]:
<> Variable [0001: Trig X] Set Hero X Co-Ordinate
<> Variable [0001: Trig X] - Object X Co-Ordinate
<> Branch If Var [0001: Trig X] is Less than 0
   <> Variable [0001: Trig X] * -1
: End


This sequence subtracts the Object's X co-ordinate from the Hero's X co-ordinate to leave us with the distance between the two. If the hero is to the left of the object we will end up witha negative number, but by simply multpilying the number by -1 we solve that problem. The same needs to be done to gather our vertical distance:

[Code]:
<> Variable [0002: Trig Y] Set Hero Y Co-Ordinate
<> Variable [0002: Trig Y] - Object Y Co-Ordinate
<> Branch If Var [0002: Trig Y] is Less than 0
   <> Variable [0002: Trig Y] * -1
: End


Now that we have the lengths of the first two sides of our triangle, we can work out the length of the hypotenuse and thus the length of the distance between the hero and the object. To do this, we need to square our lengths and add them together to obtain the square of the hypotenuse.

[Code]:
<> Variable [0001: Trig X] * Var [0001: Trig X]'s Value
<> Variable [0002: Trig Y] * Var [0002: Trig Y]'s Value
<> Variable [0003: Trig Sum] Set Var [0001: Trig X]'s Value
<> Variable [0003: Trig Sum] + Var [0002: Trig Y]'s Value


So now our third variable, Trig Sum, has a value equal to our hypotenuse squared. Here is the complication: RPG Maker does not have a square root function. This means we cannot simply square root this value to get the length of the hypotenuse. Instead we have to create a loop which goes counts up from 0, squaring each number as it goes along, until it finds a number with a square which is greater than or equal to the value of our Trig Sum variable. This can be easily achieved with one label and a new variable called Trig H:

[Code]:
<> Variable [0004: Trig Counter] Set 0
<> Label 1
<> Variable [0005: Trig H] Set Var [0004: Trig Counter]'s Value
<> Variable [0005: Trig H] * Var [0005: Trig H]'s Value
<> Branch If Var [0005: Trig H] is Var [0003: Trig Sum]'s Value or Greater
   <> Message: The distance is V[4]
: Else Handler
   <> Variable [0004: Trig Counter] +1
   <> Jump to Label 1
: End


This code works. I haven't experienced any lag with it on a small map, but on a large map and a slow computer there may or may not be some very slight delay if the distance is very big, because the code loops through each number until it finds the right one. Lag is extremely unlikely though, unless the objects are hundreds of tiles away and the computer is low-end too.

Here is the complete code for the parallell proces event:

[Code]:
<> Wait Until Key Pressed
<> Variable [0001: Trig X] Set Hero X Co-Ordinate
<> Variable [0001: Trig X] - Object X Co-Ordinate
<> Branch If Var [0001: Trig X] is Less than 0
   <> Variable [0001: Trig X] * -1
: End
<> Variable [0002: Trig Y] Set Hero Y Co-Ordinate
<> Variable [0002: Trig Y] - Object Y Co-Ordinate
<> Branch If Var [0002: Trig Y] is Less than 0
   <> Variable [0002: Trig Y] * -1
: End
<> Variable [0001: Trig X] * Var [0001: Trig X]'s Value
<> Variable [0002: Trig Y] * Var [0002: Trig Y]'s Value
<> Variable [0003: Trig Sum] Set Var [0001: Trig X]'s Value
<> Variable [0003: Trig Sum] + Var [0002: Trig Y]'s Value
<> Variable [0004: Trig Counter] Set 0
<> Label 1
<> Variable [0005: Trig H] Set Var [0004: Trig Counter]'s Value
<> Variable [0005: Trig H] * Var [0005: Trig H]'s Value
<> Branch If Var [0005: Trig H] is Var [0003: Trig Sum]'s Value or Greater
   <> Message: The distance is V[4]
: Else Handler
   <> Variable [0004: Trig Counter] +1
   <> Jump to Label 1
: End


That's all the code you need. With a little bit of Pythagoras we have created a code which can work out the distance between two objects. You could use this for a many things - eg guard awareness. You could get the guard to chase the hero if the distance is less than a certain amount, for example. You could do a fireball spell which deals damage to an enemy if it's within a certain distance. The possibilities are limitless.

Also, since RPG Maker automatically rounds fractions into whole numbers for you, we don't have to do any extra working. Here is a colour-keyed grid which I made, displaying what distances will be returned if the hero is standing on teach square while the object is on the white tile in the middle.



I hope you find this tutorial useful. More of my tutorials from now on are going to be maths based, so look out for them when they arrive. Thank you for reading.
submitted by Seraph
You are not permitted to rate tutorials...
 
Latest Comments
Seraph (Offline)
Yeah I know about the relationships between sine, cosine and tan, but I don't see how they'd help. Like you said Rpg Maker doesn't have any sin functions so it's gonna be a case of trial and error I think to get it perfect.

I've also just had a thought.. it will require more RPG Maker checking as well as just the maths with the variables, e.g. checks to see if the angle is at any of the 90 degree angles first with co-ordinate checks, because if the two objects have the same x or y co-ordinate they will be on the axes, not in any of the quadrants, and therefore you'll end up with a division by 0.

This is going to be a little harder than I first thought, but it will be done.
LOLninja (Offline)
Oh yeah, divide by 0... I remember having a problem with that. I just skipped the dividation(sp?) if x=0 and put a large number in te variable where the answer should come.

Anyway, once you've done the angles, you should be able to rotate coordinates from the origin. How cool is that.
Seraph (Offline)
I just noticed that for this tutorial you don't actually need to multiply the X or Y distances by -1 because when squared they become positive anyway. Oh well, I can't be bothered to fix it, it's a handy measure that people should learn to take for other stuff.
Barbovic (Offline)
The article rocks. You rock. The site too btw. Rock on!
ECNM (Offline)
I'm embarrassed I didn't think of this myself. This opens up so many possibilties. In fact, it helps with an A.I. problem I've been having. Great tutorial.
Seraph (Offline)
Thanks alot Barbovic, and thanks ECNM.
MewMew (Offline)
Hmmm...
Interesting
I'm starting to feel inspired! ^^
datopher64 (Offline)
sweet! i can use this for a certin minigame in my game!! i can't say what it is YET though.....
Seraph (Offline)
I am glad you like, Datopher Heya!
Felixrpg (Offline)
I wish I'd have paid more attention in my math's lessons at school now. Great tut yet again Seraph! The uses of this system are too numerous to name but suffices to say; ABS's NEED THIS!
Quick User Panel
Username:

Password:

Welcome Guest, please login or register

Change Style:
Shout Box
Gretgor says:
You did it wrong, man. Anyways, you turned a frog into a screwdriver.
dragoniqbal says:
LOLZ! I tried.. Ayy Gretgor, i can turn a frog into a
Gretgor says:
MAY A NEW WORLD OF GAMING BE BORN FROM THE ASHES OF... UHHH.... Someone finish this sentence for me, thanks.
Lemon_EX says:
Sorry, wrong web site.
Gretgor says:
Well, fix that, then.
Miscellaneous

Donate

RSS Feed

Play-Asia.com - Buy Video Games for Consoles and PC - From Japan, Korea and other Regions!