Click to See Complete Forum and Search --> : Pythagoras Theorem

RazoRmedia

03-12-2002, 07:01 PM

Danny, normally to work out distances between 2 objects I would use pythagoras theroem ie:

distance = Math.sqrt((mc2._x - mc1._x)*2) + (mc2._y - mc1._y)*2));

is this the best way to do this? Ie is it the fastest if I were to use this inside a function and use it throughout my movie?

Ian424

03-12-2002, 07:08 PM

im not the mod or a math expert, but i think thats the quickest way.

distance = square root ((x1 - x2)^2 + (y1 - y2)^2)) ?

(though maybe the * in yours was meant to be ^ instead, or im forgetting math)

..in fact, you could probably just make a function w/ that inside and plug it in, as far as i know its the closest thing to the actual equation/theorem/whatever as you can get.

RazoRmedia

03-12-2002, 07:16 PM

Originally posted by Ian424

(though maybe the * in yours was meant to be ^ instead, or im forgetting math)

yeah, you're dead right. I was meant to write the long version:

distance = Math.sqrt((mc2._x - mc1._x)*(mc2._x - mc1._x)) + ((mc2._y - mc1._y)*(mc2._y - mc1._y));

but I was being lazy, I meant squared, cheers Ian424

DonDanny

03-12-2002, 07:27 PM

Just an addition, hittesting circles that way works pretty good. You know, like radius of the two circles is half the distance, if distance is less then hit... better then the normal sqaure hittesting ;)

Ian424

03-12-2002, 07:36 PM

Originally posted by DonDanny

Just an addition, hittesting circles that way works pretty good. You know, like radius of the two circles is half the distance, if distance is less then hit... better then the normal sqaure hittesting ;)

any idea if this takes up more processor power than doing hitTest w/ 2 squares ? if it doesn't, i have some new footers to make :)

RazoRmedia

03-12-2002, 07:55 PM

as a special request, I am going to explain pythagoras theorem. Please correct me if I am wrong.

Basically Pythagoras Theorem is to work out the hypotenuse (longest side opposite the right angle) of a right angled triangle.

http://www.razormedia.co.uk/stuff/pyth.gif

So for example we know that the angle between a and b is a right angle and so therefore 90 degrees. We also know the length of the sides a and b. What we need to work out is the length of c.

Lets say for example a is 5cm and b is 5.5cm

pythagoras theorem says that a squared (written as a^2) plus b squared equals c squared.

so:

a^2 = 5^2 = 5*5 = 25

b^2 = 5.5^2 = 5.585.5 = 30.25

and because we know that c = b^2 + a^2

we can equate

c = sqrt(25+30.25) = sqrt(55.25) = 7.43303437365925

so the distance of c is 7.43 (rounded).

What use is this to us though, why would we need this in flash. well, imagine on the image above that the two red dots represent the position of movieclips. you can work out their position on the stage so with some simple mathematics you can work out the exact distance between them.

Hope this is helpful to some.

Bruce

DonDanny

03-12-2002, 08:50 PM

Hope my job as Math and Physics mod will always be that easy. I coundn't explain pythagoras theorem better, so I'll just say jup exactly ...

DonDanny

03-12-2002, 09:00 PM

It'll take longer Ian, but if you just do it for some objects it shouldn't be that processor intensive.

lesli_felix

03-19-2002, 07:22 PM

Nice explanation.

Though you might find it a little neater if a=3 and b=4

or a=5 and b=12

or even a=99 and b=4900

..all resulting in c being a whole number. :-)

They're what's known as pythagorean triples, where a^2 + b^2 = c^2 and a, b & c are all whole numbers.