Swiping an object to fixed points is not response in good way

You can continue using linear impulse if you want, by applying strictly up, down, left, or right impulse in place of the associated transitions I used. Just remember that impulse “builds up” so you should reset the object’s linear velocity each time you make a new swipe:

[lua]

rock:setLinearVelocity( 0, 0 )

[/lua]

And, you can still use transitions if you want, but you’ll need to follow the advice shown here:

https://docs.coronalabs.com/guide/physics/limitations/index.html

Brent

As for making the object gradually slow down when using force (as in, it slowly comes to a stop), you can apply linear damping to it:

https://docs.coronalabs.com/api/type/Body/linearDamping.html

local physics = require("physics") physics.start( ) physics.setGravity( 0, 0 ) local rock = display.newCircle( display.contentCenterX, display.contentCenterY, 25 ) rock.canBeSwiped = true physics.addBody( rock, "dynamic" ) rock.linearDamping = .5 local function doSwipe( event ) if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) elseif ( event.phase == "moved" ) then if ( rock.canBeSwiped == false ) then return end local dX = event.x - event.xStart local dY = event.y - event.yStart if dY \> 5 and ( dX \>= -2 and dX \<= 2 ) then -- Down! rock.canBeSwiped = false rock:setLinearVelocity( 0, 50 ) rock.canBeSwiped = true elseif dY \< -5 and ( dX \>= -2 and dX \<= 2 ) then -- Up! rock.canBeSwiped = false rock:setLinearVelocity( 0, -50 ) rock.canBeSwiped = true elseif dX \< -5 and ( dY \>= -2 and dY \<= 2 ) then -- Left! rock.canBeSwiped = false rock:setLinearVelocity( -50, 0 ) rock.canBeSwiped = true elseif dX \> 5 and ( dY \>= -2 and dY \<= 2 ) then -- Right! rock.canBeSwiped = false rock:setLinearVelocity( 50, 0 ) rock.canBeSwiped = true end elseif ( event.phase == "ended" or event.phase == "cancelled" ) then display.getCurrentStage():setFocus( nil ) print("FOCUS RELEASED") end end rock:addEventListener( "touch", doSwipe )

Thanks Both . but none of this made any difference .

using touch ( swipe ) along with applyForce or setLinearVelocity or any other forces make the movement of the object pretty unstable in some points ( i mean it will not make the object all the time go in vertical or horizontally )  . i guess it happen when you hit to touch the object from different angles or corners of the square that make the square move in different ways according to where the square had been touched  .

but in the other hand when i use Axis ( just Up,down,left,right ) arrows and apply those forces or to setLinearVelocity  the object ( square ) can move very steady without any change in its direction

but putting Axis in a puzzle game for mobile is not emotional

btw i tried many things like changing physics parameter , linear Dumping etc, the size of the square is 32*32 

If you’re applying force (not setting linear velocity directly as @scottrules44 showed) then you’ll need to reset the object’s linear velocity to 0,0 on each new swipe. Otherwise, the force will build up and the object will go off in different angles.

Also, in @scottrules44 code, the “rock.canBeSwiped = true” command should probably not be done in each conditional block, because that could easily result in force being applied multiple times during the swiping motion. Instead, try putting that command just once in the “ended”/“cancelled” condition area, so that the user can’t make another swipe in any direction until he/she has lifted off the screen.

Brent

Which exact API are you using to move the object at this time? It appears you don’t want to use transitions, and that’s OK, but I need to see what you’re doing now in your own code…

here is my code .

