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:
2) The second problem is with this line:
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.
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);
}
{
_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.