[Not Solved] Bullets Collide With Entities?

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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Bullets Collide With Entities?

Post by davisdude »

You have already pinpointed the problem: it can't get the height of the bullets if you haven't made any yet.
This may help you:

Code: Select all

gun = {}
weapon = 1
time_last_bullet = 0
time_between_bullet = .1

function gun:load( x, y, r )
	if love.timer.getTime() > time_last_bullet + time_between_bullet then
		if weapon == 1 then
			r = 3
			s = 200
			time_between_bullet = .1
			d = 5
		elseif weapon == 2 then
			r = 2
			s = 500
			time_between_bullet = .01
			d = 1
		else
			r = 5
			s = 100
			time_between_bullet = 1
			d = 10
		end
		
		startX = player[1].x + player[1].w + r/2
		startY = player[1].y + player[1].h/2 + r/2

		dir = math.atan2(( y - startY ), ( x - startX ))

		gunDx = s * math.cos(dir)
		gunDy = s * math.sin(dir)

		table.insert( gun, { x = startX, y = startY, dx = gunDx, dy = gunDy, id = true, r = r, t = weapon, d = d } )
		time_last_bullet = love.timer.getTime()
	end
end

function gun:switch_weapon()
	weapon = weapon + 1
	if weapon > 3 then
		weapon = 1
	end
	print("Weapon: "..weapon)
end

function gun:draw()
	for i, v in ipairs(gun) do
		if v.id then
			if gun[i].t == 1 then
				love.graphics.setColor( 0, 0, 0 )
				love.graphics.circle( "fill", math.floor(gun[i].x), math.floor(gun[i].y), gun[i].r )
			elseif gun[i].t == 2 then
				love.graphics.setColor( 255, 200, 0 )
				love.graphics.circle( "fill", math.floor(gun[i].x), math.floor(gun[i].y), gun[i].r )
			elseif gun[i].t == 3 then
				love.graphics.setColor( 0, 0, 255 )
				love.graphics.circle( "fill", math.floor(gun[i].x), math.floor(gun[i].y), gun[i].r )
			end
		end
	end
end

function gun:update(dt)
	for i, v in ipairs(gun) do
		if v.id then
			v.x = v.x + v.dx * dt
			v.y = v.y + v.dy * dt
		end
	end
	gun:collide()
end

function gun:collide()
	for b = #gun, 1, -1 do
		if gun[b].x < 0 + gun[b].r or gun[b].x > 800 - gun[b].r or gun[b].y < 0 + gun[b].r or gun[b].y > 600 - gun[b].r then
			print("too far!")
			table.remove(gun, b)
		end	
	end
	for b = #gun, 1, -1 do
		for e = #enemy, 1, -1 do	
			if gun_and_enemy_overlap( gun[b].x, gun[b].y, gun[b].r, enemy[e].x, enemy[e].y, enemy[e].pic:getWidth(), enemy[e].pic:getHeight() ) then
				print("hit!")
				enemy[e].hp = enemy[e].hp - gun[b].d
				table.remove(gun, b)
				print("enemy hp: "..enemy[e].hp)
				if enemy[e].hp <= 0 then
					table.remove(enemy, e)
				end
			end
		end
	end
end

function gun_and_enemy_overlap( bx, by, bw, bh ex, ey, ew, eh ) 
	if bx + bw > ex and
        bx < ex + ew and
        by + bh > ey and
        by < ey + eh then
            return true
        end
end

function love.draw()
love.graphics.scale(1.5)
 												--Draw is for graphics
	if gamestate == "playing" then
		love.graphics.setColor(255, 255, 255)
		love.graphics.draw(backfap, 0, 0)
		
		drawMap()
		playerDraw()
		bulletDraw()
		DrawEnemy()
		setSpawn()
		shadowDraw()
		guiDraw()

	end

	if gamestate == "menu" then
		menuDraw()
	end
	
	if gamestate == "options" then

	end		
	
	if gamestate == "result" then
		endScreen()
	end
        gun:draw()
end

function love.update(dt)
player.healthpercentage = math.floor((player.health/player.maxhealth)*100)								--Update means it Updates every frame (Too much leads to lag)
	if gamestate == "playing" then
		backMenu()
		playerMove(dt)
		updateEnemy(dt)
		bulletShoot(dt)
		bulletUpdate(dt)
		srdUPS()
		shadowMath()
		
	end

	if gamestate == "menu" then
		menuControl()
	end
	
	if gamestate == "options" then

	end
	
	if gamestate == "result" then
		playerReset()
	end

if love.mouse.isDown('l') then
		gun:load( love.mouse.getX(), love.mouse.getY(), r )
	end
	gun:update(dt)
end
end
You may need to do some formatting to get what you want, but I hope this helped! :awesome:
If you need any explanations, just ask!
Last edited by davisdude on Thu Jun 13, 2013 7:55 pm, edited 1 time in total.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Deltise
Prole
Posts: 26
Joined: Sat May 18, 2013 7:23 pm

Re: Bullets Collide With Entities?

Post by Deltise »

I have two tables now, and both the bullets and the entities are created in similar ways, in by which they are made from objects and created using their correspond create functions. Currently the bullet goes through the enemies like their nothing, but I want to detect collisions between the bullet and the enemy.

