Image Collision

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.
Techron
Prole
Posts: 13
Joined: Fri Mar 06, 2015 11:06 pm

Image Collision

Post by Techron » Sat Mar 07, 2015 6:52 pm

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?

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

Re: Image Collision

Post by BluBillz » Sat Mar 07, 2015 7:12 pm

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

Post by Techron » Sat Mar 07, 2015 7:55 pm

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.

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

Re: Image Collision

Post by BluBillz » Sat Mar 07, 2015 8:30 pm

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

Post by Techron » Sun Mar 08, 2015 5:12 pm

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

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

Re: Image Collision

Post by Doctory » Sun Mar 08, 2015 6:36 pm

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

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

Re: Image Collision

Post by Lacotemale » Sun Mar 08, 2015 7:51 pm

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

Post by Techron » Sun Mar 08, 2015 8:38 pm

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?

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

Re: Image Collision

Post by Lacotemale » Mon Mar 09, 2015 1:50 pm

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

Post by Techron » Mon Mar 09, 2015 7:05 pm

Here you go. Sorry about the block speed, and use WASD to move.
Attachments
Testing.love
(1.01 KiB) Downloaded 64 times

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests