Collision and Composer, detecting an object, Help!

Hi Everyone…

Thanks for all the help and all my last post, you have help me learned a lot.

I have a new app in composer. (starting to work with it)

I have 4 balls – red, yellow, blue, green

all of them are bouncing around in the screen.

I have 1 object, a white circle.

In a simple function I am trying to make a function that will detect when

the “yellow” ball collide with the white circle ONLY!

local function onLocalCollision( self, event )             if ( event.phase == "began" ) then                 print("start collision, yellow touched white")             elseif ( event.phase == "ended" ) then                 print("end collision, yellow is not touching white")             end         end         yellow.collision = onLocalCollision         yellow:addEventListener( "collision", yellow )

I have both names, when I create the objects

yellow.name = “yellow”

white.name = “white”

the yellow is “dynamic” and the white is “static”

Right now, as soon as any ball touches the white

I am detecting the collision, because in terminal I see the print stuff

but I just want to separate the balls, and only when the yellow touches the white?

Would you please help me do this?

Thanks you

Hi @helloworld2013d,

This is best achieved by using collision filters, as described in our guide:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html#filtering

Brent

Hi Brent…

I think for now the:

local greenCollisionFilter = { groupIndex = -2 }

It’s easier…

I just want to make sure how to use it.

So, in the very top of the program I put

local yellowCollisionFilter = { groupIndex = 2 }


Then, when I create the object I use this?

        yellow = display.newImageRect (sceneGroup, "images/yellow.png", 100, 100)         yellow.x = display.contentCenterX         yellow.y = display.contentCenterY - 150         yellow.name = "yellow"         physics.addBody( yellow, "dynamic", { filter=yellowCollisionFilter, density=1.0, friction=0.3, bounce=0.9, radius=50 } )

That will mean that yellow has – groupIndex = 2

so if I want to detect when it collides with skyGate

        skyGate = display.newImageRect (sceneGroup, "images/sky.png", 100, 100)         skyGate.x = display.contentCenterX - 120         skyGate.y = display.contentCenterY - 150         skyGate.name = "skyGate"         physics.addBody(skyGate, "static", { filter=yellowCollisionFilter, density = 1.0, friction = 0.3, bounce = 0.2, radius=50 })

Like that, and I use a -2 number for all the other objects…

it’s getting confusing, and still I detect collision when a green touches skyGate

Or should I use a different number for each object?

like blue - 1

green - 2

red - 3

leftWall - 4

and so on

and only

yellow = 2

skyGate = 2

Do you see I am a little confused… please could you explain a little bit better?

I read the guide twice and still I don’t really get it?

Thanks for your time, and also this post might help a lot other people like me…


And I almost forgot the main thing.

the – local function onLocalCollision (self, event)

end

the addEventListener…

Should I put it to the Yellow?

or to the SkyGate?

        skyGate.collision = onLocalCollision         skyGate:addEventListener( "collision", skyGate )

or to both objects?

Hi Brent… have you had a chance to read my post?

thanks for your time and help.

Hi @helloworld2013d,

If you need to handle “complex” collision filtering between many objects, I suggest that you use the “categoryBits/maskBits” setup, not the “groupIndex” setup. In fact, I never use the “groupIndex” method.

The worksheet and steps outlined in the guide are as clear as we can make it. :slight_smile: It might help if you draw a blank chart/worksheet on a piece of paper and then follow all of the steps carefully.

Take care,

Brent

Hi @helloworld2013d,

This is best achieved by using collision filters, as described in our guide:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html#filtering

Brent

Hi Brent…

I think for now the:

local greenCollisionFilter = { groupIndex = -2 }

It’s easier…

I just want to make sure how to use it.

So, in the very top of the program I put

local yellowCollisionFilter = { groupIndex = 2 }


Then, when I create the object I use this?

        yellow = display.newImageRect (sceneGroup, "images/yellow.png", 100, 100)         yellow.x = display.contentCenterX         yellow.y = display.contentCenterY - 150         yellow.name = "yellow"         physics.addBody( yellow, "dynamic", { filter=yellowCollisionFilter, density=1.0, friction=0.3, bounce=0.9, radius=50 } )

That will mean that yellow has – groupIndex = 2

so if I want to detect when it collides with skyGate

        skyGate = display.newImageRect (sceneGroup, "images/sky.png", 100, 100)         skyGate.x = display.contentCenterX - 120         skyGate.y = display.contentCenterY - 150         skyGate.name = "skyGate"         physics.addBody(skyGate, "static", { filter=yellowCollisionFilter, density = 1.0, friction = 0.3, bounce = 0.2, radius=50 })

Like that, and I use a -2 number for all the other objects…

it’s getting confusing, and still I detect collision when a green touches skyGate

Or should I use a different number for each object?

like blue - 1

green - 2

red - 3

leftWall - 4

and so on

and only

yellow = 2

skyGate = 2

Do you see I am a little confused… please could you explain a little bit better?

I read the guide twice and still I don’t really get it?

Thanks for your time, and also this post might help a lot other people like me…


And I almost forgot the main thing.

the – local function onLocalCollision (self, event)

end

the addEventListener…

Should I put it to the Yellow?

or to the SkyGate?

        skyGate.collision = onLocalCollision         skyGate:addEventListener( "collision", skyGate )

or to both objects?

Hi Brent… have you had a chance to read my post?

thanks for your time and help.

Hi @helloworld2013d,

If you need to handle “complex” collision filtering between many objects, I suggest that you use the “categoryBits/maskBits” setup, not the “groupIndex” setup. In fact, I never use the “groupIndex” method.

The worksheet and steps outlined in the guide are as clear as we can make it. :slight_smile: It might help if you draw a blank chart/worksheet on a piece of paper and then follow all of the steps carefully.

Take care,

Brent