-
You didn’t normalize correctly.
-
Your call to applyLinearImpulse is backwards, you have the forces in the wrong place:
object:applyLinearImpulse( xForce, yForce, bodyX, bodyY )
-
You didn’t include the object mass. (If you don’t do this, then later change the size of the ball you’ll need to re-balance your factor calculation.)
-
I made some changes to the original recipe.
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/06/tigerBall.zip
local ballRadius = 20 local ball = display.newCircle( cx, bottom - 80, ballRadius ) ball.factor = 0.06 ball.limit = 50 ball:setFillColor( 1, 0, 0 ) physics.addBody( ball, "dynamic", { radius = ballRadius, bounce = 0 } ) ball.linearDamping = 1 function ball.enterFrame(self) display.remove(self.line) if( not self.lx ) then return end self.line = display.newLine( self.x, self.y, self.lx, self.ly ) self.line:setStrokeColor(1,1,0) self.line.strokeWidth = 4 end Runtime:addEventListener( "enterFrame", ball ) function ball.finalize( self ) Runtime:removeEventListener( "enterFrame", self ) end ball:addEventListener("finalize") local ground = display.newRect( cx, bottom - 50, fullw, 40) physics.addBody( ground, "static") function ball:touch( event ) local id = event.id if(event.phase=="began") then display.getCurrentStage( ):setFocus( self, id ) self.isFocus = true -- self.t0 = system.getTimer() -- elseif(self.isFocus) then if(event.phase=="moved") then self.lx = event.x self.ly = event.y elseif((event.phase=="ended")or(event.phase=="cancelled")) then display.getCurrentStage( ):setFocus( self, nil ) self.isFocus = nil -- local dt = (system.getTimer() - self.t0)/1000 -- local dx = event.x - event.xStart local dy = event.y - event.yStart print(dx,dy) local distance = math.sqrt( dx \* dx + dy \* dy ) -- This is how you normalize (not by using math.abs()): local vx = dx/distance local vy = dy/distance -- local magnitude = self.factor \* distance/dt --print( distance, dt, magnitude, self.t0, system.getTimer(), dx, dy ) magnitude = (magnitude \> self.limit) and self.limit or magnitude magnitude = magnitude \* self.mass -- self:applyLinearImpulse( vx \* magnitude, vy \* magnitude, self.x, self.y ) -- self.lx = nil self.ly = nil -- end end end ball:addEventListener( "touch" )
https://www.youtube.com/watch?v=7np_XgPwgRE&feature=youtu.be