"Questions that don't deserve their own thread" thread

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.
Locked
User avatar
Foogles
Prole
Posts: 14
Joined: Thu Feb 18, 2016 5:33 pm

Re: "Questions that don't deserve their own thread" thread

Post by Foogles »

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.
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by Ulydev »

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.
If I understood correctly, then https://love2d.org/wiki/Fixture:setSensor should do the job for you.

EDIT: You may also try filters. https://love2d.org/wiki/Fixture:setFilterData
User avatar
Foogles
Prole
Posts: 14
Joined: Thu Feb 18, 2016 5:33 pm

Re: "Questions that don't deserve their own thread" thread

Post by Foogles »

Thank you very much for your quick, helpful response. I apologize for not expressing my thoughts clearly.
User avatar
Foogles
Prole
Posts: 14
Joined: Thu Feb 18, 2016 5:33 pm

Re: "Questions that don't deserve their own thread" thread

Post by Foogles »

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:

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'
Here are the pieces of my code that are relevant to the crash report:

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
How can I appropriately pass the two fixtures from a contact to the HandleCollision() filter?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by s-ol »

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

Code: Select all

local a = HandleCollision()
World:setContactFilter(a)
It should be obvious that this can't work because setcontactfilter will never know about the function

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Foogles
Prole
Posts: 14
Joined: Thu Feb 18, 2016 5:33 pm

Re: "Questions that don't deserve their own thread" thread

Post by Foogles »

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?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by s-ol »

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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Foogles
Prole
Posts: 14
Joined: Thu Feb 18, 2016 5:33 pm

Re: "Questions that don't deserve their own thread" thread

Post by Foogles »

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.
User avatar
Foogles
Prole
Posts: 14
Joined: Thu Feb 18, 2016 5:33 pm

Re: "Questions that don't deserve their own thread" thread

Post by Foogles »

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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by s-ol »

One way is to use :setUserdata(table) to store and :getUserdata() to retrieve it later

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Locked

Who is online

Users browsing this forum: No registered users and 6 guests