How Do I Make Something Happen When Two Sprites Collide?

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
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

How Do I Make Something Happen When Two Sprites Collide?

Post by Kookerus »

I'm trying to make a clone of snake, but I don't know how to find out if two sprites have collided. Can anyone help?
Attachments
game.love
(512 Bytes) Downloaded 123 times
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: How Do I Make Something Happen When Two Sprites Collide?

Post by s-ol »

Kookerus wrote:I'm trying to make a clone of snake, but I don't know how to find out if two sprites have collided. Can anyone help?
You need to do math. That totally depends on how the rest of your program works and I am too lazy to check right now, but basically either:

a) do it yourself, just compare the coordinates or something more useful, depending on what your code works with
b) use a collision detection library like HardonCollider or bump, which is probably an extreme overkill for something like snake.

Either way, you aren't going to check for collisions of sprites but for collisions of "game data" or whatever you want to call it, either with simple math or by comparing their bounding-box-rectangles in some way.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: How Do I Make Something Happen When Two Sprites Collide?

Post by master both »

Use this function to detect if a box is ovelapping another box, you can do the same with sprite, just fill the arguments with the respective variables and check if it's true or false.
User avatar
artofwork
Citizen
Posts: 91
Joined: Mon Sep 15, 2014 1:17 am
Location: East Coast USA

Re: How Do I Make Something Happen When Two Sprites Collide?

Post by artofwork »

Messed around with your code a little just for the fun of it hehe anyway i changed some things.

I created tables for both the player and random player or boxes in this case, gave them names to identify each player, the random player or computer changes color when it collides with the player and the text where it says hey changes to win!!! then changes back when the objects are not colliding with 1 another.

Its not a perfect application but it has some elements to help you get started messing around with it further :)

Code: Select all

local player = {
	x = 20,
	y = 50,
	width = 25,
	height = 25,
	name = "Player",
	hascollided = false
	
}

local randomplayer = {
	x = 100,
	y = 100,
	width = 10,
	height = 10,
	name = "Computer",
	color = {0,255,0},
	hascollided = false
}

function love.load()

end

function love.update(dt)
  if love.keyboard.isDown('w', 'up') then
    player.y = player.y - 3
  elseif love.keyboard.isDown('a', 'left') then
    player.x = player.x - 3
  elseif love.keyboard.isDown('s', 'down')then
    player.y = player.y + 3
  elseif love.keyboard.isDown('d', 'right') then
    player.x = player.x + 3
  end
	collisionDetect(randomplayer, player)
end
-- this function will effect the color of randomplayer & the conditions inside love.draw
function collisionDetect(obj1, obj2)
	if (obj1.x > obj2.x and obj1.x < (obj2.x + obj2.width)) and (obj1.y > obj2.y and obj1.y < (obj2.y + obj2.height)) then
		obj1.hascollided = true
		obj2.hascollided = true
		obj1.color = {255,0,0}
	else
	obj1.hascollided = false
	obj2.hascollided = false
	obj1.color = {0,255,0}
	end
end

function love.draw()
	love.graphics.setBackgroundColor(255, 255, 255)
	love.graphics.setColor(0, 0, 0)
	love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
	-- this will allow you to float a name above a character or box :p
	love.graphics.print(player.name,player.x, player.y - 15)
	love.graphics.print(randomplayer.name,randomplayer.x, randomplayer.y - 15)
	-- since collisionDetect() changes the color when the objects collide it will automatically update randomplayer's color
	love.graphics.setColor(unpack(randomplayer.color))
	love.graphics.rectangle("fill", randomplayer.x, randomplayer.y, randomplayer.width, randomplayer.height)
	
	love.graphics.setColor(0, 0, 255)
	-- this is pretty straight forward if both values are true execute the if, if they are not execute the else
	if player.hascollided and randomplayer.hascollided then 
		love.graphics.print("WIN!!!!!", 400, 300)
	else
		love.graphics.print("Hey", 400, 300)
	end
     
end
and a working love file
game.love
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

Re: How Do I Make Something Happen When Two Sprites Collide?

Post by Kookerus »

Thank you all so much!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Nikki and 7 guests