Re: Code for collisions
Posted: Sat Feb 07, 2009 5:55 pm
if your using simple shapes physics are the way to go, but in the case of mario up there it becomes much easier to mask it rather than plotting out all those points to a body
Code: Select all
-- simple binary pbm reader
-- 2007 - 2009 osgeld
-- ( osgeld@cheesefactory.us )
-- CC share alike
-- use, distrubute, remix ect
-- give credit, share updates
data = love.filesystem.read("mario.pbm")
image_width = 439
mask = {}
-- strip the pbm header
for i = 1, 3 do
found = string.find(data, "\n")
data = string.sub(data, found + 1, -1)
end
-- convert from binary to table
local x = 0
local y = 0
for counter = 1, #data do
byte = string.byte(string.sub(data, counter, counter))
byte_value = 128
repeat
if (byte / byte_value) >= 1 then
byte = byte - byte_value
else
table.insert(mask, {x, y})
end
if x < image_width then
x = x + 1
else
x = 0
y = y + 1
end
byte_value = byte_value / 2
until(byte_value < 1)
end
For this solution to work reall well, I would preferably like to see that it is capable of handling scaled sprites as well as computing the normals of the collided surfaces (e.g. intrapolating from the nearby pixels).osgeld wrote:once the lib is done is a matter of 3 mouse clicks in the gimp to make a collision mask
vs even the asteroids ship is 6 cords to map
also to hammer this point home, you will not be checking pixel perfect every loop
Code: Select all
booleanAndResult = {}
for i=0,255 do
booleanAndResult[i] = {}
for j=0,255 do
booleanAndResult[i][j] = 0
for k=0,7 do
if (i % (2 * 2^k)) >= 2^k and (j % (2 * 2^k)) >= 2^k then
booleanAndResult[i][j] = booleanAndResult[i][j] + 2^k
end
end
end
end
function bool32And(x1, x2)
return booleanAndResult[x1 % 256][x2 % 256] +
256 * booleanAndResult[math.floor(x1 / 256) % 256][math.floor(x2 / 256) % 256] +
65536 * booleanAndResult[math.floor(x1 / 65536) % 256][math.floor(x2 / 65536) % 256] +
16777216 * booleanAndResult[math.floor(x1 / 16777216) % 256][math.floor(x2 / 16777216) % 256]
end
Taking modulus to ensure that the quotient remains an integer makes things considerably faster. And so, the following version is better. Still far from the machine instructions though.osgeld wrote:ill see what i can do, and thanks for the bitwise AND, i have one but its not as smooth as yours
Code: Select all
booleanAndResult = {}
for i=0,255 do
booleanAndResult[i] = {}
for j=0,255 do
booleanAndResult[i][j] = 0
for k=0,7 do
if (i % (2 * 2^k)) >= 2^k and (j % (2 * 2^k)) >= 2^k then
booleanAndResult[i][j] = booleanAndResult[i][j] + 2^k
end
end
end
end
booleanXorResult = {}
for i=0,255 do
booleanXorResult[i] = {}
for j=0,255 do
booleanXorResult[i][j] = 0
for k=0,7 do
if ((i % (2 * 2^k)) >= 2^k and (j % (2 * 2^k)) < 2^k) or
((i % (2 * 2^k)) < 2^k and (j % (2 * 2^k)) >= 2^k)
then
booleanXorResult[i][j] = booleanXorResult[i][j] + 2^k
end
end
end
end
function bnot32(x)
return 4294967295 - x
end
function bnot64(x)
return 18446744073709551615 - x
end
function band32(x1, x2)
local sum = 0
local a1, a2
a1 = x1 % 16777216
a2 = x2 % 16777216
sum = sum + booleanAndResult[(x1 - a1) / 16777216][(x2 - a2) / 16777216]
x1 = a1 % 65536
x2 = a2 % 65536
sum = sum + booleanAndResult[(a1 - x1) / 65536][(a2 - x2) / 65536]
a1 = x1 % 256
a2 = x2 % 256
return sum + booleanAndResult[(x1 - a1) / 256][(x2 - a2) / 256] +
booleanAndResult[a1][a2]
end
function bxor32(x1, x2)
local sum = 0
local a1, a2
a1 = x1 % 16777216
a2 = x2 % 16777216
sum = sum + booleanXorResult[(x1 - a1) / 16777216][(x2 - a2) / 16777216]
x1 = a1 % 65536
x2 = a2 % 65536
sum = sum + booleanXorResult[(a1 - x1) / 65536][(a2 - x2) / 65536]
a1 = x1 % 256
a2 = x2 % 256
return sum + booleanXorResult[(x1 - a1) / 256][(x2 - a2) / 256] +
booleanXorResult[a1][a2]
end