Godot Version
4.5
Question
I found this thread and I wonder how they came up with the answer.
Just set the
collision_maskproperty to an int that packs the right binary maski.e.
collision_mask = 233
binary masks compare each binary position of a number, 233 could be represented in binary as 0b11101001, which isn’t actually accurate for the question which uses bits 1, 8, 9, 10, and 11
collision_mask = 0b11110000001
Every 1 is essentially a true and for every 0 a false
collision_mask = 0b11110000001
If I understand correctly that reads the far right 1 is layer 1,(..8,9,10,11) = true ..etc. and that 0b means the rest after layer 11 are false?
Also for the layer value, I read that each layer is up by the power of 2. If I want to turn on layer 1 and 24 I’d need to add both value up as:
collision_mask = 8388609 (1+8388608) ?
0b just means it is base 2, binary, the bits left to it are 0 by default
More specifically you can only get to the number 233 with that combinations of 1s and 0s, thats how you know the 1s are true.
A visual / physical representation is dip switches, although obviously you can do a lot more than that in the computing version.
by default means not reset to 0?
like if I had set layer 3 as true , then I set collision_mask = 0b01, that means 1 and 3 are true, correct?
No it means all the rest are 0, if you set it to 0b01 it means you set it to 0b01 and layer 1 is set only
What about this question I had before.
Also for the layer value, I read that each layer is up by the power of 2. If I want to turn on layer 1 and 24 I’d need to add both value up as:
collision_mask = 8388609 (1+8388608)
Is my understanding correct? and If so, does this reset other layers to false as well?
You would use | to perform bitwise operations, and it’d be best to use binary format with 0b to make it readable
But is my understanding correct though? I just wanna understand how it works , not necessary gonna use it.
Yes it resets the other layers, it sets the mask to the value you assign, so the layers you assign are the only ones that are set