Re: [library] bump.lua v3.1.4 - Collision Detection
Posted: Fri Oct 23, 2015 9:28 pm
Hi, definitively you will have to use filters for that. The way you use them will depend a bit on what you want to do with the collision information (or wether you want to have that information at all) and on how you can tell appart the player and the enemies from the environment (in my code I will assume that your items have a property called isEnvironment, isEnemy or isPlayer.
If you only want your player and enemies to collide with the environment, and "completely ignore" each other, you can simply make the filter function return "slide" for the environment and nil for everyting else in both cases:
Then you can pass that filter function as the last parameter of world:move of the player and enemies:
If you have done everything correctly, the player and the enemies will ignore each other.
Another possibility is that you don't want them to completely ignore each other: you want them to "physically pass" through each other, but you want to know about their collision (for example to decrease the player health gradually, or to play a sound). In that case, the filter should return a "slide" collision for the environment, and a "cross" collision for the player/enemies.
With this code the player and enemies will brehave like before (they will seem to ignore each other), but you will get an extra collision inside cols, to indicate that they are "traversing each other".
If you only want your player and enemies to collide with the environment, and "completely ignore" each other, you can simply make the filter function return "slide" for the environment and nil for everyting else in both cases:
Code: Select all
local filter = function(item, other)
if other.isEnvironment then return 'slide' end -- replace '.isEnvironment' by whatever you use
end
Code: Select all
local cols, len = world:move(player, targetX, targetY, filter)
...
local cols, len = world:move(enemy, targetX, targetY, filter)
Another possibility is that you don't want them to completely ignore each other: you want them to "physically pass" through each other, but you want to know about their collision (for example to decrease the player health gradually, or to play a sound). In that case, the filter should return a "slide" collision for the environment, and a "cross" collision for the player/enemies.
Code: Select all
local playerFilter = function(item, other)
if other.isEnvironment then return 'slide' end
if other.isEnemy then return 'cross' end
end
...
local cols, len = world:move(player, targetX, targetY, playerFilter)
...
local enemyFilter = function(item, other)
if other.isEnvironment then return 'slide' end
if other.isPlayer then return 'cross' end
end
...
local cols, len = world:move(enemy, targetX, targetY, enemyFilter)