have tried to adapt the second car tutorial at:
http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/
and everything works fine until the following line:
at which point the entire cart no longer rotates its angle to match the angle of the ground terrain (the cart moves up the hill, but the cart itself remains horizontal. ive tried just about every property in the docs, but nothing works.
anyone have any idea?
my code:
http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/
and everything works fine until the following line:
Code:
_spring1 = _world.CreateJoint(prismaticJointDef) as b2PrismaticJoint;
at which point the entire cart no longer rotates its angle to match the angle of the ground terrain (the cart moves up the hill, but the cart itself remains horizontal. ive tried just about every property in the docs, but nothing works.
anyone have any idea?
my code:
Code:
package
{
import flash.display.ShaderInput;
import flash.display.Sprite;
import flash.events.Event;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Joints.*;
import General.Input;
public class TruckGame extends Sprite
{
public const VELOCITY_ITERATIONS:int = 6;
public const POSITION_ITERATIONS:int = 2;
public const TIME_STEP:Number = 1.0 / 60.0;
public const SCREEN_WIDTH:int = 800;
public const SCREEN_HEIGHT:int = 500;
public const DRAW_SCALE:Number = 50;
private var _world:b2World;
private var _cart:b2Body;
private var _wheel1:b2Body;
private var _wheel2:b2Body;
private var _axle1:b2Body;
private var _axle2:b2Body;
private var _motor1:b2RevoluteJoint;
private var _motor2:b2RevoluteJoint;
private var _spring1:b2PrismaticJoint;
private var _spring2:b2PrismaticJoint;
private var _screen:Sprite;
private var _input:Input;
//not a part of the tutorial
private const PIXELS_TO_METRE:int = 30;
private const SWF_HALF_WIDTH:int = 400;
private const SWF_HEIGHT:int = 500;
private var circleDef;
private var circleFixtureDef:b2FixtureDef;
//_world var already present
protected var _groundBody:b2Body;
public function TruckGame():void
{
//create the drawing screen, can be used in conjunction with the camera pan tutorial
_screen = new Sprite();
_screen.scaleY = -1;
_screen.y = SCREEN_HEIGHT;
addChild(_screen);
//initialize input object
_input = new General.Input(this.stage);
//add main loop event listener
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
init();
}
private function init():void //b2Body
{
//create world
_world = new b2World(new b2Vec2(0, -10.0), false);
//create bodyDef
var groundBodyDef:b2BodyDef = new b2BodyDef();
groundBodyDef.position.Set(13.4, 0.5); // SWF_HALF_WIDTH / PIXELS_TO_METRE, SWF_HEIGHT / PIXELS_TO_METRE);
//create polygonShape as graphic for groundBody
var groundBox:b2PolygonShape = new b2PolygonShape();
groundBox.SetAsBox(50, 0.5); // SWF_HALF_WIDTH / PIXELS_TO_METRE, 20 / PIXELS_TO_METRE);
//create fixture and set physics properties
var groundFixtureDef:b2FixtureDef = new b2FixtureDef();
groundFixtureDef.shape = groundBox;
groundFixtureDef.density = 0;
groundFixtureDef.friction = 1;
//have _world create ground body
var groundBody:b2Body = _world.CreateBody(groundBodyDef);
groundBody.CreateFixture(groundFixtureDef);
//testing on additional road objects
groundBodyDef.position.Set(5, 0.8);
groundBox.SetAsOrientedBox(30, 0.3, new b2Vec2(0, 0), 10);
groundBody.CreateFixture(groundFixtureDef);
//debug drawing
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.SetSprite(_screen);
debugDraw.SetDrawScale(DRAW_SCALE);
debugDraw.SetLineThickness(1.0);
debugDraw.SetAlpha(1);
debugDraw.SetFillAlpha(0.4);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
_world.SetDebugDraw(debugDraw);
//
//groundBody.SetMassFromShapes();*/
//
addCart();
}
private function addCart():void
{
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.Set(5, 8); // 15, 3.5); //y coord space is reversed
_cart = _world.CreateBody(bodyDef); //_cart is the b2Body
//_cart.SetFixedRotation(true);
//SetAsBox args halfWidth, halfHeight
var boxDef:b2PolygonShape = new b2PolygonShape();
boxDef.SetAsBox(1.3, 0.3); // 100 / PIXELS_TO_METRE, 20 / PIXELS_TO_METRE);
var cartFixtureDef:b2FixtureDef = new b2FixtureDef();
cartFixtureDef.shape = boxDef;
cartFixtureDef.density = 2; //low number will let the cart elements wander apart, horror movie
cartFixtureDef.friction = 0.5;
cartFixtureDef.restitution = 0.2;
cartFixtureDef.filter.groupIndex = -1; //this is new to me
_cart.CreateFixture(cartFixtureDef);
//to adjust the tutorial arguments, move decimal place 2 places to the right
//b2PolygonShape is reusable, add additional cart instances to the b2Body
//fixtures are NOT reusable
boxDef.SetAsOrientedBox(0.4, 0.15, new b2Vec2( -1, -0.3), Math.PI / 3); // 40 / PIXELS_TO_METRE, 15 / PIXELS_TO_METRE, new b2Vec2( -1, -0.3), Math.PI / 3);
var cartFixtureDef2:b2FixtureDef = new b2FixtureDef();
cartFixtureDef2.shape = boxDef;
cartFixtureDef2.density = 1;
cartFixtureDef2.friction = 0.5;
cartFixtureDef2.restitution = 0.6;
cartFixtureDef2.filter.groupIndex = -1;
_cart.CreateFixture(cartFixtureDef2);
boxDef.SetAsOrientedBox(0.4, 0.15, new b2Vec2(1, -0.3), -Math.PI / 3); // 40 / PIXELS_TO_METRE, 15 / PIXELS_TO_METRE, new b2Vec2(1, -0.3), -Math.PI / 3);
var cartFixtureDef3:b2FixtureDef = new b2FixtureDef();
cartFixtureDef3.shape = boxDef;
cartFixtureDef3.density = 1;
cartFixtureDef3.friction = 0.5;
cartFixtureDef3.restitution = 0.6;
cartFixtureDef3.filter.groupIndex = -1;
_cart.CreateFixture(cartFixtureDef3);
//_cart.ResetMassData(); //TODO:this might work, but should not be set to fixtures, rather to shapes
/////////////////////////////////////////////
//add the axles
_axle1 = _world.CreateBody(bodyDef); //reusing bodyDef
boxDef.SetAsOrientedBox(0.4, 0.1, new b2Vec2( -1 - 0.6 * Math.cos(Math.PI / 3), -0.3 - 0.6 * Math.sin(Math.PI / 3)), Math.PI / 3); // 40 / PIXELS_TO_METRE, 10 / PIXELS_TO_METRE, new b2Vec2( -1 - 0.6 * Math.cos(Math.PI / 3), -0.3 - 0.6 * Math.sin(Math.PI / 3)), Math.PI / 3);
var cartAxleDef:b2FixtureDef = new b2FixtureDef();
cartAxleDef.shape = boxDef;
//_axle1.CreateFixture(cartAxleDef);
_cart.CreateFixture(cartAxleDef);
_axle1.ResetMassData(); //should be SetMassFromShapes
var prismaticJointDef:b2PrismaticJointDef = new b2PrismaticJointDef();
prismaticJointDef.Initialize(_cart, _axle1, _cart.GetWorldCenter(), new b2Vec2(Math.cos(Math.PI / 3), Math.sin(Math.PI / 3 )));
prismaticJointDef.lowerTranslation = -0.3;
prismaticJointDef.upperTranslation = 0.5;
prismaticJointDef.enableLimit = true;
prismaticJointDef.enableMotor = true;
_spring1 = _world.CreateJoint(prismaticJointDef) as b2PrismaticJoint;
_axle2 = _world.CreateBody(bodyDef); //reusing bodyDef
boxDef.SetAsOrientedBox(0.4, 0.1, new b2Vec2(1 + 0.6 * Math.cos( -Math.PI / 3), -0.3 + 0.6 * Math.sin( -Math.PI / 3)), -Math.PI / 3); // 40 / PIXELS_TO_METRE, 10 / PIXELS_TO_METRE, new b2Vec2(1 + 0.6 * Math.cos( -Math.PI / 3), -0.3 + 0.6 * Math.sin( -Math.PI / 3)), -Math.PI / 3);
var cartAxleDef2:b2FixtureDef = new b2FixtureDef();
cartAxleDef2.shape = boxDef;
//_axle2.CreateFixture(cartAxleDef2);
_cart.CreateFixture(cartAxleDef2);
_axle2.ResetMassData();
prismaticJointDef.Initialize(_cart, _axle2, _cart.GetWorldCenter(), new b2Vec2( -Math.cos(Math.PI / 3), Math.sin(Math.PI / 3)));
_spring2 = _world.CreateJoint(prismaticJointDef) as b2PrismaticJoint;
//addWheels
//setup circleDef for objects and cart wheels
circleDef = new b2CircleShape();
circleDef.SetRadius(0.8);
circleFixtureDef = new b2FixtureDef();
circleFixtureDef.shape = circleDef;
circleFixtureDef.density = 0.7;
circleFixtureDef.friction = 0.8; //probably sets the traction
circleFixtureDef.restitution = 0.6;
circleFixtureDef.filter.groupIndex = -1; //TODO:check out the groupIndex
//for (var i:uint = 0; i < 2; i++) {
//bodyDef = new b2BodyDef();
//bodyDef.type = b2Body.b2_dynamicBody;
//bodyDef.position.Set(5, 8);
//bodyDef.allowSleep = false;
//
//var revoluteJointDef:b2RevoluteJointDef = new b2RevoluteJointDef();
//revoluteJointDef.enableMotor = true;
//
//if (i == 0) {
//wheel placement is handled here
//bodyDef.position.Set(_axle1.GetWorldCenter().x - 0.3 * Math.cos(Math.PI / 3) - 1.4, _axle1.GetWorldCenter().y - 0.3 * Math.sin(Math.PI / 3) - 0.9);
//_wheel1 = _world.CreateBody(bodyDef);
//_wheel1.CreateFixture(circleFixtureDef);
//_wheel1.ResetMassData();
//
//revoluteJointDef.Initialize(_axle1, _wheel1, _wheel1.GetWorldCenter());
//_motor1 = _world.CreateJoint(revoluteJointDef) as b2RevoluteJoint;
//}
//if ( i == 1) {
//bodyDef.position.Set(_axle2.GetWorldCenter().x + 0.3 * Math.cos( -Math.PI / 3) + 1.4, _axle2.GetWorldCenter().y + 0.3 * Math.sin( -Math.PI / 3) - 0.9);
//_wheel2 = _world.CreateBody(bodyDef);
//_wheel2.CreateFixture(circleFixtureDef);
//_wheel2.ResetMassData();
//
//revoluteJointDef.Initialize(_axle2, _wheel2, _wheel2.GetWorldCenter());
//_motor2 = _world.CreateJoint(revoluteJointDef) as b2RevoluteJoint;
//}
//}
}
private function update(e:Event):void
{
if (Input.isKeyPressed(32)) {
_world = null;
init();
return;
}
_motor1.SetMotorSpeed(30 * Math.PI * (Input.isKeyPressed(40) ? 1 : Input.isKeyPressed(38) ? -1 : 0));
_motor1.SetMaxMotorTorque(Input.isKeyPressed(40) || Input.isKeyPressed (38) ? 17 : 0.5);
_motor2.SetMotorSpeed(15 * Math.PI * (Input.isKeyPressed(40) ? 1 : Input.isKeyPressed(38) ? -1 : 0));
_motor2.SetMaxMotorTorque(Input.isKeyPressed(40) || Input.isKeyPressed(38) ? 12 : 0.5);
_spring1.SetMaxMotorForce(30 + Math.abs(800 * Math.pow(_spring1.GetJointTranslation(), 2)));
_spring1.SetMotorSpeed((_spring1.GetMotorSpeed() - 10 * _spring1.GetJointTranslation()) * 0.4);
_spring2.SetMaxMotorForce(20 + Math.abs(800 * Math.pow(_spring2.GetJointTranslation(), 2)));
_spring2.SetMotorSpeed( -4 * Math.pow(_spring2.GetJointTranslation(), 1));
_cart.ApplyTorque(30 * (Input.isKeyPressed(37) ? 1 : Input.isKeyPressed(39) ? -1 : 0));
//do camera panning
_screen.x -= (_screen.x - (-DRAW_SCALE * _cart.GetWorldCenter().x + SCREEN_WIDTH / 2 - _cart.GetLinearVelocity().x * 10)) / 3;
_screen.y -= (_screen.y - (DRAW_SCALE * _cart.GetWorldCenter().y + 2 * SCREEN_HEIGHT / 3 + _cart.GetLinearVelocity().y * 6)) / 3;
var timeStep:Number = 1.0 / 60.0;
var velocityIterations:int = 6;
var positionIterations:int = 2;
_world.Step(timeStep, velocityIterations, positionIterations);
_world.ClearForces();
_world.DrawDebugData();
//Input.update();
}
}
}
{
import flash.display.ShaderInput;
import flash.display.Sprite;
import flash.events.Event;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Joints.*;
import General.Input;
public class TruckGame extends Sprite
{
public const VELOCITY_ITERATIONS:int = 6;
public const POSITION_ITERATIONS:int = 2;
public const TIME_STEP:Number = 1.0 / 60.0;
public const SCREEN_WIDTH:int = 800;
public const SCREEN_HEIGHT:int = 500;
public const DRAW_SCALE:Number = 50;
private var _world:b2World;
private var _cart:b2Body;
private var _wheel1:b2Body;
private var _wheel2:b2Body;
private var _axle1:b2Body;
private var _axle2:b2Body;
private var _motor1:b2RevoluteJoint;
private var _motor2:b2RevoluteJoint;
private var _spring1:b2PrismaticJoint;
private var _spring2:b2PrismaticJoint;
private var _screen:Sprite;
private var _input:Input;
//not a part of the tutorial
private const PIXELS_TO_METRE:int = 30;
private const SWF_HALF_WIDTH:int = 400;
private const SWF_HEIGHT:int = 500;
private var circleDef;
private var circleFixtureDef:b2FixtureDef;
//_world var already present
protected var _groundBody:b2Body;
public function TruckGame():void
{
//create the drawing screen, can be used in conjunction with the camera pan tutorial
_screen = new Sprite();
_screen.scaleY = -1;
_screen.y = SCREEN_HEIGHT;
addChild(_screen);
//initialize input object
_input = new General.Input(this.stage);
//add main loop event listener
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
init();
}
private function init():void //b2Body
{
//create world
_world = new b2World(new b2Vec2(0, -10.0), false);
//create bodyDef
var groundBodyDef:b2BodyDef = new b2BodyDef();
groundBodyDef.position.Set(13.4, 0.5); // SWF_HALF_WIDTH / PIXELS_TO_METRE, SWF_HEIGHT / PIXELS_TO_METRE);
//create polygonShape as graphic for groundBody
var groundBox:b2PolygonShape = new b2PolygonShape();
groundBox.SetAsBox(50, 0.5); // SWF_HALF_WIDTH / PIXELS_TO_METRE, 20 / PIXELS_TO_METRE);
//create fixture and set physics properties
var groundFixtureDef:b2FixtureDef = new b2FixtureDef();
groundFixtureDef.shape = groundBox;
groundFixtureDef.density = 0;
groundFixtureDef.friction = 1;
//have _world create ground body
var groundBody:b2Body = _world.CreateBody(groundBodyDef);
groundBody.CreateFixture(groundFixtureDef);
//testing on additional road objects
groundBodyDef.position.Set(5, 0.8);
groundBox.SetAsOrientedBox(30, 0.3, new b2Vec2(0, 0), 10);
groundBody.CreateFixture(groundFixtureDef);
//debug drawing
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.SetSprite(_screen);
debugDraw.SetDrawScale(DRAW_SCALE);
debugDraw.SetLineThickness(1.0);
debugDraw.SetAlpha(1);
debugDraw.SetFillAlpha(0.4);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
_world.SetDebugDraw(debugDraw);
//
//groundBody.SetMassFromShapes();*/
//
addCart();
}
private function addCart():void
{
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.Set(5, 8); // 15, 3.5); //y coord space is reversed
_cart = _world.CreateBody(bodyDef); //_cart is the b2Body
//_cart.SetFixedRotation(true);
//SetAsBox args halfWidth, halfHeight
var boxDef:b2PolygonShape = new b2PolygonShape();
boxDef.SetAsBox(1.3, 0.3); // 100 / PIXELS_TO_METRE, 20 / PIXELS_TO_METRE);
var cartFixtureDef:b2FixtureDef = new b2FixtureDef();
cartFixtureDef.shape = boxDef;
cartFixtureDef.density = 2; //low number will let the cart elements wander apart, horror movie
cartFixtureDef.friction = 0.5;
cartFixtureDef.restitution = 0.2;
cartFixtureDef.filter.groupIndex = -1; //this is new to me
_cart.CreateFixture(cartFixtureDef);
//to adjust the tutorial arguments, move decimal place 2 places to the right
//b2PolygonShape is reusable, add additional cart instances to the b2Body
//fixtures are NOT reusable
boxDef.SetAsOrientedBox(0.4, 0.15, new b2Vec2( -1, -0.3), Math.PI / 3); // 40 / PIXELS_TO_METRE, 15 / PIXELS_TO_METRE, new b2Vec2( -1, -0.3), Math.PI / 3);
var cartFixtureDef2:b2FixtureDef = new b2FixtureDef();
cartFixtureDef2.shape = boxDef;
cartFixtureDef2.density = 1;
cartFixtureDef2.friction = 0.5;
cartFixtureDef2.restitution = 0.6;
cartFixtureDef2.filter.groupIndex = -1;
_cart.CreateFixture(cartFixtureDef2);
boxDef.SetAsOrientedBox(0.4, 0.15, new b2Vec2(1, -0.3), -Math.PI / 3); // 40 / PIXELS_TO_METRE, 15 / PIXELS_TO_METRE, new b2Vec2(1, -0.3), -Math.PI / 3);
var cartFixtureDef3:b2FixtureDef = new b2FixtureDef();
cartFixtureDef3.shape = boxDef;
cartFixtureDef3.density = 1;
cartFixtureDef3.friction = 0.5;
cartFixtureDef3.restitution = 0.6;
cartFixtureDef3.filter.groupIndex = -1;
_cart.CreateFixture(cartFixtureDef3);
//_cart.ResetMassData(); //TODO:this might work, but should not be set to fixtures, rather to shapes
/////////////////////////////////////////////
//add the axles
_axle1 = _world.CreateBody(bodyDef); //reusing bodyDef
boxDef.SetAsOrientedBox(0.4, 0.1, new b2Vec2( -1 - 0.6 * Math.cos(Math.PI / 3), -0.3 - 0.6 * Math.sin(Math.PI / 3)), Math.PI / 3); // 40 / PIXELS_TO_METRE, 10 / PIXELS_TO_METRE, new b2Vec2( -1 - 0.6 * Math.cos(Math.PI / 3), -0.3 - 0.6 * Math.sin(Math.PI / 3)), Math.PI / 3);
var cartAxleDef:b2FixtureDef = new b2FixtureDef();
cartAxleDef.shape = boxDef;
//_axle1.CreateFixture(cartAxleDef);
_cart.CreateFixture(cartAxleDef);
_axle1.ResetMassData(); //should be SetMassFromShapes
var prismaticJointDef:b2PrismaticJointDef = new b2PrismaticJointDef();
prismaticJointDef.Initialize(_cart, _axle1, _cart.GetWorldCenter(), new b2Vec2(Math.cos(Math.PI / 3), Math.sin(Math.PI / 3 )));
prismaticJointDef.lowerTranslation = -0.3;
prismaticJointDef.upperTranslation = 0.5;
prismaticJointDef.enableLimit = true;
prismaticJointDef.enableMotor = true;
_spring1 = _world.CreateJoint(prismaticJointDef) as b2PrismaticJoint;
_axle2 = _world.CreateBody(bodyDef); //reusing bodyDef
boxDef.SetAsOrientedBox(0.4, 0.1, new b2Vec2(1 + 0.6 * Math.cos( -Math.PI / 3), -0.3 + 0.6 * Math.sin( -Math.PI / 3)), -Math.PI / 3); // 40 / PIXELS_TO_METRE, 10 / PIXELS_TO_METRE, new b2Vec2(1 + 0.6 * Math.cos( -Math.PI / 3), -0.3 + 0.6 * Math.sin( -Math.PI / 3)), -Math.PI / 3);
var cartAxleDef2:b2FixtureDef = new b2FixtureDef();
cartAxleDef2.shape = boxDef;
//_axle2.CreateFixture(cartAxleDef2);
_cart.CreateFixture(cartAxleDef2);
_axle2.ResetMassData();
prismaticJointDef.Initialize(_cart, _axle2, _cart.GetWorldCenter(), new b2Vec2( -Math.cos(Math.PI / 3), Math.sin(Math.PI / 3)));
_spring2 = _world.CreateJoint(prismaticJointDef) as b2PrismaticJoint;
//addWheels
//setup circleDef for objects and cart wheels
circleDef = new b2CircleShape();
circleDef.SetRadius(0.8);
circleFixtureDef = new b2FixtureDef();
circleFixtureDef.shape = circleDef;
circleFixtureDef.density = 0.7;
circleFixtureDef.friction = 0.8; //probably sets the traction
circleFixtureDef.restitution = 0.6;
circleFixtureDef.filter.groupIndex = -1; //TODO:check out the groupIndex
//for (var i:uint = 0; i < 2; i++) {
//bodyDef = new b2BodyDef();
//bodyDef.type = b2Body.b2_dynamicBody;
//bodyDef.position.Set(5, 8);
//bodyDef.allowSleep = false;
//
//var revoluteJointDef:b2RevoluteJointDef = new b2RevoluteJointDef();
//revoluteJointDef.enableMotor = true;
//
//if (i == 0) {
//wheel placement is handled here
//bodyDef.position.Set(_axle1.GetWorldCenter().x - 0.3 * Math.cos(Math.PI / 3) - 1.4, _axle1.GetWorldCenter().y - 0.3 * Math.sin(Math.PI / 3) - 0.9);
//_wheel1 = _world.CreateBody(bodyDef);
//_wheel1.CreateFixture(circleFixtureDef);
//_wheel1.ResetMassData();
//
//revoluteJointDef.Initialize(_axle1, _wheel1, _wheel1.GetWorldCenter());
//_motor1 = _world.CreateJoint(revoluteJointDef) as b2RevoluteJoint;
//}
//if ( i == 1) {
//bodyDef.position.Set(_axle2.GetWorldCenter().x + 0.3 * Math.cos( -Math.PI / 3) + 1.4, _axle2.GetWorldCenter().y + 0.3 * Math.sin( -Math.PI / 3) - 0.9);
//_wheel2 = _world.CreateBody(bodyDef);
//_wheel2.CreateFixture(circleFixtureDef);
//_wheel2.ResetMassData();
//
//revoluteJointDef.Initialize(_axle2, _wheel2, _wheel2.GetWorldCenter());
//_motor2 = _world.CreateJoint(revoluteJointDef) as b2RevoluteJoint;
//}
//}
}
private function update(e:Event):void
{
if (Input.isKeyPressed(32)) {
_world = null;
init();
return;
}
_motor1.SetMotorSpeed(30 * Math.PI * (Input.isKeyPressed(40) ? 1 : Input.isKeyPressed(38) ? -1 : 0));
_motor1.SetMaxMotorTorque(Input.isKeyPressed(40) || Input.isKeyPressed (38) ? 17 : 0.5);
_motor2.SetMotorSpeed(15 * Math.PI * (Input.isKeyPressed(40) ? 1 : Input.isKeyPressed(38) ? -1 : 0));
_motor2.SetMaxMotorTorque(Input.isKeyPressed(40) || Input.isKeyPressed(38) ? 12 : 0.5);
_spring1.SetMaxMotorForce(30 + Math.abs(800 * Math.pow(_spring1.GetJointTranslation(), 2)));
_spring1.SetMotorSpeed((_spring1.GetMotorSpeed() - 10 * _spring1.GetJointTranslation()) * 0.4);
_spring2.SetMaxMotorForce(20 + Math.abs(800 * Math.pow(_spring2.GetJointTranslation(), 2)));
_spring2.SetMotorSpeed( -4 * Math.pow(_spring2.GetJointTranslation(), 1));
_cart.ApplyTorque(30 * (Input.isKeyPressed(37) ? 1 : Input.isKeyPressed(39) ? -1 : 0));
//do camera panning
_screen.x -= (_screen.x - (-DRAW_SCALE * _cart.GetWorldCenter().x + SCREEN_WIDTH / 2 - _cart.GetLinearVelocity().x * 10)) / 3;
_screen.y -= (_screen.y - (DRAW_SCALE * _cart.GetWorldCenter().y + 2 * SCREEN_HEIGHT / 3 + _cart.GetLinearVelocity().y * 6)) / 3;
var timeStep:Number = 1.0 / 60.0;
var velocityIterations:int = 6;
var positionIterations:int = 2;
_world.Step(timeStep, velocityIterations, positionIterations);
_world.ClearForces();
_world.DrawDebugData();
//Input.update();
}
}
}