Search code examples
androidluacoronasdkgame-physics

Corona SDK move object without it being effected by gravity


So I am trying to make a Flappy Birdesque game to learn how to make games using Corona SDK. I have a top column that I want to be able to move linearly. So I am using topColumn.setLinearVelocity(), but I also have gravity set in the game so the bird can flap properly :). But my issue is that when the game starts, the pipes fall to the ground due to gravity. Is there a way to move the topColumn and bottomColumn without them being affected by gravity? They are dynamic bodies right now, but I don't know how to move them using static.

Any help?

local physics = require "physics"
physics.start()
physics.setGravity( 0, 100 )
...
function addColumns()
	
	height = math.random(display.contentCenterY - 200, display.contentCenterY + 200)

	topColumn = display.newImageRect('topColumn.png',100,714)
	topColumn.anchorX = 0.5
	topColumn.anchorY = 1
	topColumn.x = display.contentWidth
	physics.addBody(topColumn, "dynamic", {density=0, bounce=0, friction=0})
	topColumn.y = height - 160
	topColumn:setLinearVelocity( -20,0 )
	
	bottomColumn = display.newImageRect('bottomColumn.png',100,714)
	bottomColumn.anchorX = 0.5
	bottomColumn.anchorY = 0
	bottomColumn.x = display.contentWidth
	bottomColumn.y = height + 160
	physics.addBody(bottomColumn, "dynamic", {density=0, bounce=0, friction=0})
	bottomColumn:setLinearVelocity( -20,0 )

end	
...


Solution

  • It sounds like you need kinematic bodies.

    From Corona documentation

    "dynamic" — dynamic bodies are fully simulated. They can be moved manually in code, but normally they move according to forces like gravity or reactionary collision forces. This is the default body type for physical objects in Box2D. Dynamic bodies can collide with all body types.

    "static" — static bodies does not move under simulation and they behave as if they have infinite mass. Static bodies can be moved manually by the user, but they do not accept the application of velocity. Static bodies collide only with dynamic bodies, not with other static bodies or kinematic bodies.

    "kinematic" — kinematic bodies move under simulation only according to their velocity. Kinematic bodies will not respond to forces like gravity. They can be moved manually by the user, but normally they are moved by setting their velocities. Kinematic bodies collide only with dynamic bodies, not with other kinematic bodies or static bodies.