How to make an object not respond to gravity with Corona SDK

Today’s post is a short tutorial about physics and gravity.

If you would like an object in your Corona SDK built game to not respond to gravity, you can use the property gravityScale. Like the name implies, this will change how gravity affects the object. You can set this property to 0 to make it not affected by gravity. Let’s take a look.

local bounceBar = display.newRect(sceneGroup, 0, 0, 100, 20)
physics.addBody( bounceBar )
bounceBar.gravityScale = 0
In this example, we created a rectangle, added a physics body to it, and then set the gravity scale to 0. This forced our object to not respond to gravity.

Another quick pro-tip is to add the body type “static” for stationery objects. For example, if you want the bounce bar to bounce other objects but not move itself, you would make bodyType = “static” to make it stay put. Here’s how:

local bounceBar = display.newRect(sceneGroup, 0, 0, 100, 20)
physics.addBody( bounceBar, "static" )
bounceBar.gravityScale = 0

 

Leave a Reply

Your email address will not be published. Required fields are marked *