Help with 'event.contact'

I have some IGmines (in-ground mines) that only explode when either the player or an enemy rolls over them.

I am trying to get my bullets to ignore when they collide with the IGmine’s physics body, at the moment the only thing that happens is the bullet gets removed, but I can’t seem to get the bullet to completely ignore the collision.

Should I attached the ‘preCollison’ to the IGmine itself rather than the bullet?

The code below throws an error:
“attempt to index local ‘event’ (a nil value)”

– The “event” it is referring to, is this line: event.other.collType

(To save you time from having to read through all my code, I took out quite a bit of code and left just enough so you can get a good idea for how I have things setup)
[lua]
local function shootBullet()
    local pt = mathapi.rotateAboutPoint( {x=user_turret.x,y=user_turret.y-55}, user_turret, user_turret.rotation - 90 )
                
            local bullet1 = loader:createSpriteFromSHDocument(“Weapon5ex”, “GameFX”, “LevelAssets.pshs”);
            – various bullet attributes –

–==================== ‘RemoveSelf’ functions for various objects–====================

function checkNumberOfEnemyTanks()
    
    if EnemyTanks == 0 then
        print(" All Enemy Tanks Destroyed ")
        --onLevelCompletion()
        
    end        
end    

    function removePlayer()
        – code for removing player –
    end

–==================== Collision Handling–====================

    function preCollision( self, event )
        if  event.other.collType == “passthru”  then
            print(" hit passthru object")
            event.contact.isEnabled = false
            self.isSensor = true ; self.setAsSensor = true
        end
    end

        local onbullet1Collision = function( self, event )
         if event.phase == “began” then
             preCollision()
             
             PBcollisions = PBcollisions + 1
                
                if event.other.class == “EnemyTank”
                then  
                    print(" Hit EnemyTank “)
                    self:removeSelf()
                    self = nil
                    Emitter = Particles.GetEmitter(“E1”)
                    Emitter.x = event.other.x
                    Emitter.y = event.other.y
                    Particles.StartEmitter(“E1”)
                    display.remove( event.other )
                   – event.other = nil
                    EnemyTanks = EnemyTanks - 1
                    print(” EnemyTank DEAD. Number of EnemyTanks reduced by 1 “)
                    checkNumberOfEnemyTanks()
            end
 
                if event.other.class == “bullet”  
                then
                    self:removeSelf()
                    self = nil
                    print(” bullets collided&removed “)
            end
            
            elseif  event.phase == “ended”  then
        
              –  if PBcollisions == 1 then
                    self:removeSelf()
                    self = nil
                    PBcollisions = 0
                    print(” Projectile Removed. PBcollisions set to 0 ")
              – end    
    end
end
bullet1.preCollision = preCollision
bullet1:addEventListener( “preCollision” )
bullet1.collision = onbullet1Collision
bullet1:addEventListener( “collision”, bullet1 )
end

[/lua]

Hi Saer,

For this, you should definitely use collision filters, not pre-collision and event.contact. These are described in my old (but still valid) forum post here:

http://forums.coronalabs.com/topic/2128-collision-filters-helper-chart/

Brent

P.S. - I’m going to respond to your other post on raycasting tomorrow. I just got too busy today. :slight_smile:

Ahh, darn! lol
I read in your blog post ‘Introducing Physics “event.contact”’ and I was happy when I read the following:
“Using a physics contact , you can gain temporary access to this upcoming collision and tell Corona how you’d like to handle it. You can even tell Corona to ignore it completely!” < My mind translated that into “You can tell corona to ignore a specified collision by using event.contact!” :stuck_out_tongue:

What are your thoughts, if any, on RG’s Collision Calculator? link
It looked much more appealing, being able to use strings instead of a numbers and such.

  • No worries, I can imagine you’re pretty busy. :smiley:

Hi Saer,

Certainly, you could use pre-collision and event.contact for this, but that’s a bit more processor-intensive, and there’s no need to go that route if you can use collision filters. And, by all means, use RoamingGamer’s collision filter calculator… it doesn’t matter how you calculate them, as long as you’re getting the correct values. :slight_smile:

Brent

Alright, thanks.
I only have a few physics-bodies that I don’t want to collide, so it won’t be uber intensive to add a filter. :smiley:

Hi Saer,

For this, you should definitely use collision filters, not pre-collision and event.contact. These are described in my old (but still valid) forum post here:

http://forums.coronalabs.com/topic/2128-collision-filters-helper-chart/

Brent

P.S. - I’m going to respond to your other post on raycasting tomorrow. I just got too busy today. :slight_smile:

Ahh, darn! lol
I read in your blog post ‘Introducing Physics “event.contact”’ and I was happy when I read the following:
“Using a physics contact , you can gain temporary access to this upcoming collision and tell Corona how you’d like to handle it. You can even tell Corona to ignore it completely!” < My mind translated that into “You can tell corona to ignore a specified collision by using event.contact!” :stuck_out_tongue:

What are your thoughts, if any, on RG’s Collision Calculator? link
It looked much more appealing, being able to use strings instead of a numbers and such.

  • No worries, I can imagine you’re pretty busy. :smiley:

Hi Saer,

Certainly, you could use pre-collision and event.contact for this, but that’s a bit more processor-intensive, and there’s no need to go that route if you can use collision filters. And, by all means, use RoamingGamer’s collision filter calculator… it doesn’t matter how you calculate them, as long as you’re getting the correct values. :slight_smile:

Brent

Alright, thanks.
I only have a few physics-bodies that I don’t want to collide, so it won’t be uber intensive to add a filter. :smiley: