linearDamping but in 1 axis

Hi all,

I use a ‘touch’ event to drag my dynamic body around; it works great with a joint!

But when I release I can ‘throw’ the object; really I just want it to drop with gravity i.e. have no x axis movement. I have tried setting linearvelocity on the x to 0. Also lineardampling works great but not on individual axis.

  body.linearDamping

Any ideas???

Hi m.hula,

Can you show me the code where you’re “dropping” this image and trying to apply 0 to the linear X velocity? This should be working, so I’m curious why it’s not… a peek at the code may help solve this…

Thanks,

Brent

Hey there!

Here you go:

  if event.phase == “ended” and fudge == true then

        placing.lhTag = original_tag

        display.getCurrentStage():setFocus( nil )

        placing.isFocus = false

        placing.gravityScale = 1

        placing.isAwake = true

        placing.isSensor = false

        fudge = false

        placing.isBodyActive = false

The objects a dynamic moved via a touch joint. I *assume* it’s getting its velocity off the joint…

Cheers

Hi m.hula,

Yes, the dragged object is getting the residual force from the pull of the joint.

Try getting and then setting the linear velocity as follows, and put these lines before the “isBodyActive = false” line. I’m not sure that you should even keep that line, since you expressed that the body should begin to fall down, and making it inactive will obviously prevent that. :slight_smile:

[lua]

local vx, vy = placing:getLinearVelocity()

placing:setLinearVelocity( 0, vy )

[/lua]

The reason for the first line is that you need to get the Y linear velocity and then maintain it (apply it) in the new setting on the second line. This will keep the current Y velocity from before the drop, but stop the X entirely.

Of course, if you want to object to just stop dead in its tracks, set both values to 0 and omit the first line.

Brent

Hi Brent,

Yes I originally tried something like you suggested.

Here’s what I tried though:

  if event.phase == “ended” and fudge == true then

        placing.lhTag = original_tag

        display.getCurrentStage():setFocus( nil )

        placing.isFocus = false

        placing.gravityScale = 1

        placing.isAwake = true

        placing.isSensor = false

        fudge = false

        --placing.isBodyActive = false

        --placing.linearDamping = 10000000

        --placing.linearDamping = 0

        

– just messing around here trying to fix it!!

        placing.isBodyActive = true

        local vx, vy = placing:getLinearVelocity()

        placing:setLinearVelocity( 0, vy )

        placing.isBodyActive = false

        placing.isBodyActive = true

        

        --loader:cancelPreCollisionCallbackBetweenTags(placing.lhTag,“fruit”, pre_collision)

        --loader:cancelPreCollisionCallbackBetweenTags(placing.lhTag,“static”, pre_collision)

        loader:cancelBeginOrEndCollisionCallbackBetweenTags(placing.lhTag,“static”, beginend_collision)

        loader:cancelBeginOrEndCollisionCallbackBetweenTags(placing.lhTag,“fruit”, beginend_collision)

        remove_listener_place2()

        add_listener_touch()

        touch_joint:removeSelf()

Still has x velocity

Any ideas?

Cheers

Hi m.hula,

I think this may be simply the order in which you’re processing things. Try removing the joint before you get/set the linear velocity. I suspect that the joint is still exerting its pull on the object because you remove it at the end.

Brent

Hi Brent,

Yeah tried that (I thought the same)

So…

  if event.phase == “ended” and fudge == true then

        touch_joint:removeSelf()

still the object gets velocity. My assumption (so that means I am probably wrong) is that although I kill the joint the actual ‘kill’ doesn’t happen until a frame later and so the velocity exists at this point. I am of course probably wrong. I assume with most physics the ‘effect’ is 1 frame later and not instant at that point…

Any more suggestions welcome! :slight_smile:

Yes, this could be the issue, that it’s being delayed one frame. Try the “short timer” method and see if this fixes it. This shows how to associate the physics object with the timer and then know its object ID in the listener function… very convenient, since you don’t need to know the explicit reference name, and you can use this function for many things if needed.

[lua]

local function delayActions( event )

   local obj = event.source.objectID

   local vx, vy = obj:getLinearVelocity()

   obj:setLinearVelocity( 0, vy )

end

–at the end and inside your other function…