How would I compare values from two tables, constantly checking for any matches in the x and y axis variables between any bullet and enemy.
(Both tables hold x and y variables of bullets and enemies)




--For creating Bullets

Code: Select all

bullets = {}
bulletEnt = {}

function bulletEnt:create(v)
   local _i = {}
   setmetatable(_i, {__index = self})
   _i:setup(v)
   return _i
end

function bulletEnt:setup(v)
   -- "v" can be used to pass parameters into the Ent to set its default properties, for example...
   self.x = v.x or 100
   self.y = v.y or 100
   self.dir = v.dir or 'down'
   self.range = v.range or 60
   self.speed = v.speed or 300
end


leftsword = love.graphics.newImage("Assets/sword_left.png")
rightsword = love.graphics.newImage("Assets/sword_right.png")
upsword = love.graphics.newImage("Assets/sword_up.png")
downsword = love.graphics.newImage("Assets/sword_down.png")



function bulletEnt:update(dt)
	if self.dir == 'right' then
	self.x = self.x + self.speed * dt
	end
	if self.dir == 'left' then
	self.x = self.x - self.speed * dt
	end
	if self.dir == 'up' then
	self.y = self.y - self.speed * dt
	end
	if self.dir == 'down' then
	self.y = self.y + self.speed * dt
	end
	local dx = player.x - self.x
	local dy = player.y - self.y
	local distance = math.sqrt(dx*dx+dy*dy)
	
	if distance >= self.range then
	   for i, v in pairs(bullets) do
			if v == self then
			table.remove(bullets, i)
			end
		end
	end
	end
	



function bulletEnt:draw()

if self.dir == 'right' then
	love.graphics.setColor(255,255,255)
	love.graphics.draw(rightsword, self.x, self.y)
end

if self.dir == 'left' then
	love.graphics.setColor(255,255,255)
	love.graphics.draw(leftsword, self.x, self.y)
end

if self.dir == 'up' then
	love.graphics.setColor(255,255,255)
	love.graphics.draw(upsword, self.x, self.y)
end

if self.dir == 'down' then
	love.graphics.setColor(255,255,255)
	love.graphics.draw(downsword, self.x, self.y)
end

end
------------------------------------
------------------------------------

-- bullets[#bullets+1] = bulletEnt:create {x = 100, y = 100, speed = 60, range = 60}

------------------------------------
------------------------------------

function DrawBullets()
for i, e in pairs(bullets) do
   e:draw()
end
end
function updateBullets(dt)
for i, e in pairs(bullets) do
   e:update(dt)
end
end


--For Creating Enemy (Skeleton)

Code: Select all

enemies = {}
skullEnemy = {}



function skullEnemy:create(v)
   local _i = {}
   setmetatable(_i, {__index = self})
   _i:setup(v)
   return _i
end
function skullEnemy:setup(v)
   -- "v" can be used to pass parameters into the enemy to set its default properties, for example...
   self.x = v.x or 100
   self.y = v.y or 100
   self.speed = v.speed or 90
   self.health = 200
end

function skullEnemy:update(dt)
   --Update the skull here using "self" to reference itself, for example...
  dx = player.x - self.x
  dy = player.y - self.y
  distance = math.sqrt(dx*dx+dy*dy)
  self.x = self.x + (dx / distance * self.speed * dt)
  self.y = self.y + (dy / distance * self.speed * dt) 
  
if distance <= player.width/2 and distance >= (player.width/2) * -1 then
player.health = player.health - 2
end


if self.health <= 0 then
   for i, v in pairs(enemies) do
     if v == self then
       table.remove(enemies, i)
     end
   end
end
end

function skullEnemy:draw()
   love.graphics.setColor(255,255,255)
   love.graphics.draw(skullpic, self.x, self.y)
end

--enemies[#enemies+1] = skullEnemy:create {x = 100, y = 100, speed = 60}

-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------

function DrawEnemy()
for i, e in pairs(enemies) do
   e:draw()
end
end
function updateEnemy(dt)
for i, e in pairs(enemies) do
   e:update(dt)
end
end
Updated zip is here too.
Attachments
Game.zip
(211.19 KiB) Downloaded 101 times
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Bullets Collide With Entities?

Post by davisdude »

Put gun:collide() in your love.update

Code: Select all

function gun:collide()
   for b = #gun, 1, -1 do
      if gun[b].x < 0 + gun[b].r or gun[b].x > 800 - gun[b].r or gun[b].y < 0 + gun[b].r or gun[b].y > 600 - gun[b].r then
         print("too far!")
         table.remove(gun, b)
      end   
   end
   for b = #gun, 1, -1 do
      for e = #enemy, 1, -1 do   
         if gun_and_enemy_overlap( gun[b].x, gun[b].y, gun[b].r, enemy[e].x, enemy[e].y, enemy[e].pic:getWidth(), enemy[e].pic:getHeight() ) then
            print("hit!")
            enemy[e].hp = enemy[e].hp - gun[b].d
            table.remove(gun, b)
            print("enemy hp: "..enemy[e].hp)
            if enemy[e].hp <= 0 then
               table.remove(enemy, e)
            end
         end
      end
   end
end
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 2 guests