When I started with Corona I wasn’t immediately able to visualize how collision bit filters worked, so I created the following helper function. It works but I don’t know if it makes any sense, it just helps me visualize what happens, maybe this is of use to someone. The idea is that when the X shown can fall through the wall of XXX’s below it, then it won’t collide… and reversely if it would collide with the wall, well then it’s collidable towards it. In this case, it’s for the game Free the Fobbles, where a sprite has a .type and more assigned (like sprite.type = ‘fobble’). I needed bright Fobbles to not collide with bright doors, and dark Fobbles to not collide with darker doors:
[code]function appGetCollisionFilter(self)
local filter = {}
local categoryDefault = ’ X’
local categoryFobble1 = ’ X ’
local categoryFobble2 = ’ X ’
local categoryStone1 = ’ X ’
local categoryStone2 = ’ X ’
local maskDefault = ’ XXXXXX’ – collides with everything
local maskFobble1 = ’ XX XXX’ – collides with everything but categoryStone1
local maskFobble2 = ’ X XXXX’ – collides with everything but categoryStone2
filter.categoryBits = categoryDefault
filter.maskBits = maskDefault
if self.type == ‘fobble’ and self.parentPlayer == 1 then
filter.categoryBits = categoryFobble1
filter.maskBits = maskFobble1
elseif self.type == ‘fobble’ and self.parentPlayer == 2 then
filter.categoryBits = categoryFobble2
filter.maskBits = maskFobble2
elseif self.type == ‘stone’ and self.parentPlayer == 1 then
filter.categoryBits = categoryStone1
elseif self.type == ‘stone’ and self.parentPlayer == 2 then
filter.categoryBits = categoryStone2
end
filter.categoryBits = misc.binaryToDecimal(filter.categoryBits, ‘X’)
filter.maskBits = misc.binaryToDecimal(filter.maskBits, ‘X’)
return filter
end
function binaryToDecimal(binaryString, optionalBitChar)
– (based on a function I found online)
if optionalBitChar == nil then optionalBitChar = ‘1’ end
local num = 0
local ex = string.len(binaryString) - 1
local l = ex + 1
for i = 1, l do
b = string.sub(binaryString, i, i)
if b == optionalBitChar then num = num + 2 ^ ex end
ex = ex - 1
end
return tonumber( string.format(’%u’, num) )
end[/code] [import]uid: 10284 topic_id: 7622 reply_id: 307622[/import]