Physics not working

Hello!

I have some trouble with my game. I have the following code:

local physics = require ("physics") physics.start(); physics.pause() physics.setDrawMode( "hybrid" ) physics.setGravity(0, 10) local ball = display.newImage("graphics/ball.png") ball:scale(0.2, 0.2) ball.x = display.contentCenterX; ball.y = display.contentCenterY - 150; ball:setFillColor(1, 1, 1) physics.addBody(ball, "dynamic", {radius=ball.contentWidth/2, density=1.0, friction=0.2, bounce=1.0}) local platform = display.newImage("graphics/platform.png") platform:scale(0.5, 0.5) platform.x = display.contentCenterX platform.y = display.contentCenterY + 200 physics.stop()

It should create a ball that is a physic body that is affected by gravity (it is supposed to fall down) but after running this code I only see my ball and it’s not moving. Any ideas what’s wrong?

Regards.

Take the pause off line 3. You’re starting the physics engine, then pausing it immediately.

I have removed completely the physics.pause() and itd doesn’t worked. Then I have placed it before physics.stop() and it neither worked.

You need to remove physics.stop too.

Thank you! It helped! I thought tha physics.stop() was something like “here is the end of physics loop” and placed it at the end. 

The physics.stop() API says to stop running the physics simulation and forget about all the bodies you just setup. Corona has an internal ticker that runs every 30 or 60 times per second. We do as much work as we can during that tick and then update the screen. Your code above does so little work, it gets done in a single frame update. So in effect  you were turning physics on, creating a couple of things and them stopping all activity before the screen could update once.

Corona is an event driven platform. There really isn’t a “game loop”. You can build one if you need it, but for many things you don’t. You create items. They get interacted with you get notified about the interaction and then react to the change.

Rob

Take the pause off line 3. You’re starting the physics engine, then pausing it immediately.

I have removed completely the physics.pause() and itd doesn’t worked. Then I have placed it before physics.stop() and it neither worked.

You need to remove physics.stop too.

Thank you! It helped! I thought tha physics.stop() was something like “here is the end of physics loop” and placed it at the end. 

The physics.stop() API says to stop running the physics simulation and forget about all the bodies you just setup. Corona has an internal ticker that runs every 30 or 60 times per second. We do as much work as we can during that tick and then update the screen. Your code above does so little work, it gets done in a single frame update. So in effect  you were turning physics on, creating a couple of things and them stopping all activity before the screen could update once.

Corona is an event driven platform. There really isn’t a “game loop”. You can build one if you need it, but for many things you don’t. You create items. They get interacted with you get notified about the interaction and then react to the change.

Rob