--------------------------------------------------------------------------------- -- -- scene.lua -- --------------------------------------------------------------------------------- local sceneName = ... local composer = require( "composer" ) local json = require( "json" ) local loadsave = require( "loadsave" ) local myData = require( "mydata" ) local physics = require("physics") -- Load scene with same root filename as this file local scene = composer.newScene( sceneName ) --------------------------------------------------------------------------------- centerX = display.contentCenterX centerY = display.contentCenterY screenLeft = display.screenOriginX screenWidth = display.contentWidth - screenLeft \* 2 screenRight = screenLeft + screenWidth screenTop = display.screenOriginY screenHeight = display.contentHeight - screenTop \* 2 screenBottom = screenTop + screenHeight display.contentWidth = screenWidth display.contentHeight = screenHeight function scene:create( event ) local sceneGroup = self.view -- Called when the scene's view does not exist -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc myData.settings = loadsave.loadTable( "settings.json" ) local pl2 = self:getObjectByName( "pl2" ) local bk = self:getObjectByName( "bk" ) BaseT = self:getObjectByName( "BaseT" ) local borderDown = self:getObjectByName( "borderDown" ) local borderUp = self:getObjectByName( "borderUp" ) local borderLeft= self:getObjectByName( "borderLeft" ) local borderRight = self:getObjectByName( "borderRight" ) borderDown.name="borderDown" borderUp.name="borderUp" borderLeft.name="borderLeft" borderRight.name="borderRight" borderDown.name="borderDown" pl2.name="pl2" BaseT.name="BaseT" --physics.setDrawMode( "hybrid" ) --physics.setDrawMode( "debug" ) local CoronaDev = display.newText( "Level 2",25,25, "Exo-ExtraLight.otf", 13 ) CoronaDev.x=30 CoronaDev.y=-20 sceneGroup:insert(CoronaDev) CoronaDev:setTextColor( 0.5,0.5,0.5 ) ---New Move With Tweaks --- local function handleSwipe( event ) if ( event.phase == "began" ) then -- display.getCurrentStage():setFocus( event.target ) -- This made object stop moving once the swipe stop in fast way elseif ( event.phase == "moved" ) then local dX = event.x - event.xStart local dY = event.y - event.yStart if event.phase == "moved" then if dX \>= 10 then event.target:setLinearVelocity(600,0) elseif dX \<= -10 then event.target:setLinearVelocity(-600,0) elseif dY \>= 10 then event.target:setLinearVelocity(0,600) elseif dY \<= -10 then event.target:applyLinearImpulse(0,-600) end end return true elseif ( event.phase == "ended" or event.phase == "cancelled" ) then --event.target:setLinearVelocity(0,0) --- This put strange stop if the touch was so short end end local SaveLevel = function () myData.settings.currentLevel = 2 end SaveLevel() local GoToNextLevel = function ( event ) if ( event.phase == "began" ) then if event.target.name == "BaseT" then Level=2 composer.showOverlay( "pass") pl2:setLinearVelocity(0,0) myData.settings.unlockedLevels = 3 end end end local SquareGoAway = function ( event ) if ( event.phase == "began" ) then if event.target.name == "borderDown" or event.target.name == "borderUp" or event.target.name == "borderLeft" or event.target.name == "borderRight" or event.target.name == "borderRightDown"then local GameOver=1 composer.gotoScene ("GameOver") return true end end end local SoundImpact = function ( event ) if ( event.phase == "began" ) then SoundImpactName = audio.play (SoundImpact,{ channel=3}) pl2:setLinearVelocity(0,0) return true elseif ( event.phase == "ended" ) then end return true end borderRight:addEventListener( "collision", SquareGoAway ) borderUp:addEventListener( "collision", SquareGoAway ) borderLeft:addEventListener( "collision", SquareGoAway ) borderDown:addEventListener( "collision", SquareGoAway ) BaseT:addEventListener( "collision", GoToNextLevel ) pl2:addEventListener( "touch", handleSwipe ) pl2:addEventListener( "collision", SoundImpact ) Runtime:addEventListener( "enterFrame", SaveLevel ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase loadsave.saveTable( myData.settings, "settings.json" ) end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc --BaseT:removeEventListener( "collision", GoToNextLevel ) end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

i removed the ( dX >= -2 and dX <= 2 ) and stuff becuase it made the swipe hard to complete .

pl2 is the square ( dynamic , Fixed Rotation , Active , Awake , Sleeping Allowed , 0 Angular Damping and 0 linear Damping , 0 bounce , 100 Friction , 20 Density , 0 Radius )

Other objects ( other Squares ) ( Static , Active , Friction 100 , bounce 0 , linear and angular damping are 0 , Radius 0 , Density is 100 )

No X and Y Gravity .

I am using Composer GUI

Corona SDK 2912 . and test same behavior on Smart Phone

it is a maze and if i cant fix this strange behavior players can use it to be moved to the exit ( hole ) of the maze without having to solve the maze using Vertical and Horizontal movement

@Brent i already recorded a video showing the problem . how can i send it privately to you ?

Hi @sobh,

The “( dX >= -2 and dX <= 2 )” (and similar) was really a crucial element in my example. It controls (limits) all of those diagonal swipes that you’re trying to avoid. If you want to make the swipes a bit easier to complete, but not allow swipes too far toward the 45, 135, 225, and 315 degree vectors, then change -2 and 2 to something like -5 and 5 respectively.

You also really should keep that “canBeSwiped” functionality I built in. If you take all of that out, you’re allowing the touch detection to continue even after a “successful swipe”. That is going to cause problems one way or another.

Best regards,

Brent

Hi Brent .

i test it again using your advice and it is pretty working as intended for now .

i will update this later