One Way Damping

Hi,

I want to apply linear damping only in one way. (x or y)

Any way to do this?
seems like you would just write your own function to happen on enter frame

*sudo-code, not exact*
Code:
var dampingPercent = .2;

addEventListener(Event.ENTER_FRAME, EnterFrame);
function EnterFrame(e:Event=null):void{
   rightDamping(heroBody);
}

function rightDamping(thisBody:b2Body){
  var velocity:b2Vec2 = thisBody.GetLinearVelocity();
  if(velocity.x > 0){
     velocity.x *= 1-dampingPercent;
     thisBody.SetLinearVelocity(velocity);  //this isn't actually necessary read below;
  }
}
//velocity is a b2Vec2
//b2Vec2's (like many other variable types) use pointers to access the variable
//thus changing velocity.x in this function, has already applied the change to the variable that the body is referencing.

//if you were to ever need to change this, i recommend thisBody.GetLinearVelocity().copy();  that will give you a separate copy of the variable.
//many Box2D datatypes have the copy function.  however ActionScript datatypes(such as Array and XML) are not so fortunate.  there are still ways to copy them by creating new instances and passing values, or using byte data.


anyway, goodluck.
Thanks for the answer&tips, it works.
It also depends on what you are trying to simulate. If you want a linear damping then the above suggestion will work. If you want something that approximates friction though( like air resistance or water or whatever you can think of ) then you'll want a damping that scales with the velocity...

http://en.wikipedia.org/wiki/Air_resistance

You can simplify this equation by precalculating A( the area ) * C * P. Then all you have to do to calculate the opposing force is multiple the precalculated value by -1/2 the veloctiy squared.
Great function, logic is quite simple. One concern I have is if the user calls this in their update function with different frame rates. I think this would mean the function would be called more or fewer times, and the decay would be longer/shorter.

Original thread: https://box2d.org/forum/viewtopic.php?t=3963