Page 1 of 1

Bullet death when hitting a bump wall

Posted: Wed Nov 30, 2022 2:20 pm
by fridays18
Im trying to make it so that when a bullet class hits a wall it "dies"

dying =

Code: Select all

self.alive = false
I have it so it died when it goes outside the screen but I have no clue where to even start when it comes to killing it on a Bump lib wall, I added each bullet to the world when its made, and made its move system work so it has collisions, just dont know how to kill them on walls, any help is very much appreciated

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

		if player.d == 4 then
    self.path = 4
	elseif player.d == 1 then
		self.path = 1
	elseif player.d == 2 then
		self.path = 2
	elseif player.d == 3 then
    self.path = 3
		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

			self.x, self.y = world:move(self, self.x+self.vx, self.y+self.vy)

end

function Bullet:draw()
    --love.graphics.circle("fill", self.x, self.y,self.r)
	love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
end



Re: Bullet death when hitting a bump wall

Posted: Wed Nov 30, 2022 3:51 pm
by darkfrei
fridays18 wrote: Wed Nov 30, 2022 2:20 pm Im trying to make it so that when a bullet class hits a wall it "dies"

dying =

Code: Select all

self.alive = false
I have it so it died when it goes outside the screen but I have no clue where to even start when it comes to killing it on a Bump lib wall, I added each bullet to the world when its made, and made its move system work so it has collisions, just dont know how to kill them on walls, any help is very much appreciated
(not tested)

Code: Select all


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

	if player.d == 1 then -- left
		self.vx = -self.speed
	elseif player.d == 2 then -- right
		self.vx = self.speed
	elseif player.d == 3 then -- up
		self.vy = -self.speed
	elseif player.d == 4 then -- down
		self.vy = self.speed
	end

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

function Bullet:update(dt)
	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
	end
	if self.x > screenx or self.x < 0 or self.y > screeny or self.y < 0 then
		self.dead = true
	end
end

Re: Bullet death when hitting a bump wall

Posted: Wed Nov 30, 2022 4:01 pm
by fridays18
darkfrei wrote: Wed Nov 30, 2022 3:51 pm
fridays18 wrote: Wed Nov 30, 2022 2:20 pm Im trying to make it so that when a bullet class hits a wall it "dies"

dying =

Code: Select all

self.alive = false
I have it so it died when it goes outside the screen but I have no clue where to even start when it comes to killing it on a Bump lib wall, I added each bullet to the world when its made, and made its move system work so it has collisions, just dont know how to kill them on walls, any help is very much appreciated
(not tested)

Code: Select all


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

	if player.d == 1 then -- left
		self.vx = -self.speed
	elseif player.d == 2 then -- right
		self.vx = self.speed
	elseif player.d == 3 then -- up
		self.vy = -self.speed
	elseif player.d == 4 then -- down
		self.vy = self.speed
	end

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

function Bullet:update(dt)
	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
	end
	if self.x > screenx or self.x < 0 or self.y > screeny or self.y < 0 then
		self.dead = true
	end
end
Works perfect thank you!