4
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ cocos2d-iphone.org/forum/topic/28924 Perhaps this can help? \$\endgroup\$
    – Sidar
    Sep 5 '12 at 6:37
  • \$\begingroup\$ Inertia makes it harder to change velocity. It will not slow the player down which I believe is what the OP is asking for. \$\endgroup\$ Sep 5 '12 at 6:56
  • \$\begingroup\$ Yes i am trying ballBody->SetLinearDamping(1.2f); but can not see any effect. \$\endgroup\$
    – Diken
    Sep 5 '12 at 7:06
  • \$\begingroup\$ 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 \$\endgroup\$ Sep 5 '12 at 7:14
5
\$\begingroup\$

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);
\$\endgroup\$
5
  • \$\begingroup\$ How to add friction joint and which anchor point i have to set to zero?? \$\endgroup\$
    – Diken
    Sep 5 '12 at 7:05
  • \$\begingroup\$ @Diken updated answer \$\endgroup\$ Sep 5 '12 at 7:12
  • \$\begingroup\$ This code is apply in update method of HelloWorldLayer.mm?? I show you my code of jump and movement of player \$\endgroup\$
    – Diken
    Sep 5 '12 at 7:16
  • \$\begingroup\$ No just once after you create the two necessary bodies. \$\endgroup\$ Sep 5 '12 at 7:20
  • \$\begingroup\$ But player is a dynamic body and which is define in your code as yourBall but what about bodyB?? \$\endgroup\$
    – Diken
    Sep 5 '12 at 7:25
3
\$\begingroup\$

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)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.