physics.setGravity()

Type Function
Library physics.*
Return value none
Revision Release 2024.3703
Keywords physics, gravity
See also physics.getGravity()

Overview

Sets the x and y components of the global gravity vector in units of m/s². The default is ( 0, 9.8 ) to simulate standard Earth gravity, pointing downward on the y axis.

Note that this function can be called at any time in the physics simulation, upon which all objects under the effect of gravity will respond to the new values.

Syntax

physics.setGravity( gx, gy )
gx, gy (required)

Numbers. Sets the global gravity vector in x/y direction, in units of m/s². In other words, the "pull" of gravity in the x (horizontal) and y (vertical) directions.

Example

local physics = require( "physics" )
physics.start()

physics.setGravity( 0, 6 )

local crate = display.newRect( 100, 50, 40, 40 )
crate:setFillColor( 1 )
crate.x = 180
crate.y = -50
crate.rotation = 5
 
physics.addBody( crate, { density=3.0, friction=0.5, bounce=0.3 } )