No collision between static and dynamic objects

I want to create a maze game using gyroscope to move the ball. The problem is that the ball collide with the walls as it should but after a while it passes through the walls. I can’t explain why.

XdAZw.png

Here is my code:

ball=display.newImage("ball1.png") ball.x=30 ball.y=display.contentCenterY ball.name="ball" physics.addBody ( mazepart1, "static" ,physicsData:get("mazepart1")) physics.addBody ( mazepart1, "static" ,physicsData:get("mazepart2")) physics.addBody (ball, "dynamic" ,physicsData:get("ball1")) top=display.newLine( 0, 1, display.contentWidth, 1) physics.addBody ( top, "static") bottom=display.newLine( 0, display.contentHeight, display.contentWidth,display.contentHeight) physics.addBody ( bottom, "static") left=display.newLine( 1, 0, 1, display.contentHeight) physics.addBody ( left, "static") right=display.newLine( display.contentWidth-1, 0, display.contentWidth-1, display.contentHeight) physics.addBody ( bottom, "static") local function onGyroscopeDataReceived( event ) -- Calculate approximate rotation traveled via deltaTime. --physics.setGravity( 10 \* event.xGravity, -10 \* event.yGravity ) local deltaRadiansX = event.xRotation \* event.deltaTime local deltaDegreesX = deltaRadiansX \* (180 / math.pi) local deltaRadiansY = event.yRotation \* event.deltaTime local deltaDegreesY = deltaRadiansY \* (180 / math.pi) ball.x = ball.x + deltaDegreesX \*5 ball.y = ball.y + deltaDegreesY \*5 end -- Set up the above function to receive gyroscope events if the sensor exists. Runtime:addEventListener( "gyroscope", onGyroscopeDataReceived )

Any idea why may this happen?

I noticed two things in your code that you might want to change.  First, you create the walls using display.newLine.  display.newLine doesn’t work well (or at all) with physics, so instead, use display.newRect, and make the rectangle thin, like a line.  Second, in your gyroscope listener, you set the coordinates of the ball directly.  Since the ball is a physics object, setting the coordinates directly can result in funny behavior.  Instead, try using the gyroscope information and apply a force or impulse to the ball, letting the physics engine take care of moving the ball in response to the force.

  • Andrew

I noticed two things in your code that you might want to change.  First, you create the walls using display.newLine.  display.newLine doesn’t work well (or at all) with physics, so instead, use display.newRect, and make the rectangle thin, like a line.  Second, in your gyroscope listener, you set the coordinates of the ball directly.  Since the ball is a physics object, setting the coordinates directly can result in funny behavior.  Instead, try using the gyroscope information and apply a force or impulse to the ball, letting the physics engine take care of moving the ball in response to the force.

  • Andrew