# (quick)Box2D top down game

Hello,

I’ve been messing around with quickBox2D for a while, and first of all I have to say that it’s a great engine, but so majorly different from as3 that it’s hard to apply certain methods.

I’m currently trying to make a top down car game, where you control the car with rotational movement, it moves in the direction of it’s current angle.
I could manage this in pure actionscript even though I find it hard to understand (not a genius in maths). But box2D is just so different that I find it impossible to figure out. It would be greatly appreciated if people could give me some pointers on this.

Secondly, since this features a top down view, there is no gravity, therefor the friction does not apply and objects keep moving at their given speed. Any simple workaround for this? Seems like adding every item on stage to an array and applying friction is quite a hassle.

Already found this tutorial http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/ , but it is way too fancy and complicated.

This is exactly what I’m after:
http://60-242-116-83.tpgi.com.au/joomla/projects/flash/46-car-physics-demo-with-box2d.html

current source code:

``````
import com.actionsnippet.qbox.*;
import Box2D.Common.Math.*;
import Box2D.Collision.Shapes.*;
[SWF(width = 800, height = 600, backgroundColor = 0x000000)]

var sim:QuickBox2D = new QuickBox2D(this);
sim.gravity.y = 0;
sim.gravity.x = 0;
sim.createStageWalls();

var char:QuickObject = sim.addBox({x:2, y:18, width:1, height:2, allowSleep:false, groupIndex:-1, fillColor:0x0099CC, density: 2, friction: 1});
var box:QuickObject = sim.addBox({x:10, y:18, width:2, height:2, allowSleep:true, fillColor:0x003300, friction: 10, density:20});
var staticBox:QuickObject = sim.addBox({x:20, y:18, width:3, height:3, allowSleep:false, isSleeping:true, fillColor:0xFF0000, density:0, friction: 1});

sim.start();

var charVel:b2Vec2;
var charVelAng:Number;

function onLoop(evt:Event):void {
charVel = char.body.GetLinearVelocity();
charVelAng =  char.body.GetAngularVelocity();
var filter:b2FilterData;
if (key[Keyboard.RIGHT]){
charVel.x += 1
charVelAng += 0.1;
//char.body.SetLinearVelocity(charVel);
charVelAng += 0.1;
//char.body.SetAngularVelocity(charVelAng);
}

if (key[Keyboard.LEFT]){
charVel.x -=1;
trace(char.angle);
charVelAng -= 0.1;
//char.body.SetAngularVelocity(charVelAng);
}
if (key[Keyboard.UP]){ //&& sim.w.GetContactCount()> 0){
charVel.y += -1;
charVelAng *= 0.8;
}

if (key[Keyboard.DOWN]){ //&& sim.w.GetContactCount()> 0){
charVel.y -= -1;
charVelAng *= 0.8;
}

char.body.SetLinearVelocity(charVel);
char.body.SetAngularVelocity(charVelAng);

}

var key:Object = new Object();

function onKeyPressed(evt:KeyboardEvent):void{

key[evt.keyCode] = true;
}
function onKeyReleased(evt:KeyboardEvent):void{

key[evt.keyCode] = false;

}

``````

and these are the original controls I’m trying to convert:

``````
car.x += Math.sin (car.rotation * Math.PI / 180) * speed;
car.y += Math.cos (car.rotation * Math.PI / 180) * -speed;

``````