In preCollision when and why is event.contact occasionally nil?

I noticed that every now and again in a collision listener the event.contact field is nil. The docs don’t mention anything about this, so I was wondering what cases cause this and whether it’s an indicator that there’s a bug in my code?

For reference my code is

[lua]

function smuggler:preCollision(event)

    event.contact.isEnabled=smuggler.alpha>0

  end

  smuggler:addEventListener(“preCollision”)

[/lua]

Where smuggler is a physics object.

Hi @tap32,

If you do a basic…

[lua]

print( event.contact )

[/lua]

…inside the function (instead of the line you have), it’s not always triggered? What is the “smuggler” object colliding with?
 

Brent

There’s a variety of other dynamic objects and static objects in the world. Could it be that if it collides with an object marked as a sensor that there’s no contact object? 

When I added the print statement I did notice that occasionally event.contact was nil. I did add the following line:

[lua]

  if not event.contact then

      print (event.other,event.other.isSensor)

      return

   end

[/lua]

Which occasionally prints: table: 0x7fba1e4dd030 nil

Hi again,

Can you give me an estimate on the percentage of times it comes back as nil? Precollisions are very “sensitive” and often (potentially) fire many events right before the actual collision occurs. Most of these are redundant events which can be ignored or filtered out. Is the occasional time this occurs in your game causing issues in the physical behavior?

I haven’t done testing, but it’s possible that sensors don’t get the contact object. If this seems to be the case in your app, I’ll do a little testing and confirm if that is true or false and update the docs.

Best regards,

Brent

Thanks for the offer, I’m working on trying to get an estimate on the number of times this is happening

Hi @tap32,

If you do a basic…

[lua]

print( event.contact )

[/lua]

…inside the function (instead of the line you have), it’s not always triggered? What is the “smuggler” object colliding with?
 

Brent

There’s a variety of other dynamic objects and static objects in the world. Could it be that if it collides with an object marked as a sensor that there’s no contact object? 

When I added the print statement I did notice that occasionally event.contact was nil. I did add the following line:

[lua]

  if not event.contact then

      print (event.other,event.other.isSensor)

      return

   end

[/lua]

Which occasionally prints: table: 0x7fba1e4dd030 nil

Hi again,

Can you give me an estimate on the percentage of times it comes back as nil? Precollisions are very “sensitive” and often (potentially) fire many events right before the actual collision occurs. Most of these are redundant events which can be ignored or filtered out. Is the occasional time this occurs in your game causing issues in the physical behavior?

I haven’t done testing, but it’s possible that sensors don’t get the contact object. If this seems to be the case in your app, I’ll do a little testing and confirm if that is true or false and update the docs.

Best regards,

Brent

Thanks for the offer, I’m working on trying to get an estimate on the number of times this is happening