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

Understanding the 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 about how they might act in the 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 the physics engine

There are three main functions that affect the physics simulation. The following will start, pause, and stop the physics engine:

  • 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 sets the x and y parameters of the global gravity vector in units of meters per second square (acceleration units). The default is (0, 9.8) to simulate standard earth gravity, pointing downwards on the y axis. The syntax is 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).

The syntax is gx, gy = physics.getGravity().

Tilt-based gravity

When you have physics.setGravity(gx, gy) and the 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 to convert between the onscreen Corona coordinates and simulated physics coordinates. This should be done before any of the physical objects are instantiated.

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

The syntax is physics.setScale(value):

physics.setScale( 60 )

physics.setDrawMode

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

The syntax is physics.setDrawMode(mode). The three rendering modes are:

  • physics.setDrawMode("debug"): This mode shows collision engine outlines only, as you can see in the following screenshot:
    physics.setDrawMode
  • physics.setDrawMode("hybrid"): This mode overlays collision outlines on normal Corona objects, as you can see in the following screenshot:
    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: This is used to denote dynamic physics bodies (the default body type)
  • Dark blue: This is used to denote kinematic physics bodies
  • Green: This is used to denote static physics bodies such as the ground or walls
  • Gray: This is used to denote a body that is in a sleeping state due to lack of activity
  • Light blue: This is used to denote 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.

The syntax is 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. However, this will increase processor engagement, so it should be handled carefully because it might slow down the application.

The syntax is physics.setVelocityIterations( value ):

physics.setVelocityIterations( 6 )