3

When I searching about Snapping grid function. Actually it is Tile Collide.(You maybe know I learning GMS2 because of tags..)

It is not important for my question.

Cut to the chase, I learned about this formula.

pos.x - (pos.x % gridwidth)  <- this is number calculation.

It worked well what I want to. And I found different formula.

This formula is working for collision of obj and tiles.

(pos.x)&~(gridwidth-1)    <- this is binary calculation.

And It has same work. What happen?? I can't understand how these formula are transformed..

Actually I understand '&' has same work with subtract. But I don't understand others.

for example

var f1,f2;
var pos_x = 102;
var gridwidth = 64; // It must be even power of 2.
f1 = pos_x - (pos_x % gridwidth);
f2 = (pos_x)&(~(gridwidth-1));

Rob
  • 4,927
  • 4
  • 26
  • 41
JINU_K
  • 133
  • 7
  • why python and c# tag at the same time? What language is this? – Paritosh Singh Apr 07 '19 at 17:06
  • GML is a script language for GameMaker Studio2 I thought GML base on Python.. just my guess. I'm sorry if you confused about that. And I delete C# tag because of it's confusion. – JINU_K Apr 07 '19 at 17:12
  • Does grid width have to be an even power of 2? That's the only way I see the second formula making any sense. - if so, then both of these are "snapping" to a multiple of an even power of two, that even power of two being `gridwidth`. – CryptoFool Apr 07 '19 at 17:20
  • provide potential values for pos.x and gridwidth? because these two formulas are not equivalent at all. for example, with 10 and 3 `x - (x % gridwidth) #9` and `(x)&~(gridwidth-1) #8` – Paritosh Singh Apr 07 '19 at 17:22
  • Oh I see.. I added an example what I did for it. – JINU_K Apr 07 '19 at 17:32
  • Cool. So does my answer explain what's going on well enough for you to understand it? – CryptoFool Apr 07 '19 at 17:37
  • Yes!! My trouble is solved, thanks for you. – JINU_K Apr 07 '19 at 17:47

1 Answers1

2

These two formulas are only equivalent if gridwidth is an even power of two, like 128, 256, 2048, etc.

In either case, pos.x - (pos.x % gridwidth) is doing a floor back to an even multiple of gridwidth, "snapping" to the start of that grid cell, where the grid consists of cells of width gridwidth.

If you can assume gridwidth is an even power of 2, then (pos.x)&~(gridwidth-1) is doing the same thing. Basically, given this assumption, every value in one grid cell will only differ in its lower order bits...the bits below the gridwidth bit. This code is just clearing those bits, thereby calculating the result to a be an even multiple of gridwidth, which will be the first position in that cell of the grid.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Thanks for your Answer. As you said, second formula has same work with first formula when `gridwidth` has even number. – JINU_K Apr 07 '19 at 17:45
  • 1
    Not just any even number though...an even power of 2 only. This won't work for a grid width of 10, or 1000. - I figure that's what you meant. Just making sure. – CryptoFool Apr 07 '19 at 17:57