local dr = timer.performWithDelay( 20, delayActions ) ; dr.objectID = placing

[/lua]

Brent

Hey!

Now that DID work perfectly! Thank-you  :rolleyes:

Hi m.hula,

Can you show me the code where you’re “dropping” this image and trying to apply 0 to the linear X velocity? This should be working, so I’m curious why it’s not… a peek at the code may help solve this…

Thanks,

Brent

Hey there!

Here you go:

  if event.phase == “ended” and fudge == true then

        placing.lhTag = original_tag

        display.getCurrentStage():setFocus( nil )

        placing.isFocus = false

        placing.gravityScale = 1

        placing.isAwake = true

        placing.isSensor = false

        fudge = false

        placing.isBodyActive = false

The objects a dynamic moved via a touch joint. I *assume* it’s getting its velocity off the joint…

Cheers

Hi m.hula,

Yes, the dragged object is getting the residual force from the pull of the joint.

Try getting and then setting the linear velocity as follows, and put these lines before the “isBodyActive = false” line. I’m not sure that you should even keep that line, since you expressed that the body should begin to fall down, and making it inactive will obviously prevent that. :slight_smile:

[lua]

local vx, vy = placing:getLinearVelocity()

placing:setLinearVelocity( 0, vy )

[/lua]

The reason for the first line is that you need to get the Y linear velocity and then maintain it (apply it) in the new setting on the second line. This will keep the current Y velocity from before the drop, but stop the X entirely.

Of course, if you want to object to just stop dead in its tracks, set both values to 0 and omit the first line.

Brent

Hi Brent,

Yes I originally tried something like you suggested.

Here’s what I tried though:

  if event.phase == “ended” and fudge == true then

        placing.lhTag = original_tag

        display.getCurrentStage():setFocus( nil )

        placing.isFocus = false

        placing.gravityScale = 1

        placing.isAwake = true

        placing.isSensor = false

        fudge = false

        --placing.isBodyActive = false

        --placing.linearDamping = 10000000

        --placing.linearDamping = 0

        

– just messing around here trying to fix it!!

        placing.isBodyActive = true

        local vx, vy = placing:getLinearVelocity()

        placing:setLinearVelocity( 0, vy )

        placing.isBodyActive = false

        placing.isBodyActive = true

        

        --loader:cancelPreCollisionCallbackBetweenTags(placing.lhTag,“fruit”, pre_collision)

        --loader:cancelPreCollisionCallbackBetweenTags(placing.lhTag,“static”, pre_collision)

        loader:cancelBeginOrEndCollisionCallbackBetweenTags(placing.lhTag,“static”, beginend_collision)

        loader:cancelBeginOrEndCollisionCallbackBetweenTags(placing.lhTag,“fruit”, beginend_collision)

        remove_listener_place2()

        add_listener_touch()

        touch_joint:removeSelf()

Still has x velocity

Any ideas?

Cheers

Hi m.hula,

I think this may be simply the order in which you’re processing things. Try removing the joint before you get/set the linear velocity. I suspect that the joint is still exerting its pull on the object because you remove it at the end.

Brent

Hi Brent,

Yeah tried that (I thought the same)

So…

  if event.phase == “ended” and fudge == true then

        touch_joint:removeSelf()

still the object gets velocity. My assumption (so that means I am probably wrong) is that although I kill the joint the actual ‘kill’ doesn’t happen until a frame later and so the velocity exists at this point. I am of course probably wrong. I assume with most physics the ‘effect’ is 1 frame later and not instant at that point…

Any more suggestions welcome! :slight_smile:

Yes, this could be the issue, that it’s being delayed one frame. Try the “short timer” method and see if this fixes it. This shows how to associate the physics object with the timer and then know its object ID in the listener function… very convenient, since you don’t need to know the explicit reference name, and you can use this function for many things if needed.

[lua]

local function delayActions( event )

   local obj = event.source.objectID

   local vx, vy = obj:getLinearVelocity()

   obj:setLinearVelocity( 0, vy )

end

–at the end and inside your other function…

local dr = timer.performWithDelay( 20, delayActions ) ; dr.objectID = placing

[/lua]

Brent

Hey!

Now that DID work perfectly! Thank-you  :rolleyes: