Top-down car again. Box2D 2.1a several questions

Hi everyone,

I'm trying to build a top-down car simulator using Box2D 2.1a. And I've got several problems with that.
1) How can I make the car stop? I mean, when I used b2d 2.0.2, I applied force to the car's b2body and it accelerated, then linearDamping gradually stopped it after the force stopped applying. Now it seems to not work anymore.

here's the code I've got so far:
Code:
private function moveWorld(e:Event):void
      {
         _world.Step(1 / 24, 10, 10);
         // jpLooks is a Sprite representation of jpBody. I didn't want to use debug draw
         jpLooks.y = jpBody.GetPosition().y * Globals.RATIO;
         jpLooks.x = jpBody.GetPosition().x * Globals.RATIO;
         jpLooks.rotation = jpBody.GetAngle();
         
         if (upPressed) {
            jpBody.ApplyForce(new b2Vec2(0, -4), new b2Vec2(jpBody.GetPosition().x, jpBody.GetPosition().y));
         }
         if (rightPressed) {
            leftPressed = false;
            jpBody.SetAngle(jpBody.GetAngle() + 1);
         }
         if (leftPressed) {
            rightPressed = false;
            jpBody.SetAngle(jpBody.GetAngle() - 1);
         }
      }
      
      private function setUp():void
      {
         jpLooks = new Jeep();
         
         var gravity:b2Vec2 = new b2Vec2(0, 0);
         var doSleep:Boolean = true;
         _world = new b2World(gravity, doSleep);
         
         var jpBodyDef:b2BodyDef = new b2BodyDef();
         jpBodyDef.type = b2Body.b2_dynamicBody;
         jpBodyDef.position.Set(200 / Globals.RATIO, 450 / Globals.RATIO);

         
         jpBody = _world.CreateBody(jpBodyDef);
         
         var jf:b2FixtureDef = new b2FixtureDef();
         jf.density = 1.0;
         jf.shape = b2PolygonShape.AsBox(17.5 / Globals.RATIO, 35.5 / Globals.RATIO);
         var fixture:b2Fixture = jpBody.CreateFixture(jf);
         
         jpLooks.x = jpBody.GetPosition().x * Globals.RATIO;
         jpLooks.y = jpBody.GetPosition().y * Globals.RATIO;
         jpLooks.rotation = jpBody.GetAngle();
         stage.addChild(jpLooks);   
      }


2) The second problem is with this line:
Code:
jpBody.ApplyForce(new b2Vec2(0, -4), new b2Vec2(jpBody.GetPosition().x, jpBody.GetPosition().y));


The car rotates when I press left or right, but it keeps on going to the upper side of the screen. I understand why it happens, but how can I calculate this vector: b2Vec2(0, -4) regarding car's rotation not using my own trigonometric calculations? I'm sure there's a built-in method, but what?

Thanks.
Any ideas? 🙄
Your code example doesn't set linear damping, and Box2D defaults to 0.0. So I think that's the answer to your first problem.

As to the second, I can't think of any shortcuts better than just using trig on your car's angle.

In theory, if you can get a b2Vec2 already pointing in the right direction (such as multiple fixtures of the car), you can Normalize() that and multiply by the desired acceleration. But since that requires a sqrt(), plus whatever hassles are necessary to get the initial vector, it doesn't sound like a good tradeoff.

BTW, GetPosition() already returns a b2Vec2, so you don't need to create a new one in your ApplyForce. Just pass the result from GetPosition() directly. (Following code untested, but should get you on the right track if not outright 'work'. And you probably want to use a constant for that '* 4' acceleration magnitude).

Code:
          if (upPressed) {
            var force:b2Vec2 = new b2Vec2(Math.cos(jpLooks.rotation) * 4, Math.sin(jpLooks.rotation) * 4);
            jpBody.ApplyForce(force, jpBody.GetPosition());
         }
Quote:
Your code example doesn't set linear damping, and Box2D defaults to 0.0. So I think that's the answer to your first problem.

Unfortunately, not. I tried to set it but it doesn't stop the car, it only doesn't let it accelerate automatically when I release the acceleration key.
The problem's still pressing 🙂

Quote:
In theory, if you can get a b2Vec2 already pointing in the right direction (such as multiple fixtures of the car), you can Normalize() that and multiply by the desired acceleration. But since that requires a sqrt(), plus whatever hassles are necessary to get the initial vector, it doesn't sound like a good tradeoff.

BTW, GetPosition() already returns a b2Vec2, so you don't need to create a new one in your ApplyForce. Just pass the result from GetPosition() directly. (Following code untested, but should get you on the right track if not outright 'work'. And you probably want to use a constant for that '* 4' acceleration magnitude).
Quote:
if (upPressed) {
var force:b2Vec2 = new b2Vec2(Math.cos(jpLooks.rotation) * 4, Math.sin(jpLooks.rotation) * 4);
jpBody.ApplyForce(force, jpBody.GetPosition());
}

Thanks 🙂 . So I have to use trigonometry anyway?

p.s. .rotation gives angle in degrees 😉

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