Page 1 of 2
Image Collision
Posted: Sat Mar 07, 2015 6:52 pm
by Techron
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?
Re: Image Collision
Posted: Sat Mar 07, 2015 7:12 pm
by BluBillz
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
Re: Image Collision
Posted: Sat Mar 07, 2015 7:55 pm
by Techron
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.
Re: Image Collision
Posted: Sat Mar 07, 2015 8:30 pm
by BluBillz
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.
Re: Image Collision
Posted: Sun Mar 08, 2015 5:12 pm
by Techron
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
Re: Image Collision
Posted: Sun Mar 08, 2015 6:36 pm
by Doctory
first off, move the checkCollision function out of love.update, or any function for that matter,
second off, i have no clue.
Re: Image Collision
Posted: Sun Mar 08, 2015 7:51 pm
by Lacotemale
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!
Re: Image Collision
Posted: Sun Mar 08, 2015 8:38 pm
by Techron
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?
Re: Image Collision
Posted: Mon Mar 09, 2015 1:50 pm
by Lacotemale
Sounds kinda strange. I'm not sure without having a .love file or code to test.
Re: Image Collision
Posted: Mon Mar 09, 2015 7:05 pm
by Techron
Here you go. Sorry about the block speed, and use WASD to move.