Can't get the accurate point of collision?

I have been trying to get a single point where two objects collide. In my code, two objects will collide and then they will merge into a larger object but i cant seem to get the most accurate point of collision as it always gives me two different points. Is there any way to get just one point where two objects collide?

Here’s my code:

display.setStatusBar(display.HiddenStatusBar) -- require libraries and set orientation variables local physics = require( "physics" ) physics:start() physics.setDrawMode( "normal" ) physics.setGravity( 0, 0 ) screenWidth = display.contentWidth screenHeight = display.contentHeight centerX = screenWidth \* 0.5 centerY = screenHeight \* 0.5 -- declaring variables and putting up forward references local wbcTouched local twoBalls local score = 0 local scoreText -- put images on screen local background = display.newImageRect( "images/blood.png", 1280, 720 ) local whiteCell01 = display.newImageRect( "images/bacteria.png", 50, 50 ) whiteCell01.x = centerX whiteCell01.y = centerY whiteCell01.id = "whiteCell01" physics.addBody( whiteCell01, "dynamic", {density=3, friction=0.5, bounce=2.5, radius = 26 } ) local whiteCell02 = display.newImageRect( "images/wbc.png", 50, 50 ) whiteCell02.x = centerX + 100 whiteCell02.y = centerY whiteCell02.id = "whiteCell02" physics.addBody( whiteCell02, "dynamic", {density=3, friction=0.5, bounce=2.5, radius = 26 } ) local whiteCell03 = display.newImageRect( "images/wbc.png", 50, 50 ) whiteCell03.x = centerX whiteCell03.y = centerY + 100 whiteCell03.id = "whiteCell03" physics.addBody( whiteCell03, "dynamic", {density=3, friction=0.5, bounce=2.5, radius = 26 } ) scoreText = display.newText( "Score: 0", 0, 0, "Helvetica", 24 ) scoreText.x = centerX scoreText.y = 10 score = 0 -- functions for the game stage function wbcTouched(event) -- touch event local obj = event.target if event.phase == "began" then display.getCurrentStage():setFocus(obj) obj.startMoveX = obj.x obj.startMoveY = obj.y elseif event.phase == "moved" then obj.x = (event.x - event.xStart) + obj.startMoveX obj.y = (event.y - event.yStart) + obj.startMoveY --obj.x=event.x; obj.y=event.y elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus(nil) end return true end local function onCollision( event ) physics.setAverageCollisionPositions( true ) physics.setReportCollisionsInContentCoordinates( true ) local obj1 = event.target.id local obj2 = event.other.id -- local midX = ( event.target.x + event.target.x ) \* 0.5 -- local midY = ( event.other.y + event.other.y ) \* 0.5 if ( event.phase == "began" ) then transition.to( event.target.id, {time = 10, xScale=1.2, yScale=1.2, alpha=0} ) transition.to( event.other.id, {time = 10, xScale=1.2, yScale=1.2, alpha=0} ) local hideObject = function() event.other.isBodyActive = false event.other.isVisible = false event.target.isBodyActive = false event.target.isVisible = false end timer.performWithDelay( 1, hideObject, 1 ) elseif ( event.phase == "ended" ) then twoBalls = display.newImage( "images/twoBalls.png", event.x, event.y ) twoBalls:addEventListener( "touch", wbcTouched ) physics.addBody( twoBalls,"dynamic", {density=3, friction=0.5, bounce=2.5, radius = 36 } ) score = score + 25 scoreText.text = "Score: " .. score end end whiteCell01:addEventListener( "touch", wbcTouched ) whiteCell02:addEventListener( "touch", wbcTouched ) whiteCell03:addEventListener( "touch", wbcTouched ) whiteCell01:addEventListener( "collision", onCollision ) whiteCell02:addEventListener( "collision", onCollision ) whiteCell03:addEventListener( "collision", onCollision )

Hi @naffahamin,

Try adding “return true” at the end of your “onCollision” function, and see if this isolates it down to one collision event.

Best regards,

Brent

Hi @Brent Sorrentino

Thanks, it works. 

Can you please explain why it works like this, so that in future I won’t make the same mistake.

Naffah Amin

Hi @Naffah Amin,

Yes, basically when you have two objects that collide, and both have a collision listener that shares the same listener function, “return true” tells Corona to only process one of those events.

Brent

@Brent

Now I understand it. Thanks!

Hi @naffahamin,

Try adding “return true” at the end of your “onCollision” function, and see if this isolates it down to one collision event.

Best regards,

Brent

Hi @Brent Sorrentino

Thanks, it works. 

Can you please explain why it works like this, so that in future I won’t make the same mistake.

Naffah Amin

Hi @Naffah Amin,

Yes, basically when you have two objects that collide, and both have a collision listener that shares the same listener function, “return true” tells Corona to only process one of those events.

Brent

@Brent

Now I understand it. Thanks!