Collision of bodies detection

So, what should I do? I tried putting this but it still doesnt work.

.....or q.object1.myName == "ground" and q.object2.myName == "ball"...  

Try this:

-- tap to bounce -- hide status bar display.setStatusBar( display.HiddenStatusBar ) -- enable physics  require( "physics" ) physics.start() physics.setGravity(0,10) physics.setDrawMode( "hybrid" ) -- walls physics.addBody( display.newRect( -5, 0, 10, display.contentHeight ), "static" ) physics.addBody( display.newRect( display.contentWidth-5, 0, 10, display.contentHeight ), "static" ) physics.addBody( display.newRect( 0, -5, display.contentWidth, 10 ), "static" ) physics.addBody( display.newRect( 0, display.contentHeight-5, display.contentWidth, 10 ), "static" ) -- ball local ball = display.newCircle( display.contentCenterX, display.contentCenterY, 40 ) physics.addBody( ball, "dynamic", { density=1, bounce=.8, friction=.5, radius=40 } ) -- tap handling didTap = false function tap(e)     didTap = true     return true end Runtime:addEventListener("tap", tap) -- bounce handling function ball:collision(e)     if (didTap and e.phase == "began") then         ball:applyLinearImpulse( 0, 40, ball.x, ball.y )     end     didTap = false     return true end ball:addEventListener("collision", ball)  

Yup - it did work, but when the ball stops and rests, it stops bouncing - I think it’ll be better if the if clause is skipped altogether, huh? And what about detection of collision with other objects? Shall I use the object1.myName clause inside the function ball:collision(e) clause?  

I’m trying to infer your requirements from your original code - remember that your first question was about the tap not being detected.

What do you actually want to happen?

It sounds, so far, that you want to detect a user’s tap and then make the ball bounce the next time it collides with another object. My code above does just that. If the ball comes to rest on another object (ie: there is no ended phase to the collision) you will not be able to make it bounce.

If you want to be able to make the ball bounce whenever it is in contact with another object OR (not XOR) the NEXT time it is in contact you need to modify the logic. Your code is just checking when the collision happens, which won’t do it because collisions are not fired between began and ended phases. Try this:

-- tap to bounce -- hide status bar display.setStatusBar( display.HiddenStatusBar ) -- enable physics require( "physics" ) physics.start() physics.setGravity(0,10) physics.setDrawMode( "hybrid" ) -- walls physics.addBody( display.newRect( -5, 0, 10, display.contentHeight ), "static" ) physics.addBody( display.newRect( display.contentWidth-5, 0, 10, display.contentHeight ), "static" ) physics.addBody( display.newRect( 0, -5, display.contentWidth, 10 ), "static" ) physics.addBody( display.newRect( 0, display.contentHeight-5, display.contentWidth, 10 ), "static" ) -- ball local ball = display.newCircle( display.contentCenterX, display.contentCenterY, 40 ) physics.addBody( ball, "dynamic", { density=1, bounce=.8, friction=.5, radius=40 } ) ball.didTap = false ball.collidePhase = "ended" -- tap handling function tap(e) if (ball.collidePhase == "began") then ball:doBounce() elseif (ball.collidePhase == "ended") then ball.didTap = true end return true end Runtime:addEventListener("tap", tap) -- collision handling function ball:collision(e) ball.collidePhase = e.phase if (ball.didTap and e.phase == "began") then ball:doBounce() end return true end ball:addEventListener("collision", ball) -- bounce handling function ball:doBounce() ball:applyLinearImpulse( 0, 40, ball.x, ball.y ) ball.didTap = false end

Horace, that is genius. Thanks man. But if I want it to collide with some object (eg. color changer) that makes the ball do something else, like change color? Shall I add Collide.phase to the color changer stating that if any collision happens, the color should change? or use the precollision to detect and change options so to refine the bounce?

Objects should handle their own state, don’t pass it off to other objects or things will get complicated real quick.

I would say that if you want one type of object to change the ball’s colour, you should have the colour as a property on that object. When the ball collides with that object you can check the property and the ball can change it’s own colour.

Like this…

-- tap to bounce -- hide status bar display.setStatusBar( display.HiddenStatusBar ) -- enable physics require( "physics" ) physics.start() physics.setGravity(0,10) --physics.setDrawMode( "hybrid" ) -- groups walls, toys = display.newGroup(), display.newGroup() -- walls physics.addBody( display.newRect( walls, -5, 0, 10, display.contentHeight ), "static" ) physics.addBody( display.newRect( walls, display.contentWidth-5, 0, 10, display.contentHeight ), "static" ) physics.addBody( display.newRect( walls, 0, -5, display.contentWidth, 10 ), "static" ) physics.addBody( display.newRect( walls, 0, display.contentHeight-5, display.contentWidth, 10 ), "static" ) -- wall colours walls[1].colour = {255,0,0} walls[2].colour = {0,255,0} walls[3].colour = {0,0,255} walls[4].colour = {255,255,255} -- ball local ball = display.newCircle( toys, display.contentCenterX, display.contentCenterY, 40 ) physics.addBody( ball, "dynamic", { density=1, bounce=.8, friction=.5, radius=40 } ) ball.didTap = false ball.collidePhase = "ended" -- tap handling function tap(e) if (ball.collidePhase == "began") then ball:doBounce() elseif (ball.collidePhase == "ended") then ball.didTap = true end return true end Runtime:addEventListener("tap", tap) -- collision handling function ball:collision(e) ball.collidePhase = e.phase if (e.phase == "began") then local colour = e.other.colour ball:setFillColor( colour[1], colour[2], colour[3] ) if (ball.didTap) then ball:doBounce() end end return true end ball:addEventListener("collision", ball) -- bounce handling function ball:doBounce() ball:applyLinearImpulse( math.random(-100,100), 40, ball.x, ball.y ) ball.didTap = false end

Thanks. :) You have cleared all my difficulties. But, if I want to contact you, can you please Private message me your email?

tbh, best way is on here.