Page 1 of 1

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

Posted: Sat Apr 27, 2019 10:03 pm
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!!

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

Posted: Sat Apr 27, 2019 10:08 pm
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

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

Posted: Sat Apr 27, 2019 10:53 pm
by maxtrautwein
I don't understand what I should return in the filter function, when i put "cross" it raises an error

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

Posted: Sat Apr 27, 2019 10:54 pm
by MrFariator
Can you post the code for the filter function, as well as how you're passing it to bump?

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

Posted: Sat Apr 27, 2019 11:07 pm
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"

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

Posted: Sat Apr 27, 2019 11:14 pm
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.

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

Posted: Sat Apr 27, 2019 11:53 pm
by maxtrautwein
Ok now i understand! thank you very much for your time :))