Is there a way that I can make a love.physics body that doesn't prevent other bodies from colliding but does detect the collision?
I want to reduce the players health when he collides with a certain physics body, but I don't want to actually prevent the player from moving through this body.
"Questions that don't deserve their own thread" thread
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: "Questions that don't deserve their own thread" thread
If I understood correctly, then https://love2d.org/wiki/Fixture:setSensor should do the job for you.Foogles wrote:I want to reduce the players health when he collides with a certain physics body, but I don't want to actually prevent the player from moving through this body.
EDIT: You may also try filters. https://love2d.org/wiki/Fixture:setFilterData
Re: "Questions that don't deserve their own thread" thread
Thank you very much for your quick, helpful response. I apologize for not expressing my thoughts clearly.
Re: "Questions that don't deserve their own thread" thread
I have another question regarding love physics.
I am attempting to use the function World:setContactFilter( filter ) to control what happens when different objects collide.
I am not sure how to set the contact filter function with appropriate syntax; the filter function is supposed to receive two arguments, one for each of the fixtures that have collided, but when I attempt this in my code the arguments are not received (they are nil).
Here is the crash report:
Here are the pieces of my code that are relevant to the crash report:
How can I appropriately pass the two fixtures from a contact to the HandleCollision() filter?
I am attempting to use the function World:setContactFilter( filter ) to control what happens when different objects collide.
I am not sure how to set the contact filter function with appropriate syntax; the filter function is supposed to receive two arguments, one for each of the fixtures that have collided, but when I attempt this in my code the arguments are not received (they are nil).
Here is the crash report:
Code: Select all
main.lua:45: attempt to index local 'f1' (a nil value)
Traceback
main.lua:45: in function 'HandleCollision'
main.lua:89: in function 'CreateWorld'
main.lua:6: in function 'load'
Code: Select all
function CreateWorld()
_world = love.physics.newWorld(0, 0, true)
_world:setContactFilter(HandleCollision()) --line 89
love.physics.setMeter(64)
end
Code: Select all
function HandleCollision(f1, f2)
if ((f1.getGroupIndex == 1) and (f2.getGroupIndex == 2)) or ((f1.getGroupIndex == 2) and (f2.getGroupIndex == 1)) then -- line 45
_p.health = _p.health - _e.damage
CheckDeath()
return false
elseif (f1.getGroupIndex == 2) and (f2.getGroupIndex == 2) then
return false
else
return true
end
end
Re: "Questions that don't deserve their own thread" thread
It's supposed to be world:setContactFilter(HandleCollision) because if you add the () then you call the function with no arguments, which causes errors. Also the return value is what is passed into setContactFilter and since you don't return a function that would error too.
What you are doing is the same as
It should be obvious that this can't work because setcontactfilter will never know about the function
What you are doing is the same as
Code: Select all
local a = HandleCollision()
World:setContactFilter(a)
Re: "Questions that don't deserve their own thread" thread
Thank you for the logical explanation to my mistakes. It did occur to me to call it as World:setContactFilter(HandleCollision), which does appear to be the correct approach, but that did not work for other reasons. I see now that it didn't work because I am making multiple mistakes.
You say that the filter is supposed to return a function, but the documentation states otherwise: "The function should return a boolean value where true means the fixtures will collide and false means they will pass through each other."
Have I misinterpreted this somehow?
You say that the filter is supposed to return a function, but the documentation states otherwise: "The function should return a boolean value where true means the fixtures will collide and false means they will pass through each other."
Have I misinterpreted this somehow?
Re: "Questions that don't deserve their own thread" thread
No, the documentation is right, what I was saying was if you call it with setcontactfilter(HandleCollision()), then that function would need to return another function (that returns booleans) for it to work.
I hadn't looked at your HandleCollision code, I'm pretty sure you are missing parenthesis with the getGroupIndex es, those are functions right? (Should be f1:getGroupIndex() == )
Right now you are comparing the C function itself which is going to be the same for all fixtures I guess.
I hadn't looked at your HandleCollision code, I'm pretty sure you are missing parenthesis with the getGroupIndex es, those are functions right? (Should be f1:getGroupIndex() == )
Right now you are comparing the C function itself which is going to be the same for all fixtures I guess.
Re: "Questions that don't deserve their own thread" thread
You are absolutely right -- if I hadn't made that syntax error then I wouldn't have gone on to make the setcontactfilter(handlecollision()) mistake... Thank you so much for your time.
Re: "Questions that don't deserve their own thread" thread
The physics contact filter sends two fixtures in a collision to my HandleCollision() function.
Is there a way for me to access the parent table of the fixtures passed?
I have a table that contains the shape, body, fixture, and crucial data such as health and damage of the objects.
When a collision occurs, I want to reduce the health associated with that object. However, I can't refer to fixture.health because the fixture and the health are both children of the table in question.
Is there a way for me to access the parent table of the fixtures passed?
I have a table that contains the shape, body, fixture, and crucial data such as health and damage of the objects.
When a collision occurs, I want to reduce the health associated with that object. However, I can't refer to fixture.health because the fixture and the health are both children of the table in question.
Re: "Questions that don't deserve their own thread" thread
One way is to use :setUserdata(table) to store and :getUserdata() to retrieve it later
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests