## Image Collision

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Techron
Prole
Posts: 13
Joined: Fri Mar 06, 2015 11:06 pm

### Image Collision

Hello! I am here to ask if there is any way to make it so if you touch an image then it will start a function. I have seen plugins like HardonCollision but I have zero clue how to use it. Help?

BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

### Re: Image Collision

check out BoudingBox.lua here https://love2d.org/wiki/BoundingBox.lua

then with that function use it with an IF statement.

it would be something kinda like..

Code: Select all

if checkCollision(collisionStuff) then
--this is where you would make everything else when they are touching
end


Techron
Prole
Posts: 13
Joined: Fri Mar 06, 2015 11:06 pm

### Re: Image Collision

How would I be able to use bounding box? I did research but I am clueless. I am a newbie when it comes to this kind of stuff.

BluBillz
Prole
Posts: 46
Joined: Tue Oct 29, 2013 6:02 pm

### Re: Image Collision

Techron wrote:How would I be able to use bounding box? I did research but I am clueless. I am a newbie when it comes to this kind of stuff.
Well what bounding box is doing is getting the x and y, width and height of 2 separate "images" for your case. So you would need to fill in the parameters.

Code: Select all

function love.update(dt)

if checkCollision(player.x,player.y,player.width,player.height, enemy.x,enemy.y,enemy.width,enemy.height)then
--What you want them to do when they touch
end

end


I mean that's pretty much what you are looking for i am guessing, just make sure the player and enemy x,y,width,and height are defined in there tables.

Techron
Prole
Posts: 13
Joined: Fri Mar 06, 2015 11:06 pm

### Re: Image Collision

Okay, I got that figured out and I know how to make it do something when they touch. But how do I make it so the player stops moving? Here is my code

Code: Select all

function love.update(dt, key)
function checkCollision(playerx,playery,playerw,playerh, enemyx,enemyy,enemyw,enemyh)
return playerx < enemyx+enemyw and
enemyx < playerx+playerw and
playery < enemyy+enemyh and
enemyy < playery+playerh
end
if checkCollision(playerx,playery,playerw,playerh, enemyx,enemyy,enemyw,enemyh) then
if
end
if love.keyboard.isDown("w") then
playery = playery - 3
end
if love.keyboard.isDown("s") then
playery = playery + 3
end
if love.keyboard.isDown("d") then
playerx = playerx + 3
end
if love.keyboard.isDown("a") then
playerx = playerx - 3
end
end

Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

### Re: Image Collision

first off, move the checkCollision function out of love.update, or any function for that matter,
second off, i have no clue.
"This is what videogames should be like; they should have meaning and triple machine guns"
- Jan Willem Nijman, 2013﻿
github

Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

### Re: Image Collision

Something like the following should work:

Code: Select all

 function checkCollision(playerx,playery,playerw,playerh, enemyx,enemyy,enemyw,enemyh)
return playerx < enemyx+enemyw and
enemyx < playerx+playerw and
playery < enemyy+enemyh and
enemyy < playery+playerh
end

function love.update(dt, key)

if love.keyboard.isDown("w") and checkCollision(playerx,playery,playerw,playerh, enemyx,enemyy,enemyw,enemyh) == false  then
playery = playery - 3
end
if love.keyboard.isDown("s") and checkCollision(playerx,playery,playerw,playerh, enemyx,enemyy,enemyw,enemyh) == false then
playery = playery + 3
end
if love.keyboard.isDown("d") and checkCollision(playerx,playery,playerw,playerh, enemyx,enemyy,enemyw,enemyh) == false then
playerx = playerx + 3
end
if love.keyboard.isDown("a") and checkCollision(playerx,playery,playerw,playerh, enemyx,enemyy,enemyw,enemyh) == false then
playerx = playerx - 3
end
end
So basically if you are not colliding, allow the player to move.

Hope that helps!

Techron
Prole
Posts: 13
Joined: Fri Mar 06, 2015 11:06 pm

### Re: Image Collision

That is close to working, but after I touch the other block I just stop moving all together. Is there any way to fix that?

Lacotemale
Citizen
Posts: 75
Joined: Sat Mar 08, 2014 9:01 pm

### Re: Image Collision

Sounds kinda strange. I'm not sure without having a .love file or code to test.

Techron
Prole
Posts: 13
Joined: Fri Mar 06, 2015 11:06 pm

### Re: Image Collision

Here you go. Sorry about the block speed, and use WASD to move.
Attachments
Testing.love