Problem with local Collision detection inside a "class"

Hi everyone :slight_smile:
I have a problem with collision detection in my class
I wrote a file, Object.lua which I use as a class object.
I used this article first, for creating my class :
Lua Classes and Packages in Corona

So here is the code:

Object.lua code

  
local physics = require( "physics" )  
physics.start()  
physics.setGravity( 0, 0 )  
  
Object = {}  
  
function Object:new( nameParam, startX, startY)  
  
 local object = display.newImage ("object.png")  
 object.myName = nameParam  
 object.x = startX  
 object.y = startY  
  
 physics.addBody( object, { bounce=0, density=1.0 } )  
 object.isSensor = true   
  
 -- Collision listener function  
 function object:collision( event )  
 if ( event.phase == "began" ) then  
 print( "Collision : " .. event.object1.myName .. " & " .. event.object2.myName )  
 end  
 end  
  
 -- touch listener function  
 function object:touch( event )  
  
 -- begin focus  
 display.getCurrentStage():setFocus( self, event.id )  
 self.isFocus = true  
  
 if event.phase == "began" then  
 self.markX = self.x -- store x location of object  
 self.markY = self.y -- store y location of object  
  
 elseif self.isFocus then  
  
 if event.phase == "moved" then  
  
 -- drag touch object  
 local x = (event.x - event.xStart) + self.markX  
 local y = (event.y - event.yStart) + self.markY  
  
 -- move object based on calculations above  
 self.x = x   
 self.y = y   
  
 elseif event.phase == "ended" or event.phase == "cancelled" then  
  
 -- end focus  
 display.getCurrentStage():setFocus( self, nil )  
 self.isFocus = false  
 end  
 end  
  
 return true  
 end  
  
 object:addEventListener( "collision", object )  
 object:addEventListener( "touch", object )  
  
 return object  
end  
  
return Object  

As you can see I have a touch listener for my object, which I use for drag and drop, and it’s working fine.
I’ve also a collision listener for my object, which is triggered during collision, it’s working fine too.
My problem is in this line :
print( "Collision : " … event.object1.myName … " & " … event.object2.myName )

>> event.object1 and event.object2 are both nil.
And I can’t understand why. I’m trapped :slight_smile:
I need these references to the objects which are in collision.
Normally, the event parameter has these references…
I did something wrong, but I can’t figure out what is the problem…

Here is the code for main.lua. You can try the example if you want to see by yourself.

main.lua code

local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight )  
background:setFillColor(255 , 255, 255 )  
  
display.setStatusBar( display.HiddenStatusBar )  
  
require "Object"  
  
local object1 = Object:new( "object1", 100, 100)  
local object2 = Object:new( "object2", 100, 200)  

if anyone can help me, It will be very cool.
Thanks
Olivier
[import]uid: 160159 topic_id: 28801 reply_id: 328801[/import]

I am not certain but I think it’s because you have objects 1 and 2 setup as locals in your main file. I would try making them global variables and seeing how that works for you. [import]uid: 49863 topic_id: 28801 reply_id: 116041[/import]

I think the ā€˜collision’ event inside the class only knows about itself… to test this just change the code in the collision event to this :

function object:collision( event )
if ( event.phase == ā€œbeganā€ ) then
print( "Collision : " … event.target.myName)
end
end

you will see in your debug window of your simulator each object is reporting itself when a collision occurs.
I think the simple work around is to set the collision event in main (not in the class). The touch event will be fine in the class.
I had similar issue you had, so my work around was putting the collision event in main, here is your original main.lua with a slight change:

local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
background:setFillColor(255 , 255, 255 )

display.setStatusBar( display.HiddenStatusBar )

require ā€œObjectā€

local object1 = Object:new( ā€œobject1ā€, 100, 100)
local object2 = Object:new( ā€œobject2ā€, 100, 200)

local function onCollision( event )
if ( event.phase == ā€œbeganā€ ) then
print( "Collision : " … event.object1.myName … " & " … event.object2.myName )
end
end

Runtime:addEventListener(ā€œcollisionā€, onCollision)
You will have to create a function or property in the class so you can send whatever info you want back to the object when a collision occurs.
I hope this helps. I look forward to seeing if anyone has an answer that would allow the collision event to be handled directly in the class; but I don’t know how it would be done.

[import]uid: 148857 topic_id: 28801 reply_id: 116045[/import]

@ crazypsycho6973052 :
thank you for your answer!
It’s a good idea. I believed a lot on it :slight_smile: But I tried and it still doesn’t work…

@cyberparkstudios :
Thank you for your answer too!
You probably right, this solution is good. But I like when everything is ā€œpackagedā€ in the class.
I’m a little bit OOP Psycho :slight_smile:
Maybe a CORONA Pro, Peach for instance, could help us understand this issue…

Thanks
Olivier
[import]uid: 160159 topic_id: 28801 reply_id: 116049[/import]

for a local listener try ā€œevent.otherā€ for the collided-with object – e.g., event.other.myName

perhaps something like:

function object:collision( event ) if ( event.phase == "began" ) then print("Collision : " .. self.myName .. " & " .. event.other.myName) end end [import]uid: 119931 topic_id: 28801 reply_id: 116136[/import]

@jeffrey_cc : it works :slight_smile: Thanks a lot! [import]uid: 160159 topic_id: 28801 reply_id: 116236[/import]

@jeffrey

Good to know this. Thanks for sharing. Very helpful!

@oromanetti

I tested this as well, and it works… But I noticed both objects are reporting the event(nearly simultaneously). How will you handle the action you want to take as a result? If both events are reporting it, and each will recognize itself as self.myName, will they both create an action effecting the other? How can you prevent duplicating the action???

Just curious? [import]uid: 148857 topic_id: 28801 reply_id: 116246[/import]

@oromanetti
curious, can you share, how you went about determining which object handles the collision code AND prevent duplicating the action … since both objects receive the event thinking each is the first object of the collision?
Thanks! [import]uid: 148857 topic_id: 28801 reply_id: 116375[/import]