Search code examples
luacoronasdkgame-physics

Too much force in using function physics.addForce();


I want to add force to the grenade according to the touch positions of the user.

--this is the code   
physics.addBody(grenade1,"dynamic",{density=1,friction=.9,bounce=0})
grenade1:applyForce(event.x,event.y,grenade1.x,grenade1.y)

Here more the x and y positions are the lower the force is. But the force here is too high that the grenade is up in the sky.


Solution

  • You must calibrate the force applied. Remember that F=ma so if x=250 then F=250, if the mass of the display object (set when added body, based on material density * object area) is 1 then acceleration a = 250, which is very large. So try:

    local coef = 0.001
    grenade1:applyForce(coef*event.x, coef*event.y, grenade1.x, grenade1.y)
    

    and see what you get. If too small, increase coef until the response is what you are looking for. You may find that linear (i.e., constant coef) doesn't give you the effect you want, for example coef=0.01 might be fine for small y but for large y you might find that coef=0.001 works better. In this case you would have to make coef a function of event.y, for example

    local coef = event.y < 100 and 0.001 or 0.01
    

    You could also increase the mass of the display object, instead of using coeff.

    Recall also that top-level corner is 0,0: force in top level corner will be 0,0. So if you want force to increase as you go up on the screen, you need to use display.contentHeight - event.x.