How to make an object move in the direction of a touch event, but never stop

So I am new to Corona and I was wondering how to make a ball (which at first is not moving at all) go towards the direction of a touch event and never stopping while maintaining its speed and able to bounce off other objects.

Can do this with the physics library?

Here is my code for the ball:

ball = display.newCircle( display.contentWidth / 2, display.contentHeight / 2, 20 )

ball.strokeWidth = 2
ball:setStrokeColor( 0, 0, 0 )

physics.addBody(ball, “dynamic”, {friction=0, bounce = 1, radius=20})

local function shoot(event)
    if(event.phase == “ended”) then
        --this is where I need help
        
        
    end
end

Runtime:addEventListener(“touch”, shoot)

(there is more code, obviosly, but it isn’t relevant for this post)

Yes, you can do it with the physics library.

  1. In the touch handler, find out where the touch event was in relation to the ball. Store the direction/force you need

  2. Register an enterFrame handler, and inside it, use ball:applyForce(xForce, yForce) to move the ball in the desired direction. Since the handler will be called on every frame, this will apply a continuous force to the ball.

Hi @tyrotech123,

One thing to note is that “applyForce()” will apply a force (hence the name) to the ball recursively, so if you apply that continuously in a “moved” touch handler phase, the ball will continue to move faster and faster until it blasts off the screen. To maintain a more steady pace, you should use “setLinearVelocity()”

http://docs.coronalabs.com/api/type/Body/setLinearVelocity.html

Brent

After some more research I found this, which addressed my question: http://gamedev.stackexchange.com/questions/62664/how-to-shoot-on-direction-of-touch

Yes, you can do it with the physics library.

  1. In the touch handler, find out where the touch event was in relation to the ball. Store the direction/force you need

  2. Register an enterFrame handler, and inside it, use ball:applyForce(xForce, yForce) to move the ball in the desired direction. Since the handler will be called on every frame, this will apply a continuous force to the ball.

Hi @tyrotech123,

One thing to note is that “applyForce()” will apply a force (hence the name) to the ball recursively, so if you apply that continuously in a “moved” touch handler phase, the ball will continue to move faster and faster until it blasts off the screen. To maintain a more steady pace, you should use “setLinearVelocity()”

http://docs.coronalabs.com/api/type/Body/setLinearVelocity.html

Brent

After some more research I found this, which addressed my question: http://gamedev.stackexchange.com/questions/62664/how-to-shoot-on-direction-of-touch