Corona SDK Mobile Game Development:Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Understanding Corona physics API

Corona has made it convenient to add physics to your games, especially if you've never worked on one before. The engine uses Box2D and takes only a few lines to incorporate it into your application than what it normally takes to have it set up.

Working with the physics engine in Corona is fairly easy. You use display objects and set them as a physical body in your code. Images, sprites, and vector shapes can be turned into a physical object. This is substantial in visualizing how you want your objects to react in an environment you have created. You can see results right away rather than guessing how they might act in a physical world.

Setting up the physics world

Making the physics engine available in your app requires the following line:

local physics = require "physics"

Starting, pausing, and stopping physics

There are three main functions that affect the physics simulation:

  • physics.start(): This will start or resume the physical environment. It is usually activated at the beginning of the application for physics bodies to take effect.
  • physics.pause(): This stops the physics engine temporarily.
  • physics.stop():This basically destroys the physical world altogether.

physics.setGravity

This function returns the x and y parameters of the global gravity vector, in units of meter per second square (acceleration units). The default is (0, 9.8) to simulate standard Earth gravity, pointing downwards on the y-axis. For example,

Syntax: physics.setGravity(gx, gy)

physics.setGravity( 0, 9.8 ): Standard Earth gravity

physics.getGravity

This function returns the x and y parameters of the global gravity vector, in units of meter per second square (acceleration units).

Syntax: gx, gy = physics.getGravity()

Tilt-based gravity

When you have physics.setGravity(gx, gy) and accelerometer API applied, implementing tilt-based dynamic gravity is simple. The following is an example of creating the tilt-based function:

function movePaddle(event)
  
  paddle.x = display.contentCenterX - (display.contentCenterX * (event.yGravity*3))
    
end

Runtime:addEventListener( "accelerometer", movePaddle )

The accelerometer is not present in the Corona Simulator; a device build must be created to see the effect.

physics.setScale

This function sets the internal pixels-per-meter ratio used in converting between the onscreen Corona coordinates and simulated physics coordinates. This should be done before any physical objects are instantiated.

The default scaling value is 30. For devices of higher resolution like iPad, Android, or iPhone 4, you may wish to increase this value to 60 or more.

Syntax: physics.setScale( value )

physics.setScale( 60 )

physics.setDrawMode

There are three rendering modes for the physics engine. This can be altered at any time.

Syntax: physics.setDrawMode( mode )

  • physics.setDrawMode( "debug" ): This mode shows collision engine outlines only
    physics.setDrawMode
  • physics.setDrawMode( "hybrid" ): This mode overlays collision outlines on normal Corona objects
    physics.setDrawMode
  • physics.setDrawMode( "normal" ): This mode is the default Corona renderer, with no collision outlines
    physics.setDrawMode

The physics data is displayed using color-coded vector graphics, which reflect different object types and attributes:

  • Orange—dynamic physics bodies (the default body type)
  • Dark blue—kinematic physics bodies
  • Green—static physics bodies such as the ground or walls
  • Gray—a body that is sleeping due to lack of activity
  • Light blue—joints

physics.setPositionIterations

This function sets the accuracy of the engine's position calculations. The default value is 8, meaning that the engine will iterate through eight position approximations per frame for every object but will increase processor engagement, so it should be handled carefully because it might slow down the application.

Syntax: physics.setPositionIterations( value )

physics.setPositionIterations( 16 )

physics.setVelocityIterations

This function sets the accuracy of the engine's velocity calculations. The default value is 3, meaning that the engine will iterate through three velocity approximations per frame for every object but will increase processor engagement, so it should be handled carefully because it might slow down the application.

Syntax: physics.setVelocityIterations( value )

physics.setVelocityIterations( 6 )