object:applyForce or applyLinearImpulse

hi,my question is very basic.

i am having a problem with applying a constant force to an abject.what i want is that whenever
i tap on screen,the object should get a fixed force of -50 pixels vertically.
i experimented with both applyLinearImpulse and applyForce but result i am getting is quite weird.

my code looks like this

local centerX=display.contentWidth\*0.5; local centerY=display.contentHeight\*0.5; local physics=require("physics"); physics.start() physics.setGravity( 0, 40) ------------ ball=display.newImageRect("ball.png",30,30); ball.x=centerX-140; ball.y=centerY; physics.addBody(ball, "dynamic",{density=.1, bounce=0.1, friction=1}); ------------ local function flyUp(event) if event.phase == "began" then ball:applyForce(0, -50, ball.x, ball.y) end end Runtime:addEventListener("touch", flyUp)

sometimes when i tap i get a very little force and sometimes a high force.
what can i do so that they ball gets exactly -50 pixels upward whenever i tap??

Hi @sharma.shivam88,

Both of these APIs (":applyForce" and “:applyLinearImpulse”) are based on a physical simulatation of force, which is dependant on the “mass” of the object being pushed, along with any opposing directional forces which might be on the object at the same time (so, there could be motion from a previous application of force/impulse which would affect the new one, and you’re also using downward gravity which will greatly affect the simulation). In other words, you probably won’t be able to accurately move the object exactly 50 pixels using either of these APIs.

If you want an exact movement of 50 pixels directly upward, I suggest you use “transition.to()” instead of a physical force.

https://docs.coronalabs.com/api/library/transition/to.html

HOWEVER, realize that this requires a little extra care in handling because transitions are an “unrelated” method of movement compared to physics. They both move the object on the screen, but the systems which control them are not related. This is described in more detail in the “Transitions” section here:

https://docs.coronalabs.com/guide/physics/limitations/index.html

Hope this helps; post back if you have other questions.

Brent

hi @Brent Sorrentino 

thank you for the reply,i already tried “transition.to()” but it seem to be unstable after 7-8 taps.

Hi again,

Can you explain what you mean by “unstable”? What happens after 7-8 taps?

Brent

Hi @sharma.shivam88,

Both of these APIs (":applyForce" and “:applyLinearImpulse”) are based on a physical simulatation of force, which is dependant on the “mass” of the object being pushed, along with any opposing directional forces which might be on the object at the same time (so, there could be motion from a previous application of force/impulse which would affect the new one, and you’re also using downward gravity which will greatly affect the simulation). In other words, you probably won’t be able to accurately move the object exactly 50 pixels using either of these APIs.

If you want an exact movement of 50 pixels directly upward, I suggest you use “transition.to()” instead of a physical force.

https://docs.coronalabs.com/api/library/transition/to.html

HOWEVER, realize that this requires a little extra care in handling because transitions are an “unrelated” method of movement compared to physics. They both move the object on the screen, but the systems which control them are not related. This is described in more detail in the “Transitions” section here:

https://docs.coronalabs.com/guide/physics/limitations/index.html

Hope this helps; post back if you have other questions.

Brent

hi @Brent Sorrentino 

thank you for the reply,i already tried “transition.to()” but it seem to be unstable after 7-8 taps.

Hi again,

Can you explain what you mean by “unstable”? What happens after 7-8 taps?

Brent