Collision Help

New to Corona…have published with XCode, and made what are now prototypes in Game Salad…

I am just playing around with projects but I am having trouble with colliding items. I am wanting to have a ball collide with an item (I guess a sensor?) so that if it hits one of the objects it will raise the score and if it collides with a different type of item (sensor) it will lower the score.

I have looked at the documentation, but sadly must admit that I am still a bit confused. Is there a snippet of code somewhere that demonstrate this?

Thanks in advance!

[import]uid: 12894 topic_id: 5104 reply_id: 305104[/import]

i’m guessing you don’t want the object to bounce off the other. so yes you would use .isSensor=true in that case

look at this that uses sensors for the pockets
http://developer.anscamobile.com/content/simple-pool-ipad
[import]uid: 6645 topic_id: 5104 reply_id: 16833[/import]

Thanks for the reply! I really do appreciate it. I have been trying to get this to work for a couple of days now to no avail.

I looked at the pool example, and sadly could not get it to work. I need a simple example of how to do detect a collision and have it do something (add score, change scene, etc.) For example, if I had a red box and a red box, if the ball hits the blue box I want it to do one thing and if the ball hits the red box I want it to do something else.

Can I make an object a sensor, or do I need to create a sensor on the object? I am not sure why this is giving me so many problems.

[import]uid: 12894 topic_id: 5104 reply_id: 16948[/import]

The short answer is assign a parameter to your objects like
rectBlue.myName = “blue”; rectRed.myName = “red”
when you collide you can check event.other.myName.
I’ll post a short example later (unless someone else beats me to it). [import]uid: 12635 topic_id: 5104 reply_id: 19346[/import]

Thanks…I look forward to seeing the example! :slight_smile: Sadly, I have been stuck on this for quite some time now.
[import]uid: 12894 topic_id: 5104 reply_id: 19354[/import]

Here’s another example very similar to the documentation on collisions at:
http://developer.anscamobile.com/content/game-edition-collision-detection
Then new function and returning a group are Director class conventions.
[lua]function new()
local physics = require “physics”
physics.start()
physics.setScale( 60 ) – a value that seems good for small objects (based on playtesting)
physics.setGravity( 0,1)
–physics.setDrawMode( “hybrid” )
local bounce = 1
local localGroup = display.newGroup()
local rBlue1 = display.newRect(localGroup, 130, 50, 50, 50)
local rGreen1 = display.newRect(localGroup, 220, 100, 50, 50)
local rRed1 = display.newRect (localGroup, 150, 300, 100,100)
rBlue1:setFillColor (0, 0, 255) ; rBlue1.myColor = “blue”
rGreen1:setFillColor(0, 255, 0) ; rGreen1.myColor = “green”
rRed1:setFillColor (255, 0 , 0)
physics.addBody( rBlue1, “dynamic”,{bounce = bounce})
physics.addBody( rGreen1, “dynamic”,{bounce = bounce})
physics.addBody( rRed1, “static” )
local function onCollision (self , event)
if event.phase == “began” then
local other = event.other
if other.myColor == “green” then
other.myColor = “blue”
other:setFillColor ( 0,0,255)
elseif other.myColor == “blue” then
other.myColor = “green”
other:setFillColor (0,255,0)
end
end
end – onCollision
rRed1.collision = onCollision
rRed1:addEventListener(“collision”, rRed1)
return localGroup
end – new
new()[/lua] [import]uid: 12635 topic_id: 5104 reply_id: 19430[/import]

I very much appreciate the post, but when I run the sample, the items just run off the screen (I am not seeing anything indicating a collision has taken place). Perhaps I am not understanding how collision detection works on Corona. I thought I would just simply see one of the objects disappear (or some other outward action).

Sorry for my corona ignorance… [import]uid: 12894 topic_id: 5104 reply_id: 19464[/import]

The green and blue rectangles drop down and collide with the red rectangle. Every collision, if it’s blue it becomes green, if it’s green it becomes blue; different behaviors for different colors. [import]uid: 12635 topic_id: 5104 reply_id: 19482[/import]

When I run it, the blue and green rectangles slide off the screen…neither changing colors. [import]uid: 12894 topic_id: 5104 reply_id: 19487[/import]

try increasing the size of the red rectangle so the other rectangles have to collide with it. (I don’t understand how it’s possible we would be seeing different results. I’m using a mac, tested the code on iPhone, iPhone4, and android. I get 6 or 7 bounces before they bounce off the screen.) Play with it. Change things and make it work for you. [import]uid: 12635 topic_id: 5104 reply_id: 19489[/import]

I believe you…not sure why it isn’t working…I’ll continue to play around with it. The problem seems to be that they simply slide behind the red rectangle and off the screen…no bounce…no collision. [import]uid: 12894 topic_id: 5104 reply_id: 19492[/import]