Sort of mouse joints

Hi,
I need a physics object following the touch event: I have seen the example “MultiPuck”, which uses a tempjoint to get the effect, and it works in my case but for scrolling (I have a camera scrolling)

I need the object (body in this example code) moving with the camera: I have tried to update the joint in the moveCamera function (called in enterFrame) with no result: any idea? It appears that the joint is not responsive enough to make the body move

[lua]local function moveCamera()
if (ball.x > 200 and ball.x < 10000) then
local oldx = game.x
game.x = -ball.x + 200
if (nil ~= body and body.fingerIsNotMoving) then
local diff = (game.x - oldx)
body.tempJoint:setTarget( body.x + diff, body.y )
end
end
end[/lua]

Box2d has “mouse” type joints which could probably help in this case, but Corona don’t… that’s basically what’s stopping my game to be really playable and released in a few days :frowning: [import]uid: 10482 topic_id: 3818 reply_id: 303818[/import]

Anyone here? Is the topic wrong? [import]uid: 10482 topic_id: 3818 reply_id: 12888[/import]

Corona does implement the Box2d “mouse” joint, they just changed the name to “touch” joint.

See http://developer.anscamobile.com/content/game-edition-physics-joints#Touch_joint

I’m using them in one of my current games to make physics objects follow a pre-calculated path of x,y values. Be sure to set the maxForce and frequency parameters quite high to reduce lag.
[import]uid: 9064 topic_id: 3818 reply_id: 12916[/import]

Yes, the “touch” is the one I’m using. My problem is that I have a moving camera, and the object doesn’t follow well the finger.
If the finger touches the screen and stays there without moving, I have found a way to correct the joint, calculating how much the game has scrolled in the meanwhile and calling again the drag function (with a “fake” parameter to distinguish from real touches).
But apparently there is no way to check if the finger moves slightly and then stays there: in this case the object stays behind the touch (imagine the camera moving from left to right).

I modified the original touch drag function to check if the object is moving or not, here is part of the code (I deleted the “params” stuff which is identical to the original)

[lua]local function dragFooter( event, params, fake )

local phase = event.phase
local stage = display.getCurrentStage()

local x = event.x - game.x
local y = event.y - game.y
print (phase)
if “began” == phase then

if nil == body then
body = display.newCircle( x, y, 20)
if (visibleFoot) then
body:setFillColor( 255, 255, 255, 100 )
else
body:setFillColor( 255, 255, 255, 1 )
en
body.isBullet = true
game:insert(body)
physics.addBody( body, “dynamic”, { density=0.3, friction=0.6, bounce = 0.5, radius=20, filter=footCollisionFilter } )
body.isNotMoving = true
end
body.tempJoint = physics.newJoint( “touch”, body, body.x, body.y )

elseif “moved” == phase then
if (not fake) then
body.isNotMoving = false
end
body.tempJoint:setTarget( x, y )

elseif “ended” == phase or “cancelled” == phase then

body.isNotMoving = false
body.tempJoint:removeSelf()
body:removeSelf()
body = nil

end

– Stop further propagation of touch event
return true
end

local function drag( event )
dragFooter( event, { maxForce=20000, frequency=4000, dampingRatio=1.0} , false)
end

– Camera follows ball automatically
local function moveCamera()
if (ball.x > 200 and ball.x < 10000) then
local oldx = game.x
game.x = -ball.x + 200
if (nil ~= body and body.isNotMoving) then
local diff = (game.x - oldx)
local event = {phase=“moved”, x=body.x+oldx, y=body.y}
local params = { maxForce=20000, frequency=4000, dampingRatio=1.0}
dragFooter(event,params,true)
end
end
end[/lua] [import]uid: 10482 topic_id: 3818 reply_id: 12921[/import]

Ok, just after posting, I solved it.

There is no need of “isMoving” property, just always calling the drag function does the trick. I wouldn’t have solved it without posting here, that forced me to think, so thx :slight_smile:

ciao [import]uid: 10482 topic_id: 3818 reply_id: 12924[/import]