Need help understanding bump.lua and also why my bullets act this way

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
_Joply
Prole
Posts: 3
Joined: Mon Sep 18, 2023 8:28 am

Need help understanding bump.lua and also why my bullets act this way

Post by _Joply »

I'm new to Love2D and I am trying to figure out using bump.lua for collisions. Right now, the bullets all disappear whenever you hit an enemy. I'm guessing for some reason either the bullet objects are not seperate, or it has to do something with how I am detecting collision/looping. It is very obvious what the issue is if u run it and shoot at the test enemy. The 2nd enemy is not setup at all at the moment. I attached my love file and the code is within the main.lua love.update function.
Attachments
BWDG.love
(12.34 KiB) Downloaded 55 times
MrFariator
Party member
Posts: 559
Joined: Wed Oct 05, 2016 11:53 am

Re: Need help understanding bump.lua and also why my bullets act this way

Post by MrFariator »

I believe the issue stems from the following:

Code: Select all

for i, bulletToRemove in ipairs(bulletsToRemove) do
	world:remove(bulletToRemove)
	table.remove(activeBullets, bulletToRemove[i]) -- this line here
end
I'm reasonably sure bulletToRemove[ i ] will just evaluate to nil in this context, which means that table.remove will just remove the last entry in the table, and this is not necessarily the bullet that actually collided with the enemy. As such, the bullet will continue to collide with the enemy each frame, removing more and more bullets until the bullet passes through the enemy, or it happens to finally remove itself from the active bullets list.

Perhaps the following would be a better and simpler approach (untested, so beware of typos):

Code: Select all

-- reduce the amount of times you call getWidth/Height. You could make these properties in the bullets themselves, like you have with x and y?
local bulletW, bulletH = bullet.sprite:getWidth(), bullet.sprite:getHeight() 
-- it'd be better if you moved this code so that it's only defined once; 
-- in your old code, this filter function would get defined every iteration of the for loop
local filterFunc = function(item) 
	-- Check if the bullet collides with an enemy
	return item == enemy -- in future, I might recommend just changing this to "return item.isEnemy" or similar
end

-- iterate the table in reverse, which allows us to use table.remove while we're going through it
for i = #activeBullets, 1, -1 do
	if(world:hasItem(bullet) == false) then
		world:add(bullet, bullet.x, bullet.y, bulletW, bulletH)
	end
	world:update(bullet, bullet.x, bullet.y, bulletW, bulletH)
	bullet.x = bullet.x + (bullet.speed*dt)*(math.cos(bullet.angle))
	bullet.y = bullet.y + (bullet.speed*dt)*(math.sin(bullet.angle))
	local cols, len = world:queryRect(bullet.x, bullet.y, bulletW, bulletH, filterFunc)
	if len > 0 then
		-- Bullet has hit an enemy, handle the collision here
		table.remove (activeBullets, i)
		enemy.health = enemy.health - 5
	end
end 
_Joply
Prole
Posts: 3
Joined: Mon Sep 18, 2023 8:28 am

Re: Need help understanding bump.lua and also why my bullets act this way

Post by _Joply »

Thanks, this worked, I also did your suggestion of adding fields for the width and height within the bullet object. I did have to modify it slightly to reference activeBullets because bullet itself would be nil

Code: Select all

    local filterFunc = function(item)
        return item == enemy
    end
    for i = #activeBullets, 1, -1 do
        if(world:hasItem(activeBullets[i]) == false) then
            world:add(activeBullets[i], activeBullets[i].x, activeBullets[i].y, activeBullets[i].width, activeBullets[i].height)
        end
        world:update(activeBullets[i], activeBullets[i].x, activeBullets[i].y, activeBullets[i].width, activeBullets[i].height)
        activeBullets[i].x = activeBullets[i].x + (activeBullets[i].speed*dt)*(math.cos(activeBullets[i].angle))
        activeBullets[i].y = activeBullets[i].y + (activeBullets[i].speed*dt)*(math.sin(activeBullets[i].angle))
        local cols, len = world:queryRect(activeBullets[i].x, activeBullets[i].y, activeBullets[i].width, activeBullets[i].height, filterFunc)
        if len > 0 then
            -- Bullet has hit an enemy, handle the collision here
            table.remove (activeBullets, i)
            enemy.health = enemy.health - 5
        end
    end
MrFariator
Party member
Posts: 559
Joined: Wed Oct 05, 2016 11:53 am

Re: Need help understanding bump.lua and also why my bullets act this way

Post by MrFariator »

Right, I think I intended to do that modification as well (either doing what you did, or assigning to a local variable), but must've slipped my mind.
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests