Dragging physic object ourside object, distance joint problems

I have a player that is a physic object, my idea is to move it dragging it since any point of the screen with touch event and keeping the distance fixed. I have tried several objects and joint s type but i can’t solve the issue.

I have tried with a player=dynamic and creating an static object in the touch event.x event.y, but setting player.gravityScale=0 make the joint not to work. Same setting the gravity to 0 in the scene.

I want a simply thing but I dont see the way. Two static doesn’t work, kynematic unuseful.

I want one object(created with touch event) move to other object keeping the distance.

Does anyone know what types of bodies shoud them be?

I dont want to drag the own player object directly because I dont want the finger hide the player

help.png

I answer my own, there was a faster solution, 2 local var, playerX,playerY and then on runtime touch event
 

local function onTouch( event ) if event.phase == "began" then playerX=player.x playerY=player.y elseif event.phase == "moved" then player.x=playerX+(event.x-event.xStart) player.y=playerY+(event.y-event.yStart) elseif event.phase == "ended" or event.phase == "cancelled" then playerX=nil playerY=nil end return true end

When you drag the first object do you want the second object to be dragged or to stay relative to it? Do you want the two objects always to stay in the same place when moving?

Here is a standard drag function:

local player = ... local function drag(e) if (e.phase == "began") then e.target.hasFocus = true e.target.touchjoint = physics.newJoint( "touch", e.target, e.x, e.y ) display.currentStage:setFocus( e.target, e.id ) return nil elseif (e.target.hasFocus) then if (e.phase == "moved") then e.target.touchjoint:setTarget( e.x, e.y ) else e.target.touchjoint:removeSelf() e.target.touchjoint = nil display.currentStage:setFocus( e.target, nil ) e.target.hasFocus = nil end return true end return false end player:addEventListener( "touch", drag )

@horacebury thanks, but that function only work, making click over the object, I wanted tappoing anywhere in the screen. I dont like finger hide the player.

Thanks anyway

Hi @noeguer,

As @horacebury asked, do you want the objects to remain exactly in the same relative position to each other? Or just separated by the same distance, but the object can rotate around the player?

If the second option, have you considered a “distance” joint with no damping and a fixed length?

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html#distance

If the bodies need to remain in that exact relation, @horacebury’s suggestion will still work, but you should just put the touch listener on the screen (stage) itself, and then when the user touch-moves about the screen, move the object in relation to the touch on the screen.

Brent

Brent, the solution of horacebury is not useful for my case, you need to click in the object to move it. And I need to do it from anywhere in the screen.

I have tried with distance joint, but i dont figure out how to do it, I created an physic object in touch position, but static-static doesn work, static-kinematic neither, dynamic-static neither… I need the distance between player and touch point remain fixed, like a selfie stick between player and touch point.

I dont find the way.

Hi @noeguer,

The distance joint is documented in the link I gave you:

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html#distance

You just join the two bodies together, and you set the “dampingRatio” to 1 so it’s a rigid, non-contracting/expanding joint.

You can’t use static bodies for this. You also probably can’t use kinematic, but I haven’t tested that. Try both as dynamic.

Brent

Hi, a progress with two objs dynamic, but I can touch and move aroud the player and the player doesnt move. I want completely fixed the joint, keeping the angle between two objects

I dont know if I explained well. I have the joint but if I touch and draw circle around the player, the player stay in the same position, because the joint change the angle.

I want whereve I move, the player move, see the image above. Fixed angle of the joint and fixed distance.

Hi @noeguer,

This is relatively easy then (no need for a distance joint). Just put the touch listener on the stage and use a standard “moved” listener to move both objects along with the touch.

Brent

Not so easy. Doing that the movement is not smooth when you touch and move fast.
what I am doing is to create three obj on touch with 3 distance joints which move the player inside.

Hi @noeguer,

There’s no “obvious” reason why this would not move properly, and move fast with the touch. Can you please post your code how you’re moving the object(s)?

Brent

I answer my own, there was a faster solution, 2 local var, playerX,playerY and then on runtime touch event
 

local function onTouch( event ) if event.phase == "began" then playerX=player.x playerY=player.y elseif event.phase == "moved" then player.x=playerX+(event.x-event.xStart) player.y=playerY+(event.y-event.yStart) elseif event.phase == "ended" or event.phase == "cancelled" then playerX=nil playerY=nil end return true end

When you drag the first object do you want the second object to be dragged or to stay relative to it? Do you want the two objects always to stay in the same place when moving?

Here is a standard drag function:

local player = ... local function drag(e) if (e.phase == "began") then e.target.hasFocus = true e.target.touchjoint = physics.newJoint( "touch", e.target, e.x, e.y ) display.currentStage:setFocus( e.target, e.id ) return nil elseif (e.target.hasFocus) then if (e.phase == "moved") then e.target.touchjoint:setTarget( e.x, e.y ) else e.target.touchjoint:removeSelf() e.target.touchjoint = nil display.currentStage:setFocus( e.target, nil ) e.target.hasFocus = nil end return true end return false end player:addEventListener( "touch", drag )

@horacebury thanks, but that function only work, making click over the object, I wanted tappoing anywhere in the screen. I dont like finger hide the player.

Thanks anyway

Hi @noeguer,

As @horacebury asked, do you want the objects to remain exactly in the same relative position to each other? Or just separated by the same distance, but the object can rotate around the player?

If the second option, have you considered a “distance” joint with no damping and a fixed length?

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html#distance

If the bodies need to remain in that exact relation, @horacebury’s suggestion will still work, but you should just put the touch listener on the screen (stage) itself, and then when the user touch-moves about the screen, move the object in relation to the touch on the screen.

Brent

Brent, the solution of horacebury is not useful for my case, you need to click in the object to move it. And I need to do it from anywhere in the screen.

I have tried with distance joint, but i dont figure out how to do it, I created an physic object in touch position, but static-static doesn work, static-kinematic neither, dynamic-static neither… I need the distance between player and touch point remain fixed, like a selfie stick between player and touch point.

I dont find the way.

Hi @noeguer,

The distance joint is documented in the link I gave you:

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html#distance

You just join the two bodies together, and you set the “dampingRatio” to 1 so it’s a rigid, non-contracting/expanding joint.

You can’t use static bodies for this. You also probably can’t use kinematic, but I haven’t tested that. Try both as dynamic.

Brent

Hi, a progress with two objs dynamic, but I can touch and move aroud the player and the player doesnt move. I want completely fixed the joint, keeping the angle between two objects

I dont know if I explained well. I have the joint but if I touch and draw circle around the player, the player stay in the same position, because the joint change the angle.

I want whereve I move, the player move, see the image above. Fixed angle of the joint and fixed distance.