!?Collision Detection?!

I’ve set up a local collision but it’s not working, the simulator doesn’t print anything.
Can you please help me?

It’s all about the following lines:
Line 23
Line 44
Line 65 to 96
Line 113 to 123

[code]local storyboard = require “storyboard”
local scene = storyboard.newScene()
local widget = require “widget”
display.setStatusBar( display.HiddenStatusBar )

local background, ball, pauseBtn, resumeBtn, leftWall, rightWall, ceiling, floorr, lock

local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 49 )

–physics.setDrawMode(“hybrid”)

function scene:createScene( event )
local screenGroup = self.view

background = display.newImage ( “road.jpg” )
screenGroup:insert( background )

ball = display.newImageRect( “SkySoccer Ball.gif”, 100, 100 )
ball.x = display.contentCenterX
ball.y = display.contentCenterY+240
ball.myName = “first ball”
screenGroup:insert( ball )

pauseBtn = display.newImageRect ( “pausebtn.gif”, 50, 50 )
pauseBtn.x = 20
pauseBtn.y = 20
screenGroup:insert( pauseBtn )

resumeBtn = display.newImageRect ( “resumebtn.gif”, 50, 50 )
resumeBtn.x = 20
resumeBtn.y = 20
resumeBtn.isVisible = false
screenGroup:insert( resumeBtn )

leftWall = display.newRect( -10, 0, 10, display.contentHeight )
screenGroup:insert( leftWall )
rightWall = display.newRect( display.contentWidth, 0, 10, display.contentHeight )
screenGroup:insert( rightWall )
ceiling = display.newRect( 0, -10, display.contentWidth, 10 )
screenGroup:insert( ceiling )
floorr = display.newRect( 0, 480, display.contentWidth, 10 )
floorr.myName = “second ball”
screenGroup:insert( floorr )

physics.addBody( leftWall, “static”, { bounce = 0.3 } )
physics.addBody( rightWall, “static”, { bounce = 0.3 } )
physics.addBody( ceiling, “static”, { bounce = 0.3 } )

physics.addBody( ball, { bounce = 0, radius = 40, friction = 1.0 } )

physics.addBody( floorr, “dynamic”, { bounce = 0, friction = 10, density = 1 } )

lock = physics.newJoint( “weld”, leftWall, floorr, floorr.x, floorr.y )

ball.isFixedRotation = true

ball:addEventListener( “touch”, moveBall )

pauseBtn:addEventListener( “touch”, pauseGame )

resumeBtn:addEventListener( “touch”, resumeGame )

ball.collision = onLocalCollision
ball:addEventListener( “collision”, ball )

floorr.collision = onLocalCollision
floorr:addEventListener( “collision”, floorr )

end

function moveBall( event )
if event.phase == “began” then
local x, y = ball.x-event.x, ball.y-event.y
ball:applyLinearImpulse( x / 30, -2, ball.x, ball.y )
return true
end
end

function pauseGame( event )
–if end of touch event
if(event.phase == “ended”) then
–pause the physics
physics.pause()
–pause the ball movement
ball:removeEventListener( “touch”, moveBall )
–make pause button invisible
pauseBtn.isVisible = false
–make resume button visible
resumeBtn.isVisible = true
– indicates successful touch
return true
end
end

function resumeGame( event )
–if end of touch event
if(event.phase == “ended”) then
–resume physics
physics.start()
– resume the ball movement
ball:addEventListener( “touch”, moveBall )
–make pause button visible
pauseBtn.isVisible = true
–make resume button invisible
resumeBtn.isVisible = false
– indicates successful touch
return true
end
end

local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

print( self.myName … ": collision began with " … event.other.myName )

elseif ( event.phase == “ended” ) then

print( self.myName … ": collision ended with " … event.other.myName )

end
end

scene:addEventListener( “createScene”, scene )

return scene[/code] [import]uid: 203192 topic_id: 34272 reply_id: 334272[/import]

Hopefully some more physics friendly people will chime in. The first thing I see is a forward declaration problem. You try to use onLocalCollision before you define it. On lines 65 and 68 where you do:

ball.collision = onLocalCollision

The module doesn’t know that the function below it (local function onLocalCollision()) exists yet, so its assuming onLocalCollision is a global variable, which is likely nil and when it tries to execute, there is no function to execute.

Try moving your onLocalCollision() function towards the top and see if that improves things.

Rob
[import]uid: 199310 topic_id: 34272 reply_id: 136275[/import]

Hopefully some more physics friendly people will chime in. The first thing I see is a forward declaration problem. You try to use onLocalCollision before you define it. On lines 65 and 68 where you do:

ball.collision = onLocalCollision

The module doesn’t know that the function below it (local function onLocalCollision()) exists yet, so its assuming onLocalCollision is a global variable, which is likely nil and when it tries to execute, there is no function to execute.

Try moving your onLocalCollision() function towards the top and see if that improves things.

Rob
[import]uid: 199310 topic_id: 34272 reply_id: 136275[/import]