I made a basic, filler collision function to use with all entities and items, and it works alright, but it could definitely be better.
Here is the function
Code: Select all
function collide_withTile(ia,va)
for i,v in ipairs(solid) do
--Limits the function to tiles closer to the entity.
if va.x + va.width / 2 > v.x + v.width / 2 - renderDistance * 16 and va.x + va.width / 2 < v.x + v.width / 2 + update_Distance * 16 and
va.y + va.height / 2 > v.y + v.height / 2 - renderDistance * 16 and va.y + va.height / 2 < v.y + v.height / 2 + update_Distance * 16 then
--Collision detection + resolution.
if va.x + va.width > v.x and
va.x + (va.width / 2) < v.x + (v.width / 2) and
va.y + va.height > v.y + extra and
va.y + extra < v.y + v.height then
va.x = v.x - va.width
va.xvel = 0
end
if va.y + va.height > v.y and
va.y + (va.height / 2) < v.y + (v.height / 2) and
va.x + va.width > v.x + extra and
va.x + extra < v.x + v.width then
va.y = v.y - va.height
va.yvel = 0
va.grounded = true
end
if va.x < v.x + v.width and
va.x + (va.width / 2) > v.x + (v.width / 2) and
va.y + va.height > v.y + extra and
va.y + extra < v.y + v.height then
va.x = v.x + v.width
va.xvel = 0
end
if va.y < v.y + v.height and
va.y + (va.height / 2) > v.y + (v.height / 2) and
va.x + va.width > v.x + extra and
va.x + extra < v.x + v.width then
va.yvel = 0
va.y = v.y + v.height
end
end
end
end
The problem may be the variable 'extra', i'm not sure, but before I put that in there the player would slide off or away from the tile on contact, it is set at 2 right now, and most of the sprites colliding are 16x16:P
I am very sleep deprived, and could really use some help. I'd appreciate it a ton if someone could help me touch up this function.