Page 1 of 1
bump.lib cannot get filters working
Posted: Mon Aug 22, 2016 6:33 pm
by JohnnyC
I apologize for making so many threads the last few days.
I have actual collision working but if I put a filter it just all reads as nil so nothing collides. Trying to figure out what I'm doing wrong.
Code: Select all
local playerFilter = function(player, other)
if other.enemy then return 'bounce'
elseif other.ispbullet then return 'bounce'
end
-- else return nil
end
function movePlayer(player, dt,dir)
if dir == 'right' then
local goalX, goalY = player.x + player.speed * dt, player.y
local actualX, actualY, cols, len = world:move(player, goalX, goalY, playerFilter)
player.x, player.y = actualX, actualY
-- deal with the collisions
for i=1,len do
if other.ispbullet then
collided = true
else
end
end
end
My bullets are
Code: Select all
table.insert(pbullets, pbullet)
world:add(pbullet, pbullet.x, pbullet.y, pbullet.sx, pbullet.sy)
I'm aware that I only have the code when moving right but I just want to test it first. Thank you in advance for anyone who checked my thread out.
Re: bump.lib cannot get filters working
Posted: Mon Aug 22, 2016 11:19 pm
by veethree
You're creating "playerFilter" in main.lua as a local variable, This means it's in the local scope of main.lua, So player.lua doesn't have access to it. Either make playerFilter a global, Or move it into player.lua.
There's a post on the löve blog about how scope works in lua, You can check it out
here.
Re: bump.lib cannot get filters working
Posted: Mon Aug 22, 2016 11:31 pm
by JohnnyC
I have a player filter in player.lua. I actually didn't know I had it in main as well. It was probably when I was trying to get it to work. However deleting the one in main and even making the one in player.lua global it still doesn't seem to be working.
Thank you for the link though since I've always been kinda bad with scope and I need to comb through my code and fix a lot of things regarding that.
Re: bump.lib cannot get filters working
Posted: Tue Aug 23, 2016 12:32 am
by veethree
"other.enemy" is a reference to the "enemy" object (table) that you add into the bump world (in enemyCreate), The object (table) you add into the bump world doesn't contain the "enemy" you check for in the playerFilter.
Adding the line "enemy.enemy = true" in "enemyCreate" should fix this. And bullets as far as i can tell aren't added to the bump world at all.
Also regarding scope, At the top of player.lua (same for enemy, bullets, pattern etc.) you put "Player = {}", And you return it at the bottom, But you never put anything into the table. So when you do "local Player = require "Player"" in main.lua, The variable "Player" in main.lua is just an empty table. This is pretty pointless.
What you should do is put all the functions and all player related variables into the Player table. So player.lua would look something like this
Code: Select all
local player = {}
function player.information()
player.x = 0
player.y = 0
--etc
end
function player.update(dt)
--update stuff
end
function player.draw()
--draw stuff
end
return player
Re: bump.lib cannot get filters working
Posted: Tue Aug 23, 2016 5:02 am
by JohnnyC
pbullet is added under movement under love.keyboard.isDown('m'). I know it's a bit hidden, I apologize. I tried what you said and it still did not resolve the issue. The filter is still not working, if I remove the reference to the filter it works fine with the default slide. I just can't get the filter to work. Which is what I've been doing the last few hours or so. I've created a new project just to test it out and even in the brand new project I cannot get the filter working.
Re: bump.lib cannot get filters working
Posted: Tue Aug 23, 2016 8:11 pm
by veethree
Here's a simple example of a filter working, The player can move through the orange block, but not the blue one.
Code: Select all
function love.load()
bump = require "bump"
world = bump.newWorld()
player = {
type = "player",
x = love.graphics.getWidth() / 2 - 16,
y = love.graphics.getHeight() / 2 - 16,
size = 32
}
solidWall = {
type = "solidWall",
x = 32,
y = 128,
width = 32,
height = 32
}
notSolidWall = {
type = "notSolidWall",
x = love.graphics.getWidth() - 64,
y = 128,
width = 32,
height = 32
}
world:add(player, player.x, player.y, player.size, player.size)
world:add(solidWall, solidWall.x, solidWall.y, solidWall.height, solidWall.width)
world:add(notSolidWall, notSolidWall.x, notSolidWall.y, notSolidWall.height, notSolidWall.width)
end
function playerFilter(item, other)
if other.type == "solidWall" then
return "slide"
elseif other.type == "notSolidWall" then
return "cross"
end
end
function love.update(dt)
local goalX, goalY = player.x, player.y
if love.keyboard.isDown("d") then
goalX = player.x + 200 * dt
elseif love.keyboard.isDown("a") then
goalX = player.x - 200 * dt
end
if love.keyboard.isDown("s") then
goalY = player.y + 200 * dt
elseif love.keyboard.isDown("w") then
goalY = player.y - 200 * dt
end
local actualX, actualY, cols = world:move(player, goalX, goalY, playerFilter)
player.x = actualX
player.y = actualY
end
function love.draw()
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", player.x, player.y, player.size, player.size)
love.graphics.setColor(0, 125, 255)
love.graphics.rectangle("fill", solidWall.x, solidWall.y, solidWall.width, solidWall.height)
love.graphics.setColor(255, 125, 0)
love.graphics.rectangle("fill", notSolidWall.x, notSolidWall.y, notSolidWall.width, notSolidWall.height)
end
function love.keypressed(key)
if key == "escape" then love.event.push("quit") end
end
I'm pretty sure the problem is your filter is checking for a value that doesn't exist, so it always returns nil. Perhaps you can read over my code and figure things out.
Re: bump.lib cannot get filters working
Posted: Wed Aug 24, 2016 12:45 am
by JohnnyC
Thank you, the example code you made was very useful. I got it working, you actually explained it properly earlier but I just didn't understand it properly.
I had other.isenemy because that is how it is in the example on github. I was preoccupied with the 'is' thinking it was important to have, like the code is supposed to be other.is<type>. So even though I put enemy.enemy like you said I should. It didn't work because it was looking for isenemy not enemy.