Bullet collision with enemies using bump

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
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Bullet collision with enemies using bump

Post by fridays18 »

After a lot of forum help with bump ive become relatively proficient with it. Or so I thought before trying to incorporate basic enemy collision with bullets. The goal is that when a bullet hits a enemy the bullet and enemy get cleared from the bump world and list holding them. Currently a bullet breaks if it hits an enemy or wall(good) but the enemy has no change(not good). Heres the code any help is appreciated!


This is the entirety of bullet.lua

Code: Select all

Bullet = Object:extend()

function Bullet:new(x, y)
    self.r = 1
    self.x = x
    self.y = y
    self.speed = 700
		self.w = 10
		self.h = 10
		self.path = 0
	self.vx = 0
	self.vy = 0
	self.dead = false


		if player.d == 4 then
    self.path = 4
		self.y = self.y + 10
	elseif player.d == 1 then
		self.path = 1
		self.x = self.x - 10
	elseif player.d == 2 then
		self.path = 2
		self.x = self.x + 10
	elseif player.d == 3 then
    self.path = 3
		self.y = self.y - 10
		end

		world:add(self, self.x, self.y, self.w, self.h)
end

function Bullet:update(dt)
	
		self.vx = 0
	self.vy = 0
	
	if self.x > screenx or self.x < 0 or self.y > screeny or self.y < 0 then
	self.dead = true
	end
	
	if self.path == 4 then
    self.vy = self.vy + self.speed * dt
	elseif self.path == 1 then
		self.vx = self.vx - self.speed * dt
	elseif self.path == 2 then
		self.vx = self.vx + self.speed * dt
	elseif self.path == 3 then
    self.vy = self.vy - self.speed * dt
		end

	if self.dead == false then
			self.x, self.y = world:move(self, self.x+self.vx, self.y+self.vy)

	local targetX, targetY = self.x+self.vx*dt, self.y+self.vy*dt
	local newX, newY = world:move(self, targetX, targetY)
	if newX == targetX and newY == targetY then -- no collision
		self.x, self.y = newX, newY
	else -- collision
		self.dead = true
		world:remove(self)
			--enemy update
	for i,v in ipairs(listOfEnemies) do
				enHIT = CheckCollision(self.x ,self.y,self.w,self.h,v.x,v.y,v.width,v.height)
				if enHIT == true then
        v.dead = true
				world:remove(v)
				table.remove(listOfEnemies, i)
				enHIT = false
				end
    end
			
	end

			end

end

function Bullet:draw()
	if self.dead == true then
		love.graphics.circle("line",self.x,self.y,29)
	end
    --love.graphics.circle("fill", self.x, self.y,self.r)
	love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
end


The specific issue in bullet.lua update()

Code: Select all

	local targetX, targetY = self.x+self.vx*dt, self.y+self.vy*dt
	local newX, newY = world:move(self, targetX, targetY)
	if newX == targetX and newY == targetY then -- no collision
		self.x, self.y = newX, newY
	else -- collision
		self.dead = true
		world:remove(self)
			--enemy update
	for i,v in ipairs(listOfEnemies) do
				enHIT = CheckCollision(self.x ,self.y,self.w,self.h,v.x,v.y,v.width,v.height)
				if enHIT == true then
        v.dead = true
				world:remove(v)
				table.remove(listOfEnemies, i)
				enHIT = false
				end
    end
			
	end

			end
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Bullet collision with enemies using bump

Post by darkfrei »

Just use cols from move:
https://github.com/kikito/bump.lua/blob/master/bump.lua

Code: Select all

function World:move(item, goalX, goalY, filter)
  local actualX, actualY, cols, len = self:check(item, goalX, goalY, filter)

  self:update(item, actualX, actualY)

  return actualX, actualY, cols, len
end
Where col is collision with .item and .other:

Code: Select all

local col           = rect_detectCollision(x,y,w,h, ox,oy,ow,oh, goalX, goalY)

        if col then
          col.other    = other
          col.item     = item
          col.type     = responseName

          len = len + 1
          collisions[len] = col
        end
So check if the .other is your object and set col.other.dead = true
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Bullet collision with enemies using bump

Post by fridays18 »

darkfrei wrote: Mon Dec 05, 2022 6:48 pm Just use cols from move:
https://github.com/kikito/bump.lua/blob/master/bump.lua

Code: Select all

function World:move(item, goalX, goalY, filter)
  local actualX, actualY, cols, len = self:check(item, goalX, goalY, filter)

  self:update(item, actualX, actualY)

  return actualX, actualY, cols, len
end
Where col is collision with .item and .other:

Code: Select all

local col           = rect_detectCollision(x,y,w,h, ox,oy,ow,oh, goalX, goalY)

        if col then
          col.other    = other
          col.item     = item
          col.type     = responseName

          len = len + 1
          collisions[len] = col
        end
So check if the .other is your object and set col.other.dead = true
So col will be tracking the collision as a boolean, than if collision is true than the variable. something = something? im a bit confused on how it works
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Bullet collision with enemies using bump

Post by darkfrei »

fridays18 wrote: Tue Dec 06, 2022 1:13 pm So col will be tracking the collision as a boolean, than if collision is true than the variable. something = something? im a bit confused on how it works
Cols is a collision list an col is a collision.
Every col is a table with an element col.other, that is one of your objects, so you can get it as

Code: Select all

if #cols > 0 then
  local firstObject = cols[1].other
  firstObject.dead = true
  print (firstObject.x, firstObject.y, firstObject.w, firstObject.h, firstObject.dead)
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Bullet collision with enemies using bump

Post by fridays18 »

darkfrei wrote: Tue Dec 06, 2022 8:00 pm
fridays18 wrote: Tue Dec 06, 2022 1:13 pm So col will be tracking the collision as a boolean, than if collision is true than the variable. something = something? im a bit confused on how it works
Cols is a collision list an col is a collision.
Every col is a table with an element col.other, that is one of your objects, so you can get it as

Code: Select all

if #cols > 0 then
  local firstObject = cols[1].other
  firstObject.dead = true
  print (firstObject.x, firstObject.y, firstObject.w, firstObject.h, firstObject.dead)
end
ahh okay ty!
Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests