Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Assume that there is a circle and it can be moved by using keyboard arrows.Is required that increasing speed over time like increasing car speed. For example; max speed is 25 and time to reach max speed shall be 5 sec. Over 5 sec the speed will reach to max speed. Does Box2d handle that situation?. I tried setting linear valocity but it seems to make the circle have constant speed instead of increased speed over time.

Thank You!

Note: I'm using Box2DWeb Javascript port of Box2D.

share|improve this question
    
Increasing the speed over time is known as acceleration. Box2D can handle acceleration. –  Byte56 Jul 2 '13 at 17:21
    
Box2d is best when want to run simulations. The more tightly you want to control things the worse it is. At your point I would just do the math yourself. Not to mention Box2D is kinda built around forces and there is nothing linear about how forces effect thigns. –  ClassicThunder Jul 2 '13 at 17:54

2 Answers 2

up vote 0 down vote accepted

With Box2D you should just have to apply an Impulse to your object, instead of setting the velocity manually.

ApplyImpulse(direction,point)  //Direction = vector

For the point, you can always use the center of your Body object by using:

Body.GetWorldCenter()

By applying small impulses, you will see your object accelerate. The speed of acceleration will depend on a bunch of variables, such as the force of your impulse, the weight of your object, friction, etc...

share|improve this answer

You'll have to time the amount of acceleration per frame yourself. There is no built-in function to increase velocity over time to a cap as you described. Extending Jon's answer, you probably end with up some code like this (adapted from http://www.iforce2d.net/b2dtut/constant-speed check it out the page, its very similar to what you want to to do):

timer = 5.0f; // 5 seconds    
   update(float delta){ 
        timer -= delta; //timer keeps track of time left
        b2Vec2 vel = body->GetLinearVelocity();
        float desiredVel;
        if(timer > 0)
            desiredVel = MIN_VEL + * acceleration * delta; //m/s
        float velChange = desiredVel - vel.x;
        float impulse = body->GetMass() * velChange; //disregard time factor
        body->ApplyLinearImpulse( b2Vec2(impulse,0), body->GetWorldCenter() );
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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