Reputation: 25
I'm still struggling with the basics of Lua and Love2d while adding some physics to my first game prototype:
Creating a local or global (non method) collision callback function, that is called when two objects collide works like a charm. Now - As all my game elements are objects (even the game area) I tried to create the callback function as a method of the area-object with no success:
It seems that Area:beginContact
assumes that the first parameter is the instance of the object. As self.world:setCallbacks
is not aware of that, this will to all parameter of the call being shiftes to the left: fixture_a within the callback function becomes filled with fixture_b, fixture_b with contact and contact is becoming nil.
I tried to solve this problem by defining the callback as self.world:setCallbacks(self:beginContact)
. I thought this would force the first parameter to be set as the instance of the object, but this just leads to an error.
Any ideas how to solve this issue?
function Area:new(room)
self.room = room
self.game_objects = {}
self.physics_objects = {}
love.physics.setMeter(64)
self.world = love.physics.newWorld(0, 0, true)
self.world:setCallbacks(self.beginContact) <---- here's the problem
end
function Area:beginContact(fixture_a, fixture_b, contact)
print(self)
local name_a = fixture_a:getUserData()
local name_b = fixture_b:getUserData()
print(name_a, name_b, contact, '!!beginning contact')
end
Upvotes: 1
Views: 251
Reputation: 191
Here's the documentation: https://love2d.org/wiki/World:setCallbacks
I'm just a beginner, but perhaps rename Area:beginContact
to AreaBeginContact
and use setCallbacks(AreaBeginContact,...)
. On collision that is called can get the Area from the fixtures
function AreaBeginContact(fixture_a, fixture_b, contact)
(...)
local fixture_area = fixture_a:getArea()
(you would need to implement getArea function), and do Area-specific stuff.
Upvotes: 0