adjust speed of moving object

Hello! 

 

I want to make a game where I can move a ball in any direction of the screen by touch and below is the code that works fine to do so, but the ball is going too fast. I want the ball to go at a slower but constant speed. I have already tried to give the ball a density but then the speed isn’t constant and then the speed depends on how far I reach my finger away from the ball. No matter how far I move my finger away from the ball it should always move at the same speed. 

If someone can help me with this that would be great!

 

Thodan

local centerX = display.contentCenterX local centerY = display.contentCenterY local \_W = display.contentWidth local \_H = display.contentHeight local physics = require("physics") physics.start() physics.setScale( 60 ) physics.setGravity( 0, 0 ) display.setStatusBar( display.HiddenStatusBar ) local background = display.newRect(centerX,centerY,\_W,\_H) background:setFillColor(1,1,1) local ball = display.newCircle(centerX, centerY, 16) physics.addBody(ball, "dynamic", {radius = 16}) ball.alpha = 0 ball:setFillColor(0,0,0) function shotBall( event ) local b = event.ball local phase = event.phase if phase == "began" then display.getCurrentStage():setFocus(b) ball.isFocus = true ball.x = event.x ball.y = event.y ball:setLinearVelocity(0,0) ball.angularVelocity = 0 local showBall = transition.to (ball, {alpha=1, xScale=1, yScale=1,time=200}) myLine = nil elseif ball.isFocus then if phase == "moved" then if (myLine) then myLine.parent:remove(myLine) end myLine = display.newLine( ball.x,ball.y, event.x,event.y ) myLine:setStrokeColor( 1, 1, 1, 50/255 ) myLine.strokeWidth = 9 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) ball.isFocus = false if ( myLine ) then myLine.parent:remove( myLine ) end ball:applyForce( (event.x - ball.x),(event.y - ball.y), ball.x, ball.y ) end end return true end background:addEventListener("touch", shotBall)

You have 3 options:

  1. Use a touch joint on the ball to control its position - the touch joint does not need to be driven by the position of the actual touch, just wherever you to it to target. The target location will be where you calculate its next position to be, possibly updated with a transition or timer.
  2. Use an anchor object - a static sensor body (possibly with a filter to prevent collisions with other objects) controlled as with the touchpoint, but tied to the ball with a weld joint.
  3. setLinearVelocity() - literally control the speed and direction of the ball’s physics body. Wherever it is and however it’s moving, you can change that by setting it’s velocity.

 No matter how far I move my finger away from the ball it should always move at the same speed.

literally MOVE at same speed?  or have same amount of force applied?

if the former, then setLinearVelocity()

if the latter, then normalize your force vector, fe:

 local fx = event.x - ball.x -- x-component of force vector local fy = event.y - ball.y -- y-component of force vector local fm = math.sqrt(fx\*fx+fy\*fy) -- magnitude of force vector -- normalize vector f if (fm \> 0) then fx = fx / fm fy = fy / fm end -- a "magic" scaling factor of your own choosing local FORCE\_SCALE = 1.0 -- less or more as desired ball:applyForce( fx\*FORCE\_SCALE, fy\*FORCE\_SCALE, ball.x, ball.y )

thanks horacebury and davebollinger!

I normalized the force vector like you said david and it works!

thanks!

You have 3 options:

  1. Use a touch joint on the ball to control its position - the touch joint does not need to be driven by the position of the actual touch, just wherever you to it to target. The target location will be where you calculate its next position to be, possibly updated with a transition or timer.
  2. Use an anchor object - a static sensor body (possibly with a filter to prevent collisions with other objects) controlled as with the touchpoint, but tied to the ball with a weld joint.
  3. setLinearVelocity() - literally control the speed and direction of the ball’s physics body. Wherever it is and however it’s moving, you can change that by setting it’s velocity.

 No matter how far I move my finger away from the ball it should always move at the same speed.

literally MOVE at same speed?  or have same amount of force applied?

if the former, then setLinearVelocity()

if the latter, then normalize your force vector, fe:

 local fx = event.x - ball.x -- x-component of force vector local fy = event.y - ball.y -- y-component of force vector local fm = math.sqrt(fx\*fx+fy\*fy) -- magnitude of force vector -- normalize vector f if (fm \> 0) then fx = fx / fm fy = fy / fm end -- a "magic" scaling factor of your own choosing local FORCE\_SCALE = 1.0 -- less or more as desired ball:applyForce( fx\*FORCE\_SCALE, fy\*FORCE\_SCALE, ball.x, ball.y )

thanks horacebury and davebollinger!

I normalized the force vector like you said david and it works!

thanks!