[bump.lua] detect a collision only between 2 items

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.
Post Reply
maxtrautwein
Prole
Posts: 17
Joined: Sun Apr 14, 2019 5:14 pm

[bump.lua] detect a collision only between 2 items

Post by maxtrautwein »

hi! Let's say I have 4 items in my world item1, item2, item3 and item4.
I want to print "Collision between item1 and item2" only when item1 and item2 collide, and not when item1 and item3 collide for example, is there a way to check a specific collision between 2 items from the world with bump.lua??
Thanks in advance!!
MrFariator
Party member
Posts: 511
Joined: Wed Oct 05, 2016 11:53 am

Re: [bump.lua] detect a collision only between 2 items

Post by MrFariator »

You can write a custom filter for those specific circumstances. Lets say that item2 is a table, and has a "isItem2" field in it, then you could just detect it like so:

Code: Select all

local filterForItem1 = function(item, other)
  if     other.isItem2 then
    -- return whatever the intended collision response is
  else
    -- handle other cases
  end
end
Then just pass this filter to world:move() when moving item1. You can then handle the actual collisions by checking for the "isItem2" field in the list of returned collisions.

Edit: I read your question wrong. Instead of the above, you can use world:project() to simulate the movement of item1, check for collisions with item3, and set a flag in item1 if there's gonna be a collision. Then during world:move, that flag handles collisions against item2 in the custom filter you use.

For a simpler use (eq. your printing case), you can just use a single pass with world:move(), like the following (assuming that the filter can allow item1 to collide with both item2 and item3, eq. crossing over them):

Code: Select all

local item2Col, item3Col = false, false

local newX, newY, cols, len = world:move( item1, goalX, goalY, filter )
for i = 1, len do
  if cols[i].other.isItem2 then
    item2Col = true
  elseif cols[i].other.isItem3 then
    item3Col = true
  end
end

if item2Col and not item3Col then
  print("Colliding with item2!")
end
maxtrautwein
Prole
Posts: 17
Joined: Sun Apr 14, 2019 5:14 pm

Re: [bump.lua] detect a collision only between 2 items

Post by maxtrautwein »

I don't understand what I should return in the filter function, when i put "cross" it raises an error
MrFariator
Party member
Posts: 511
Joined: Wed Oct 05, 2016 11:53 am

Re: [bump.lua] detect a collision only between 2 items

Post by MrFariator »

Can you post the code for the filter function, as well as how you're passing it to bump?
maxtrautwein
Prole
Posts: 17
Joined: Sun Apr 14, 2019 5:14 pm

Re: [bump.lua] detect a collision only between 2 items

Post by maxtrautwein »

Code: Select all

function camera.filter(item,other)
	if other.is3 then 
		return "cross"
	end	
end	

function camera.update(dt) 
	local filter = camera.filter(player,camera.rect[3])
	player.x, player.y, cols, len = world:move(player,player.x,player.y,filter)
	for i = 1, len do
		if cols[i].other.is3 then 
			print('player collide with rect 3')
		end	
	end	
end
in my code the example is not the same, here i want to detect if my player collide with the rectangle3, and if it does, i print "collide"
MrFariator
Party member
Posts: 511
Joined: Wed Oct 05, 2016 11:53 am

Re: [bump.lua] detect a collision only between 2 items

Post by MrFariator »

That's not quite how the filter is supposed to be used. Here's the fixed up code:

Code: Select all

function camera.update(dt) 
	player.x, player.y, cols, len = world:move(player,player.x,player.y, camera.filter) -- note we're passing the function, not its return value
	for i = 1, len do
		if cols[i].other.is3 then 
			print('player collide with rect 3')
		end	
	end	
end
Now, if camera.rect[3] (or some other object in the bump world) contains the field "is3", then the print function will be called upon collision.
maxtrautwein
Prole
Posts: 17
Joined: Sun Apr 14, 2019 5:14 pm

Re: [bump.lua] detect a collision only between 2 items

Post by maxtrautwein »

Ok now i understand! thank you very much for your time :))
Post Reply

Who is online

Users browsing this forum: No registered users and 82 guests