The Daily Click ::. Forums ::. Klik Coding Help ::. Testing the distance between 2 objects
 

Post Reply  Post Oekaki 
 

Posted By Message

Gregorian Chan



Registered
  12/05/2006
Points
  7
1st July, 2007 at 07:16:03 -

Hey I am trying to work out how to retrieve the distance of pixels between 2 objects and have the result shown on a counter. Is there any way to do this? Thanks

 
n/a

Joe.H

Evil Faker

Registered
  19/08/2002
Points
  3305
1st July, 2007 at 07:41:03 -

http://www.create-games.com/article.asp?id=1790

read.

 
My signature is never too big!!!

DeadmanDines

Best Article Writer

Registered
  27/04/2006
Points
  4758
1st July, 2007 at 08:22:48 -

If you're using MMF2, just write this:

sqr( ( X("Object 1") - X("Object 2")) POW 2 + ( Y("Object 1") - Y("Object 2")) POW 2)


A quick jiggly about how it works:

You find the distance on the X axis (point 1 - point 2), and you square it (times it by itself). In MMF2 you can just use 'POW 2' to do the squaring quickly.

Then you do the same with the Y axis and add these two values together.

Then finally, use Square Root (sqr()) to convert the big number you get into the distance.


The groovy thing is that it works for multiple objects. For example, I have a pathfinding system where you have lots of objects and you have to find out how far each of them is from a certain point on the map. You can just do this:

ALWAYS
- set Alterable Value A to (the distance formula we used above)

and that'll do it for all of them! In just one event

 
191 / 9999 * 7 + 191 * 7

LIJI

Flava's Smarter Twin

Registered
  29/08/2006
Points
  2032

I like penguins
1st July, 2007 at 09:05:18 -

If you want the exact distance in pixels, use what DeadmanDines said.
If you want to compare distances between points or check the closest/farest point, use this:
Abs( X("Object 1") - X("Object 2"))+Abs( Y("Object 1") - Y("Object 2"))
This will make the application faster.

 
Service Unavailable

Joe.H

Evil Faker

Registered
  19/08/2002
Points
  3305
1st July, 2007 at 09:05:29 -

Hey Dines, you should start making that game again.

You know?

That game where you controlled a mushroom?

 
My signature is never too big!!!

DeadmanDines

Best Article Writer

Registered
  27/04/2006
Points
  4758
1st July, 2007 at 09:38:44 -

Oh yeah, I remember that!

Maybe I will, thanks for reminding me.

I found a few references to it on the WayBack Machine, but unfortunately all my files and pics and stuff were linked by CSS, so it didn't crawl thru em

 
191 / 9999 * 7 + 191 * 7

Gregorian Chan



Registered
  12/05/2006
Points
  7
1st July, 2007 at 17:53:07 -

Thank you all very muchly. My brain feels better now

 
n/a

Gregorian Chan



Registered
  12/05/2006
Points
  7
1st July, 2007 at 17:53:09 -

Thank you all very muchly. My brain feels better now

 
n/a
   

Post Reply



 



Advertisement

Worth A Click