Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

adjust speed of moving object
Started by Thodan Feb 25 2019 02:44 PM

- - - - -
3 replies to this topic
applyforce corona sdk
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 11 posts
  • Corona SDK



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!




local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight
local physics = require("physics")
physics.setScale( 60 ) 
physics.setGravity( 0, 0 ) 
display.setStatusBar( display.HiddenStatusBar )
local background = display.newRect(centerX,centerY,_W,_H)
local ball = display.newCircle(centerX, centerY, 16)
physics.addBody(ball, "dynamic", {radius = 16})
ball.alpha = 0
function shotBall( event )
  local b = event.ball
  local phase = event.phase
if phase == "began" then
  ball.isFocus = true
  ball.x = event.x
  ball.y = event.y
  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 = 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 )
    ball:applyForce( (event.x - ball.x),(event.y - ball.y),  ball.x, ball.y )
return true	
background:addEventListener("touch", shotBall)

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Corona Geek

  • 3,070 posts
  • Corona SDK

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.

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Corona Geek

  • 1,373 posts
  • Corona SDK

 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
  -- 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 )

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 11 posts
  • Corona SDK

thanks horacebury and davebollinger!


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