The Daily Click ::. Forums ::. Non-Klik Coding Help ::. Nearest tile
 

Post Reply  Post Oekaki 
 

Posted By Message

alessandroLino

I create vaporware

Registered
  11/03/2009
Points
  172
23rd June, 2010 at 17:25:58 -

Anyone know how to find how far im to nearest tile? Ok, that was a bit confusing

I need to snap a object to a grid, but only if its close to the position its going to be snapped (with the px/tileH*tileW formula)

Image

So basically i need to find the red (or blue) line on the pic, any ideas?

 
n/a

Pixelthief

Dedicated klik scientist

Registered
  02/01/2002
Points
  3419

Game of the Week WinnerWeekly Picture Me This Winner!You've Been Circy'd!VIP MemberI like Aliens!Evil klikerThe SpinsterI donated an open source project
23rd June, 2010 at 18:27:16 -

to find which tile you are in, you can simply do:


xpos("object") = x("object") / width("tile")


now if you want distance, you might want to offset it and set a distinct width. So for example, for 32 size tiles:


xpos("object") = x("object") / 32
ypos("object") = y("object") / 32



now if you want the distance, theres a simple way to do this. You want to look at how far you are from the center of each tile. The division is giving us each tile at its upper left hand corner. You want the distance from the center, which is offset from the corner by 16 in each direction. Taking the modulo of the position gives us the distance from the center:


ist("object") = x("object") mod 32 - 16
ydist("object") = y("object") mod 32 - 16




This gives us positive and negative distances from the center of each tile.
Now when you want to snap objects to that tile, it becomes a question of what kind of distance you want to check.


If you want to check the absolute distance, that is, along the 2d plane, you'd need to use a slow square root:

ist("object") = x("object") mod 32 - 16
ydist("object") = y("object") mod 32 - 16
distance("object") = sqrt(pow(ist("object"),2) + pow(ydist("object"),2))

alternatively, you can use the approximation formula which is much, much faster:

ist("object") = x("object") mod 32 - 16
ydist("object") = y("object") mod 32 - 16
distance("object") = ((Max(Abs((ist("object"))), Abs((ydist("object"))))) * 0.941246) + ((Min(Abs((ist("object")) ), Abs((ydist("object"))))) * 0.41)



then you can compare like this:


+If Distance("object") < 10
=Set X("object") to X("object") / 32 + 16
=Set Y("object") to Y("object") / 32 + 16




Again alternatively, if you just wanted the distance to any side of the tile, you can compare either:


+If abs(ist("object")) < 8
LOGICAL OR
+If abs(ydist("object")) < 8
=Set X("object") to (X("object") / 32) * 32 + 16
=Set Y("object") to (Y("object") / 32) * 32 + 16




I've made a rough example using that code:
http://sites.google.com/site/claniraq/stuff/tileexample.mfa?attredirects=0&d=1

 
Gridquest V2.00 is out!!
http://www.create-games.com/download.asp?id=7456

alessandroLino

I create vaporware

Registered
  11/03/2009
Points
  172
23rd June, 2010 at 19:15:32 -

Oh thanks a lot!

Wasnt really going to use on mmf, but seeing the example actually helps a ton

 
n/a

Pixelthief

Dedicated klik scientist

Registered
  02/01/2002
Points
  3419

Game of the Week WinnerWeekly Picture Me This Winner!You've Been Circy'd!VIP MemberI like Aliens!Evil klikerThe SpinsterI donated an open source project
23rd June, 2010 at 19:41:27 -

oh, yeah.
the idea is the same for any language- just find the modulo of the position

 
Gridquest V2.00 is out!!
http://www.create-games.com/download.asp?id=7456
   

Post Reply



 



Advertisement

Worth A Click