How Do I Make Something Happen When Two Sprites Collide?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How Do I Make Something Happen When Two Sprites Collide?
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
Re: How Do I Make Something Happen When Two Sprites Collide?
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: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?
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.
- 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?
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.
Re: How Do I Make Something Happen When Two Sprites Collide?
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
and a working love file
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
Re: How Do I Make Something Happen When Two Sprites Collide?
Thank you all so much!
Who is online
Users browsing this forum: Bing [Bot], Semrush [Bot] and 6 guests