User Tag List

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16

Thread: Distance Formula

  1. #1
    Clicker Multimedia Fusion 2

    Join Date
    Sep 2006
    Location
    Britain, South Coast
    Posts
    1,030
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Distance Formula

    [color:#0088aa][size:17pt]Distance Formula[/size][/color]
    [color:#0088aa]How Distance Formula Works[/color]

    [color:#000099]

    The purpose of this article is to explain how to work out the distance between two points. Some maths is involved, but I'll try and explain it as simply as possible (I've got dyscalculia, of which fact I am immensely proud, so don't go saying you're bad at maths and therefore can't do it. ).

    So anyway, it really is pretty straightforward, so let's start.

    [size:14pt][color:#0088aa]Our Objects[/color][/size]

    Here are our two objects. We're gonna find the distance between them:

            &n bsp;       &nbs p; 

    First bit is pretty straightforward, I'm assuming you get the stuff about X and Y axes (x goes along the top, y goes down the left).

    So if we count how far apart they are on the X and Y axis, we get this:

    X distance = 6 squares apart
    Y distance = 2 squares apart

    We can do that with the following MMF expressions:

    Code:
    x("Object A") - x("Object B") //The X Distance
    
    
    y("Object A") - y("Object B") //The Y Distance
    Simply put, we're taking the coordinates of one object, and subtracting them from another. There will obviously be a difference left over between the two, and that is our distance.

    So far so good, but this gives us two distances. One for the X axis, one for the Y axis. We need to combine them into one single value.

    To do that, we need to move on and see how - funnily enough - this whole thing has a lot to do with Triangles.


    [size:14pt][color:#0088aa]Triangles, You Say?[/color][/size]

    Oui, monsieur. Sim, senhor. Ja, mein herr. Etc.

    Look at our diagram of the two objects now. For simplicity, I've named them A and B as well, so we can tell them apart:

            &n bsp;       &nbs p; 

    We know the X and Y distances, we can find them easily as I showed above, by taking the coordinates of the one object and subtracting it from the other. The remaining amount is the distance between the two on that axis.

    But now, we must combine them. How? Well do you notice how we're forming a kind of triangle? The top (dotted) line is the only line whose length we don't already know.

    How handy it is, then, that about 2580 years ago, a Greek chap called Pythagoras found out just how to deal with our very problem.


    [size:14pt][color:#0088aa]What The Greek Dude Said About Triangles[/color][/size]

    Pythagoras found out that, in a right-angled triangle (the kind we're dealing with here), the angled line (called the Hypotenuse) has a very basic relationship to the two straight lines.

    To explain it better, look at this diagram from Wikipedia:

            &n bsp;       &nbs p;                 &n bsp;  

    On this diagram, a is the Y Distance, and b is the X Distance. c is the total distance, which we want to find out.

    Pythagoras takes the X Distance, and the Y Distance, and he uses them to create a couple of squares (the straight a and b squares you see on the diagram).

    He then adds them together, creating a kind of Super Square (a bigger square, basically ).

    That Super Square will look very much like square c, and the length of each one of its sides will be... the length of the distance that we are looking for.


    Confused? It will become simpler We'll do a worked example.


    [size:14pt][color:#0088aa]Worked Example[/color][/size]

    So, we're gonna work through the example we mentioned at the outset, and then there'll be a nice example file for you to look through at your leisure.

            &n bsp;       &nbs p; 

    STEP ONE

    Okay, so as we did above, count how many rows (Y) and columns ( the two objects are apart in our diagram. We'll imagine each grid square on the diagram is a pixel.

    You'll find they are:

    X distance = 6 pixels apart
    Y distance = 2 pixels apart

    In MMF, we do this:
    Code:
    X Distance = x("Object A") - x("Object B")
    Y Distance = y("Object A") - y("Object B")
    STEP TWO
            &n bsp;       &nbs p;                 &n bsp;  

    Turn those distances into squares. You can do this in Windows Calculator by typing in the distances and squaring them. The result:

    X distance = 6▓ = 36 pixels
    Y distance = 2▓ = 4 pixels

    ...so now we have the area of two squares, right?

    In MMF, there is no square button, but you can type 'POW 2' to get the same effect. So:

    Code:
    X square = X Distance POW 2
    Y square = Y Distance POW 2

    STEP THREE

    Add the two squares together.

    Big Square = 36 + 4 = 40 pixels

    So now we have the area of our even-bigger-square, our Super Square.

    I'm sure I don't need to tell you that in MMF it would be:

    Code:
    Big Square = X Square + Y Square

    STEP FOUR

    Now that we have our Big Square, we need to find out how long its sides are. So bring up your Windows Calculator, and use the Square Root function. This is like flattening the square.

    Flattened Big Square = √40 = 6.3245 pixels

    And that's the total distance!!

    In MMF that's:
    Code:
    Total Distance = SQR(Big Square)
    And that's it!!



    The Final Formula Becomes...

    Code:
    sqr(
        ( x("Object A") - x("Object B") ) POW 2
      + ( y("Object A") - y("Object B") ) POW 2
                                                )
    Heh... That may well have confused you, but even if it has, the basis is there in your head now.

    Look in the attached example, and hopefully seeing it in practical use will make things a little better. It also comes with examples of cool ways you can use this formula.

    You can of course do all of this using the Direction Calculator Object, but it can be useful to know how to do it without.

    I hope this helped some, any comments on how I can improve this article are appreciated
    [/color]
    Attached files Attached files

  2. #2
    No Products Registered

    Join Date
    Dec 2006
    Posts
    1,332
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Distance Formula

    Yet another great article from Dines. The only problem I have with it is the X and Y distances part. You have:

    X Distance = x("Object A") - x("Object B")
    Y Distance = y("Object A") - y("Object B")

    but shouldn't it be:

    X Distance = abs(x("Object A") - x("Object B"))
    Y Distance = abs(y("Object A") - y("Object B"))

    Just so you don't end up with negative values incase object B has a larger position value than object A.

  3. #3
    Clicker Fusion 2.5

    Join Date
    Jun 2006
    Location
    Stockholm, Sweden
    Posts
    1,529
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Distance Formula

    Yep Brandon is probably right. It should be absolute values.

    I can't understand how you can write a such big article about this.

  4. #4
    Clicker Multimedia Fusion 2

    Join Date
    Sep 2006
    Location
    Britain, South Coast
    Posts
    1,030
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Distance Formula

    Lol, yeah, oops :blush:

  5. #5
    No Products Registered

    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    1,141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Distance Formula

    Abs() is not needed, when you multiply something with itself the answer is always positive. -2 * -2 = 4

  6. #6
    Clicker Fusion 2.5

    Join Date
    Jun 2006
    Location
    Stockholm, Sweden
    Posts
    1,529
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Distance Formula

    LoL that's true! xD

  7. #7
    No Products Registered

    Join Date
    Feb 2008
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Distance Formula

    You are a genius in math(s)

  8. #8
    No Products Registered

    Join Date
    Dec 2006
    Posts
    1,332
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Distance Formula

    Quote Originally Posted by Werbad
    Abs() is not needed, when you multiply something with itself the answer is always positive. -2 * -2 = 4
    What do you mean? I'm not talking about the actual distance formula, of course you don't need to use absolute values for that. I'm talking about earlier on how Dines mentions how to find X and Y distances separately, which does require an absolute value to make it positive in some cases, because after all you can't have a negative distance. I think we are talking about two different things because there is no multiplying in what I was referring to, only simple subtraction.

  9. #9
    No Products Registered

    Join Date
    Feb 2007
    Location
    Sydney, Australia
    Posts
    1,654
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Distance Formula

    We just learnt Pythagoras theorem in year 8 maths

    Great job Dines!

  10. #10
    No Products Registered

    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    1,141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Distance Formula

    Just making sure nobody would add Abs() to the Distance formula thinking there was an error in Dines article...

Page 1 of 2 1 2 LastLast

Similar Threads

  1. INI Formula Help
    By MajorityCA in forum Multimedia Fusion 2 - Technical Support
    Replies: 2
    Last Post: 8th February 2009, 01:28 AM
  2. Node formula
    By nivram in forum Multimedia Fusion 2 - Technical Support
    Replies: 5
    Last Post: 17th December 2008, 06:50 AM
  3. RPG Damage Formula
    By ZayLong in forum Multimedia Fusion 2 - Technical Support
    Replies: 11
    Last Post: 5th September 2008, 08:14 PM
  4. Help with formula
    By LB in forum The Games Factory 2 - Technical Support
    Replies: 6
    Last Post: 13th June 2007, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •