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:

58
59
60
the_rev_joint.enableMotor = true;
the_rev_joint.motorSpeed = 2;
the_rev_joint.maxMotorTorque = 1;

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

60
the_rev_joint.maxMotorTorque = 10;

and it will work as expected

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

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 4.67 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 17 comments

  1. Monkios

    on January 19, 2009 at 4:17 pm

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

  2. Gawd

    on January 19, 2009 at 4:26 pm

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

  3. Peter

    on January 19, 2009 at 4:28 pm

    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.

  4. Emanuele Feronato

    on January 19, 2009 at 7:59 pm

    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?

  5. Gustav

    on January 19, 2009 at 8:41 pm

    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

  6. oliver_l1

    on January 19, 2009 at 11:22 pm

    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/

  7. dim

    on January 19, 2009 at 11:42 pm

    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

  8. Emanuele Feronato

    on January 20, 2009 at 10:14 am

    APE has not been updated since august 2007…

  9. Shival

    on January 20, 2009 at 10:20 am

    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.

  10. lukasz

    on January 24, 2009 at 8:42 am

    excellent work ;]

  11. KCaz

    on January 25, 2009 at 7:06 pm

    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.

  12. Box2D joints: Prismatic Joints : Emanuele Feronato

    on February 3, 2009 at 2:15 pm

    [...] script is the one you can find at Box2D joints: Revolute Joint – Building motors with some modifications in order to create prismatic joints. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [...]

  13. Creating a sling with Box2D using joints : Emanuele Feronato

    on February 7, 2009 at 10:44 pm

    [...] used a distance joint, a revolute joint and a motor. The code does not need to be commented because there is nothing new, just look how I destroy the [...]

  14. anita

    on March 6, 2009 at 4:18 am

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

  15. Nils

    on March 31, 2009 at 3:39 pm

    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?

  16. Box2DFlash 2.1a released – what changed : Emanuele Feronato - italian geek and PROgrammer

    on January 27, 2010 at 5:30 pm

    [...] But I think an example will be more explicative than a thousand words, so I created a simple vehicle you can control with left and right arrow keys. It uses revolute joints and motors. [...]

  17. Adam

    on January 7, 2011 at 6:19 pm

    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.