assigning physics.setGravity a different value

 --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- drawButtons() --\> --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local flyPlane = function(event) if event.phase == "began" and gameIsActive == true then print("began") physics.setGravity( 0, -10 ) elseif event.phase == "ended" and gameIsActive == true then print("ended") physics.setGravity( 0, 7 ) inBound = true elseif event.phase == "moved" and gameIsActive == true then -- do nothing end end --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- keepPlaneInBound() --\> --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local keepPlaneInBound = function() -- print("checking") if paperPlane.y \<= paperPlane.height + 10 and gameIsActive == true and inBound == true then inBound = false paperPlane.y = paperPlane.height + 12 end end

The function flyPlane is set on “touch” “enterframe” eventlistner and keepPlaneInBound has been set to “Runtime” “enterframe” eventlistner. What i want ot achieve is that when the plane exceeds the upper bound limit. Its previous physcis.setGravity value to be completely removed and the new value to be assigned to it when the user lifts the finger (when event = “ended”).
What happens odd is that it does not accept the new physics.setGravity() value. In case i use physics.pause() and then assign it the new geavity value. On physics.start it first completes the part of the previous setGravity value hence moving it upwards again… :confused:

Hi @Arsalan,

I think you’ll need to post a bit more of your code for others to help you… specifically, the part where you create the actual plane, whether this project is done using Storyboard (or not), where you start and pause the physics engine, etc.

Best regards,

Brent

folks. i got what i was looking for. The technique to remove the force applying onto the physics body completely in order to assign it a new gravity value is to simply change its bodyType to a different type and then assigning back the required bodyType again to the object. This way the effect of the previous gravity value on your physics object gets completely removed.This is what i did. It surely worked for me.

local flyPlane = function(event) if event.phase == "began" and gameIsActive == true then print("began") -- paperPlane.bodyType = "dynamic" physics.setGravity( 0, -10 ) -- physics.start() elseif event.phase == "ended" and gameIsActive == true then print("ended") -- paperPlane.bodyType = "dynamic" physics.setGravity( 0, 7 ) -- physics.start() elseif event.phase == "moved" and gameIsActive == true then end end --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- keepPlaneInBound() --\> --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local keepPlaneInBound = function() -- print("checking") if paperPlane.y \<= paperPlane.height + 10 and gameIsActive == true then print("Out of Bound -y: ", paperPlane.bodyType) physics.pause() paperPlane.bodyType = "static" paperPlane.y = paperPlane.height + 11 elseif paperPlane.y \> display.contentHeight - paperPlane.height - scale.height and gameIsActive == true then print("Out of Bound +y: ", paperPlane.bodyType) physics.pause() paperPlane.bodyType = "static" paperPlane.y = display.contentHeight - paperPlane.height - scale.height - 1 else print("In Bound: ", paperPlane.bodyType) paperPlane.bodyType = "dynamic" physics.start() end end

Hi @Arsalan,

I think you’ll need to post a bit more of your code for others to help you… specifically, the part where you create the actual plane, whether this project is done using Storyboard (or not), where you start and pause the physics engine, etc.

Best regards,

Brent

folks. i got what i was looking for. The technique to remove the force applying onto the physics body completely in order to assign it a new gravity value is to simply change its bodyType to a different type and then assigning back the required bodyType again to the object. This way the effect of the previous gravity value on your physics object gets completely removed.This is what i did. It surely worked for me.

local flyPlane = function(event) if event.phase == "began" and gameIsActive == true then print("began") -- paperPlane.bodyType = "dynamic" physics.setGravity( 0, -10 ) -- physics.start() elseif event.phase == "ended" and gameIsActive == true then print("ended") -- paperPlane.bodyType = "dynamic" physics.setGravity( 0, 7 ) -- physics.start() elseif event.phase == "moved" and gameIsActive == true then end end --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- keepPlaneInBound() --\> --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local keepPlaneInBound = function() -- print("checking") if paperPlane.y \<= paperPlane.height + 10 and gameIsActive == true then print("Out of Bound -y: ", paperPlane.bodyType) physics.pause() paperPlane.bodyType = "static" paperPlane.y = paperPlane.height + 11 elseif paperPlane.y \> display.contentHeight - paperPlane.height - scale.height and gameIsActive == true then print("Out of Bound +y: ", paperPlane.bodyType) physics.pause() paperPlane.bodyType = "static" paperPlane.y = display.contentHeight - paperPlane.height - scale.height - 1 else print("In Bound: ", paperPlane.bodyType) paperPlane.bodyType = "dynamic" physics.start() end end