I'm making a game on Corona that launches a ball.

The force is based on the magnitude of the drag: the larger is the drag -> the higher is the force.

I'm having some trouble to do this, because I can't calculate the magnitude of the drag.

I tried a simple (finalX - initialX) but that's not a good idea.

local function onTouch(event)

    if "began" == event.phase then

    if "ended" == event.phase then
        local finalX = event.x
        local finalY = event.y

        forceX = event.x - event.xStart
        forceY = event.y - event.yStart

        print(forceX, forceY)


local function gameComponents()
    local crate = display.newImageRect( "images/sonic.png", 90, 90 )
    crate.x, crate.y = 60, 360
    crate.rotation = 0
    --physics.addBody( crate, "static", { friction=0.3} )

    Runtime:addEventListener( "touch", onTouch )
    return crate


local Game = {}
Game.new = gameComponents
return Game

Use the hypotenuse lenght of the triangle described by the differences on th x and y axis. This traduces to:

dx = event.x - event.xStart
dy = event.y - event.yStart
force = math.sqrt(dx ^ 2 + dy ^ 2)

And that's it. Good luck with your game!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.