Image Collision
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
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?
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..
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
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
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.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.
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
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
first off, move the checkCollision function out of love.update, or any function for that matter,
second off, i have no clue.
second off, i have no clue.
- Lacotemale
- Citizen
- Posts: 75
- Joined: Sat Mar 08, 2014 9:01 pm
Re: Image Collision
Something like the following should work:
So basically if you are not colliding, allow the player to move.
Hope that helps!
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
Hope that helps!
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.
Re: Image Collision
Here you go. Sorry about the block speed, and use WASD to move.
- Attachments
-
- Testing.love
- (1.01 KiB) Downloaded 215 times
Who is online
Users browsing this forum: Google [Bot] and 5 guests