Slow down physics object

Hi guys,

I have a player (i.e. dynamic physics object) moving at constant speed via:

self:setLinearVelocity(vx, 0)

I want to slow it down when hit, like when you shut down car engine during driving (i.e. I do not want it to stop momentarily).

I tried using self.linearDamping, no good.

So I tried something like this:

local vx, vy  = self:getLinearVelocity() self:setLinearVelocity(0, 0) timer.performWithDelay ( 1, function() self:applyLinearImpulse(vx, 0) end,  1 )

No good, because player accelerates too quickly.

Any good ideas?  :smiley:

Thank you.

Ivan

** UPDATE: I didn’t add the limit code on the first posting **

@ivan,

Honestly, a high linearDamping coupled with a transition sounds like the right solution:

obj.linearDamping = 5 transition.to( obj, { linearDamping = 0.75, time = 250 } ) -- Changed to match code below

You can even play with easings to modify the rate of change.

Oh, and I wouldn’t use setLinearVelocity for movement of a car or vehicle.

I’d do something like this:

obj.maxVX = 100 -- More complicated for omni-directional speed limit obj.forceX = 2 obj.linearDamping = 0.75 -- Adjust to suit needs function obj.enterFrame( self ) self:applyForce( self.forceX \* self.mass, 0, self.x, self.y ) -- was missing in first posting ==\> local vx,vy = self:getLinearVelocity() if( vx \> self.maxVX ) then self:setLinearVeloicty( self.maxVX, vy ) end end Runtime:addEventListener( "enterFrame", obj ) -- Now you can adjust forceX any time to change 'acceleartion'

Hi @ivan888,

I can’t think of a logical reason why setting the linear damping wouldn’t work to slow the object down… that’s exactly what linear damping is meant for. Unless, I suppose, you are “overriding” it by immediately or constantly setting back its linear velocity, or applying a bunch of new force to it.

Best regards,

Brent

Hi roaminggamer,

Thanks a lot!!  :smiley:

Your code works like a charm!

@Brent: I messed up with the forces, linearDamping works great!

Thank you guys!  :slight_smile:

** UPDATE: I didn’t add the limit code on the first posting **

@ivan,

Honestly, a high linearDamping coupled with a transition sounds like the right solution:

obj.linearDamping = 5 transition.to( obj, { linearDamping = 0.75, time = 250 } ) -- Changed to match code below

You can even play with easings to modify the rate of change.

Oh, and I wouldn’t use setLinearVelocity for movement of a car or vehicle.

I’d do something like this:

obj.maxVX = 100 -- More complicated for omni-directional speed limit obj.forceX = 2 obj.linearDamping = 0.75 -- Adjust to suit needs function obj.enterFrame( self ) self:applyForce( self.forceX \* self.mass, 0, self.x, self.y ) -- was missing in first posting ==\> local vx,vy = self:getLinearVelocity() if( vx \> self.maxVX ) then self:setLinearVeloicty( self.maxVX, vy ) end end Runtime:addEventListener( "enterFrame", obj ) -- Now you can adjust forceX any time to change 'acceleartion'

Hi @ivan888,

I can’t think of a logical reason why setting the linear damping wouldn’t work to slow the object down… that’s exactly what linear damping is meant for. Unless, I suppose, you are “overriding” it by immediately or constantly setting back its linear velocity, or applying a bunch of new force to it.

Best regards,

Brent

Hi roaminggamer,

Thanks a lot!!  :smiley:

Your code works like a charm!

@Brent: I messed up with the forces, linearDamping works great!

Thank you guys!  :slight_smile: