# Can i add friction in air?

I have issue regarding speed in air. When i jump and move simultaneously that time speed of player increase.For jump i am using impuls and for movement i am using force.I want to slow speed when player is in air.

Following is my update method ih HUDLayer

-(void)update:(ccTime)dt :(b2Body *)ballBody :(CCSprite *)player1 :(b2World *)world
{

if (moveRight.active==YES)
{
ballBody->SetActive(true);
b2Vec2 locationworld=b2Vec2(maxSpeed,0);
double mass=ballBody->GetMass();
ballBody->ApplyForce(mass*locationworld, ballBody->GetWorldCenter());
//  ballBody->SetLinearDamping(1.2f);
}
else if(moveLeft.active==YES)
{
ballBody->SetActive(true);
b2Vec2 locationworld=b2Vec2(-10,0);
double mass=ballBody->GetMass();
ballBody->ApplyForce(mass*locationworld, ballBody->GetWorldCenter());
//    ballBody->SetLinearDamping(1.2f);

}


} Following is jump

-(void)jump:(b2Body*)ballBody:(ccTime)dt:(BOOL)touch
{
if (touch)
{

if (jumpSprte.active==YES)
{
ballBody->SetActive(true);
b2Vec2 locationWorld;
//locationWorld=b2Vec2(0.0f,98.0f);
locationWorld=b2Vec2(0,32);
//        double mass=ballBody->GetMass();
ballBody->ApplyLinearImpulse(locationWorld, ballBody->GetWorldCenter());
//  ballBody->ApplyForce(mass*locationWorld, ballBody->GetWorldCenter());
ballBody->SetLinearDamping(1.2f);

}
}
}


So where i apply logic??

• cocos2d-iphone.org/forum/topic/28924 Perhaps this can help? Sep 5, 2012 at 6:37
• Inertia makes it harder to change velocity. It will not slow the player down which I believe is what the OP is asking for. Sep 5, 2012 at 6:56
• Yes i am trying ballBody->SetLinearDamping(1.2f); but can not see any effect. Sep 5, 2012 at 7:06
• Well that is a ridiculously huge value for linear damping. "Normally you will use a damping value between 0 and 0.1." You should keep this handy box2d.org/manual.html Sep 5, 2012 at 7:14

Sounds like you want a friction joint. Set the two anchor points to zero so the friction doesn't have any direction. Below is vague pseudocode of how to create a friction joint and add it to your world.

Also make sure you keep the Box2D Manual handy.

var frictionJoint = new FrictionJoint();

frictionJoint.localAnchorA = new Vector2(0,0);
frictionJoint.localAnchorB = new Vector2(0,0);

frictionJoint.bodyA = yourBall;
frictionJoint.bodyB = ASquareAsLargeAsTheAreaYouNeedFrictionIn

frictionJoint.maxForce = 0.5; //This the most force the joint will apply to your object. The faster its moving the more force applied
frictionJoint.maxTorque = 5; //Set to 0 to prevent rotation

world.CreateJoint(frictionJoint);

• How to add friction joint and which anchor point i have to set to zero?? Sep 5, 2012 at 7:05
• @Diken updated answer Sep 5, 2012 at 7:12
• This code is apply in update method of HelloWorldLayer.mm?? I show you my code of jump and movement of player Sep 5, 2012 at 7:16
• No just once after you create the two necessary bodies. Sep 5, 2012 at 7:20
• But player is a dynamic body and which is define in your code as yourBall but what about bodyB?? Sep 5, 2012 at 7:25

Unlike surface friction between solids, air friction (drag) depends on the speed of the object, squared.

An easy way to model friction and drag

F(friction) = -k;
F(drag) = speed * speed * -k;


Both forces act in the opposite direction to the velocity of the object.

Notice that surface friction is a constant whereas air friction (drag) must be modified each frame according to the object speed (length of velocity vector)