Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Box2D - filterData helper — Gideros Forum

Box2D - filterData helper

eezingeezing Member
edited July 2014 in Code snippets

Here's a simple little function to manage filterData for Box2d.
 
local categories = { __categoryIndex = 1 }
 
local function getCategory(...)
 
	local value = 0
 
	for i, name in ipairs(arg) do
 
		if not categories[name] then
 
			local v = categories.__categoryIndex * 2
			categories.__categoryIndex = v
			categories[name] = v
			value = value + v
 
		else
 
			value = value + categories[name]
 
		end
	end
 
	return value
end
 
 
-- Use --
 
local categoryBits = getCategory("car")  -- New category
local maskBits = getCategory("car")  -- Will only collide with cars
print(categoryBits, maskBits) --> 2, 2
 
local categoryBits = getCategory("plane")  -- New category
local maskBits = getCategory("plane")  -- Will only collide with planes
print(categoryBits, maskBits) --> 4, 4
 
local categoryBits = getCategory("plane")  -- Use existing category
local maskBits = getCategory("plane", "car")  -- Collide with both cars and planes
print(categoryBits, maskBits) --> 4, 6
 
local categoryBits = getCategory("car")  -- Use existing category
local maskBits = getCategory("plane", "car", "boat") -- Collide with cars, planes, and boats (will create new 'boat' category)
print(categoryBits, maskBits) --> 2, 14
+1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.