Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Box2D for Flash Games

You're reading from  Box2D for Flash Games

Product type Book
Published in Nov 2012
Publisher Packt
ISBN-13 9781849519625
Pages 166 pages
Edition 1st Edition
Languages

Table of Contents (15) Chapters

Box2D for Flash Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Hello Box2D World 2. Adding Bodies to the World 3. Interacting with Bodies 4. Applying Forces to Bodies 5. Handling Collisions 6. Joints and Motors 7. Skinning the Game 8. Bullets and Sensors Index

Controlling joints with motors


Some joints, such as the revolute joint feature motors, which in this case can be used to rotate the joint at a given speed unless a given maximum torque is exceeded.

Learning motors will allow you to develop every kind of car/truck game you see on the Web.

  1. To create the truck, we need to apply a motor on the rightmost cart, so in the addCart function we are adding an argument to tell us whether it should have a motor or not. Change the Main function to specify that frontCart will have a motor while rearCart won't:

    public function Main() {
      world=new b2World(new b2Vec2(0,5),true);
      debugDraw();
      ground();
      var frontCart:b2Body=addCart(200,430,true);
      var rearCart:b2Body=addCart(100,430,false);
      var dJoint:b2DistanceJointDef=new b2DistanceJointDef();
      dJoint.bodyA=frontCart;
      dJoint.bodyB=rearCart;
      dJoint.localAnchorA=new b2Vec2(0,0);
      dJoint.localAnchorB=new b2Vec2(0,0);
      dJoint.length=100/worldScale;
      var distanceJoint:b2DistanceJoint;
      distanceJoint...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}