Box2D joints: Revolute Joint – Building motors

After seeing how to create a revolute joint, it’s time to use them to create motors.

Box2D already has everything we need to create such motors.

The script is the same as the one I showed you in Box2D joints: Revolute Joint, and you just need to add the lines I am going to show between lines 57 and 58:

enableMotor: A flag to enable the joint motor

motorSpeed: The DESIRED (see below) motor speed, radians per second.

maxMotorTorque: The maximum motor torque (moment) used to achieve the desired motor speed, in joules.

Normally one joule is the amount of energy required by a force of one newton traveling through a distance of one meter, that’s why around the web you can see torque expressed as N*m.

Anyway, it does not matter if I whether I am talking about joules or bananas, if your torque is too weak, the motor won’t have the necessary force to reach the desired speed, and sometimes it won’t even work, like in this case:

The motor does not have enough force to raise its own arm…

To make it work, just change line 60 with a higher value such as

and it will work as expected

Next time we’ll se how to limit a motor and create passive forces

  • Give a little swing to the motor with a torque of 1 and it will work ^^

  • Gawd

    I’m digging these Box2D Tuts. Even though its too advanced for me its still a fun read. ^^ Keep up the good work.

  • Peter

    Totally agree with Gawd:

    I’m digging these Box2D Tuts. Even though its too advanced for me its still a fun read. ^^ Keep up the good work.

  • Emanuele Feronato

    I feel a bit pointless reading a tutorial “for fun” and nothing more…

    … do readers need a basic tutorial, a really simple box2d “hello world” to get used with it?

  • Gustav

    Actually, a box2d simple tutorial, where to get it, how to use it, the tools, and so on, would be great. Using these physics could create awesome games, for example your game, SamePhysics. Please? :D
    Oh, and a small list of game examples based on box2d! :D

  • oliver_l1

    just try the APE physics engine.It is easy to understand and use and there is a fantastic tutorial out there which explains the “basics”.

    These tutorials can be found at:

    http://www.andrewgarrahan.com/

  • dim

    i like these tutorials, keep up good work … i’m programming complex websites, web aplications, widgets and more … for now, i haven’t stand a chance to make some game … not becouse skills, but becouse time … but i enjoy these tutorials … they kindle my interest about these things

  • Emanuele Feronato

    APE has not been updated since august 2007…

  • Yeah exactly.
    A real start from scratch series would be perfect.
    Meaning you cannot use a Chinese dictionary unless you know how to speak chinese.

  • excellent work ;]

  • KCaz

    Hello, I’ve been following all your box2D work and I after reading most of your tutorials I’ve started to make a game, and it’s coming along great!

    The only thing that I want to know is how to add MC’s to joints.

  • Pingback: Box2D joints: Prismatic Joints : Emanuele Feronato()

  • Pingback: Creating a sling with Box2D using joints : Emanuele Feronato()

  • anita

    Thanks for all your posts. This is the most useful blog ever

  • Nils

    Is there an inexpensive tool for building Flash applications, like this one? What about debugging? Is it possible to set breakpoints and single step the code? Otherwise I think I will develop all the physics in C++ or C#, and then translate it to another language for the web.
    Any suggestions?

  • Pingback: Box2DFlash 2.1a released – what changed : Emanuele Feronato - italian geek and PROgrammer()

  • Is it possible to constrain the turn of the revolute joint so that it only moves through an arc?

    I am trying to build a pinball simulation, and have a lovely looking flipper instantiated, unfortunately, it swings round and round! How can I restrict its movement to say 60degs?

    Is there a spring class in box2D Emanuel?

    Thanks for all your tutorials -they are a great help.