how to set multiple collision masks in script
currently i can only set 1 mask in my code
phys.collision_mask = 1
how to set multiple collision masks in script
currently i can only set 1 mask in my code
phys.collision_mask = 1
i dont remember exactly, but i think that collision 1, is the mask 0.
if tipo == "turret":
set_collision_mask_bit(13,true) # <- mask 12
i want to do something like>
phys.collision_mask = 1 and 4
but that obviously doesnt work
Are you trying to set bits 1 and 4 in the collision mask?
DaveTheCoder yes
There's some inconsistency in how the bits are numbered.
In the Inspector, the left-most bit is numbered 1. In the method call set_collision_mask_bit(), that same bit is 0. In either case, that's the LSB (least significant bit) of the integer mask.
It doesn't matter which numbering scheme you use, so long as you're consistent about it.
If you set the bits numbered 1 and 4 in the Inspector, that's equivalent to doing it in code by:
# Set bits 0 and 3. Leave other bits unchanged.
set_collision_mask_bit(0, true)
set_collision_mask_bit(3, true)
or:
# Set whole mask to specified value.
collision_mask = 1 + 8
or:
collision_mask = 9
or:
collision_mask = 0b1001 # binary
It's a bit mask, so it's binary. You can save the bit values as binary numbers and then use the bitwise or |
to combine them.
var gun_mask = 0b0001
var knife_mask = 0b1000
collision_mask = gun_mask | knife_mask
DaveTheCoder that doesnt work on my end
cybereality
how do i get the bit binary for 1 and 4?
im trying to use it in your raycast script u helped me with.
var from = camera.project_ray_origin(wantedpos)
var to = from + camera.project_ray_normal(wantedpos) * ray_length
var space = get_world_3d().direct_space_state
var phys = PhysicsRayQueryParameters3D.new()
phys.from = from
phys.to = to
phys.collision_mask = 1
If you can't do it in your head, then you can use an online converter.
https://www.rapidtables.com/convert/number/decimal-to-binary.html
cybereality ok got it working, tnx!
but the binary for mask 4 says 100 on that website, while var mask4 = 0b1000
was the correct number
100 is the binary for 4, but in Godot the bit mask layer is not the same as the value (bit mask layer 4 is the bit 3, or the 4th one from the right) . If you roll over the bit mask grid in the inspector, it will tell you the "value" which is the binary number (in decimal). For the layer 4 it is value 8, which is 0b1000
. But if you are not combining them, it is easy to figure out without conversion as the mask layer is just the digit in the binary number that is set to one.
Layer 1 = 0b0001
Layer 3 = 0b0100
etc.