is it possible to lock applyForce speed ?

Hey, can this be done? I have a standard ship which floats up and down using applyForce on touch. At the moment what happens is  the player player presses on the screen for a few seconds and the ship just flies up, gathering acceleration on touch. I know this is the meaning of force (that it picks up speed) but is there a way i can lock the speed on touch? 

I have also used setLinearvelocity too and found its controls to rigid and less fluid. It also jumps a tad bit on touch rather than float up.

fly function

function activateFly(ship,event) if fuel \< 60 then ship:applyForce(0, -1.4, ship.x, ship.y) elseif fuel \> 60 then ship:applyForce(0, -1.4, ship.x, ship.y) ship.gravityScale = 1.75 end end function touchScreen(event) print("touch") if event.phase == "began" then ship.enterFrame = activateFly Runtime:addEventListener("enterFrame", ship) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", ship) end end Runtime:addEventListener("touch", touchScreen)

Hi @dip,

If I understand correctly, you want the force to be applied but “max out” at a certain point, so the user can’t continue to press and let the ship fly off at an extreme speed? In that case, you should get the velocity during the press enterFrame, and if it’s over a certain threshold, just don’t apply any additional force.

Brent

Hi Brent,

Yes thats exactly what I want to happen but I am not sure how I could add that in my code above though. How do i set the max force value which acts as its max out point on player press? 

Hi @dip,

It appears that you’re ship can only move directly upward? What I was suggesting is that you simply read the current linear velocity, and if it’s <= to a certain value (it will be <= for upward Y movement, not >= ), then you simply don’t apply any more force.

Does that help get you started on it?

Brent

Hi Brent, I understand what you mean but I am not sure how I would add that into my code above. If i am to get the value of my applyForce property through “print” the value appears to be nil and 5. 

Hi @dip,

I think this would do the trick… of course, I have no idea if the -5 is the correct value, so you’d have to determine that (just print out your “vy” values and check what maximum upward speed seems correct to you in the game).

[lua]

function activateFly(ship,event)

   local vx,vy = ship:getLinearVelocity()

   if ( vy <= -5 ) then return end

   if  fuel < 60 then

      ship:applyForce(0, -1.4, ship.x, ship.y)

   elseif fuel > 60 then

      ship:applyForce(0, -1.4, ship.x, ship.y)

      ship.gravityScale = 1.75

   end

end

[/lua]

Thats perfect, thank you so much it works exactly as I wanted it to be. 

Thank you Brent your a hero

Hi @dip,

If I understand correctly, you want the force to be applied but “max out” at a certain point, so the user can’t continue to press and let the ship fly off at an extreme speed? In that case, you should get the velocity during the press enterFrame, and if it’s over a certain threshold, just don’t apply any additional force.

Brent

Hi Brent,

Yes thats exactly what I want to happen but I am not sure how I could add that in my code above though. How do i set the max force value which acts as its max out point on player press? 

Hi @dip,

It appears that you’re ship can only move directly upward? What I was suggesting is that you simply read the current linear velocity, and if it’s <= to a certain value (it will be <= for upward Y movement, not >= ), then you simply don’t apply any more force.

Does that help get you started on it?

Brent

Hi Brent, I understand what you mean but I am not sure how I would add that into my code above. If i am to get the value of my applyForce property through “print” the value appears to be nil and 5. 

Hi @dip,

I think this would do the trick… of course, I have no idea if the -5 is the correct value, so you’d have to determine that (just print out your “vy” values and check what maximum upward speed seems correct to you in the game).

[lua]

function activateFly(ship,event)

   local vx,vy = ship:getLinearVelocity()

   if ( vy <= -5 ) then return end

   if  fuel < 60 then

      ship:applyForce(0, -1.4, ship.x, ship.y)

   elseif fuel > 60 then

      ship:applyForce(0, -1.4, ship.x, ship.y)

      ship.gravityScale = 1.75

   end

end

[/lua]

Thats perfect, thank you so much it works exactly as I wanted it to be. 

Thank you Brent your a hero