Skip to content

Instantly share code, notes, and snippets.

@HoraceBury
Last active October 30, 2016 08:23
Show Gist options
  • Save HoraceBury/8538519 to your computer and use it in GitHub Desktop.
Save HoraceBury/8538519 to your computer and use it in GitHub Desktop.
Collisions library modified version of the Corona SDK CollisionFilter sample.
-- collision library
local collisionslib = {}
local categoryNames = {}
local categoryCounter = 1
local function newCategory( name )
categoryNames[ name ] = categoryCounter
categoryCounter = categoryCounter * 2
return categoryNames[ name ]
end
local function getCategory( name )
local category = categoryNames[ name ]
if (category == nil) then
category = newCategory( name )
end
return category
end
collisionslib.getCategory = getCategory
local function getMask( ... )
local mask = 0
for i=1, #arg do
local name = arg[i]
mask = mask + getCategory( name )
end
return mask
end
collisionslib.getMask = getMask
local function getFilter( categoryName, maskNames )
return { categoryBits=getCategory( categoryName ), maskBits=getMask( unpack( maskNames ) ) }
end
collisionslib.getFilter = getFilter
return collisionslib
--
-- Abstract: CollisionFilter sample project
-- Demonstrates "categoryBits" and "maskBits" for collision filtering
--
-- Version: 1.1
--
-- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license
-- Copyright (C) 2010 Corona Labs Inc. All Rights Reserved.
--
-- Supports Graphics 2.0
---------------------------------------------------------------------------------------
local collisionslib = require("collisionslib")
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight
local physics = require("physics")
physics.start()
physics.setScale( 60 )
display.setStatusBar( display.HiddenStatusBar )
local bkg = display.newImage( "stripes.png", centerX, centerY )
display.setDefault( "anchorX", 0.0 ) -- default to TopLeft anchor point for new objects
display.setDefault( "anchorY", 0.0 )
--borderCollisionFilter = { categoryBits = 1, maskBits = 6 } -- collides with (4 & 2) only
--borderCollisionFilter = { categoryBits = collisionslib.getCategory("border"), maskBits = collisionslib.getMask("border","red","blue") } -- collides with (4 & 2) only
borderCollisionFilter = collisionslib.getFilter( "border", { "border","red","blue" } )
borderBodyElement = { friction=0.4, bounce=0.8, filter=borderCollisionFilter }
local borderTop = display.newRect( 0, 0, 320, 1 )
borderTop:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderTop, "static", borderBodyElement )
local borderBottom = display.newRect( 0, 479, 320, 1 )
borderBottom:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderBottom, "static", borderBodyElement )
local borderLeft = display.newRect( 0, 1, 1, 480 )
borderLeft:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderLeft, "static", borderBodyElement )
local borderRight = display.newRect( 319, 1, 1, 480 )
borderRight:setFillColor( 0, 0, 0, 0) -- make invisible
physics.addBody( borderRight, "static", borderBodyElement )
local red = {}
local blue = {}
--local redCollisionFilter = { categoryBits = 2, maskBits = 3 } -- collides with (2 & 1) only
--local blueCollisionFilter = { categoryBits = 4, maskBits = 5 } -- collides with (4 & 1) only
--local redCollisionFilter = { categoryBits = collisionslib.getCategory("red"), maskBits = collisionslib.getMask("border","red") } -- collides with (2 & 1) only
--local blueCollisionFilter = { categoryBits = collisionslib.getCategory("blue"), maskBits = collisionslib.getMask("border","blue") } -- collides with (4 & 1) only
local redCollisionFilter = collisionslib.getFilter( "red", { "border","red" } ) -- collides with (2 & 1) only
local blueCollisionFilter = collisionslib.getFilter( "blue", { "border","blue" } -- collides with (4 & 1) only
local redBody = { density=0.2, friction=0, bounce=0.95, radius=43.0, filter=redCollisionFilter }
local blueBody = { density=0.2, friction=0, bounce=0.95, radius=43.0, filter=blueCollisionFilter }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment