4

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.

Thanks in advance


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??

Tetrad
  • 30,124
  • 12
  • 94
  • 143
Diken
  • 85
  • 1
  • 4
  • http://www.cocos2d-iphone.org/forum/topic/28924 Perhaps this can help? – Sidar Sep 05 '12 at 06: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. – ClassicThunder Sep 05 '12 at 06:56
  • Yes i am trying ballBody->SetLinearDamping(1.2f); but can not see any effect. – Diken Sep 05 '12 at 07: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 http://www.box2d.org/manual.html – ClassicThunder Sep 05 '12 at 07:14

2 Answers2

5

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);
ClassicThunder
  • 8,383
  • 35
  • 49
3

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)

charlie
  • 103
  • 2
milkplus
  • 308
  • 3
  • 6