Page 1 of 1

Help with setting up a collision system, without much change

Posted: Sun Nov 03, 2013 3:32 am
by Krimzen
Hi, I'm participating in the Mini Ludum Dare 46, the first dare on my own, and I've had to look up a few tutorials to help, they all work BUT I have one issue, now I need my bullets to collide with the enemy.

This is my code, I want it to detect collision, and then I want to be able to add my own things once collision has been detected between the two.

(Also if possible have the enemies move down individually at a steady pace)

I'm sorry if this has been posted before, but I'm not sure exactly where to look and where I do look it doesn't seem to fit what I've done.

(I am also aware that putting everything into one script is a bad idea, but it's worked so far, plus I prefer everything to be directly at my fingertips (I have to change this habit XD ))

Code: Select all

local Gamestate = require 'gamestate'
local game = Gamestate.new()

screenX = 720
screenY = 512

player = {
	x = 100,
	y = 400,
	speed = 200
}

bullets = { }
enemy = {}

function game:init()

	--VARIABLES

	--ENEMIES
	self.table = {} -- stores the single enemys later
	self.timer = 0 -- use with spawntimer
	self.spawnTime = 1.5 -- time when the next enemy should spawn
	enemyCount = 0
	

	--Graphics Variables
	g = love.graphics
	bg = g.newImage("images/gamebg.png")
	enemy = g.newImage("images/rubbish.png")
	hero = g.newImage("images/sharkman.png")
	g.setFont(g.newFont("fonts/shark.otf", 40))

	--Sound Variables
	a = love.audio

	--DO ON LOAD


	--Sound

end


function game:update(dt)


	--MOVE LEFT
	if love.keyboard.isDown('left') and player.x > 0 then 
		player.x = player.x - dt * player.speed 
	end

	--MOVE RIGHT
	if love.keyboard.isDown('right') and player.x < 620 then 
		player.x = player.x + dt * player.speed 
	end


	local i, o
for i, o in ipairs(bullets) do
	o.x = o.x + math.cos(o.dir) * o.speed * dt
	o.y = o.y + math.sin(o.dir) * o.speed * dt
	if (o.x < -10) or (o.x > love.graphics.getWidth() + 10)
	or (o.y < -10) or (o.y > love.graphics.getHeight() + 10) then
		table.remove(bullets, i)
	end
end


--SPAWN ENEMY--

 self.timer = self.timer + dt
   if self.timer > self.spawnTime then
      game:enemy( math.random( 36, screenX - 40 ) ,100 , 100) -- math.random for a different position everytime you spawn an enemy
      self.timer = 0
      enemyCount = enemyCount + 1
   end

end


function game:enemy( x, y, xvel ) -- to use self you have to type a " : " to say that you want to use the self. 
   
   local temp =    {
                  x = x,
                  y = y,
                  w = 32,
                  h = 64,
                  xvel = xvel,
               }
   table.insert( self.table, temp )
end




function game:draw()
	--Background
	bg:setFilter('nearest', 'nearest')
	g.draw(bg, 0, 0, 0, 4)

	--Hero
	hero:setFilter('nearest', 'nearest')
	g.draw(hero, player.x, player.y, 0, 2)


	love.graphics.setColor(255, 255, 255, 224)
local i, o
for i, o in ipairs(bullets) do
	love.graphics.circle('fill', o.x, o.y, 10, 8)
end

	--SPAWN ENEMY

	enemy:setFilter('nearest', 'nearest')
   for i, v in ipairs( self.table ) do
      g.draw(enemy, v.x, v.y, 0, 2.5)
   end

   if enemyCount >= 10 then
   	g.print("Too much pollution, Game over!", 50, 50)
   	g.print("Press Enter!", 250, 125)
   end


   --GAME OVER STUFFZ

end

function game:keypressed( key )

	if key == " " and enemyCount < 10 then
		local direction = math.atan2( -1, 0)
		table.insert(bullets, {
			x = player.x + 65,
			y = player.y,
			dir = direction,
			speed = 400
	})
	end

end

function game:keyreleased( key )

end

return game
Thanks for the help guys, my .love file is attached :)

Re: Help with setting up a collision system, without much ch

Posted: Sun Nov 03, 2013 7:30 am
by Roland_Yonaba
Your bullets have an x, y, width and height properties.
The player also have an x, y, width and height properties.
Just perform a bounding box collision test, and you are good to go.