Box2D joints: Prismatic Joints

Time to learn something about Box2D prismatic joints.

A prismatic joint provides one degree of freedom: translation along an axis fixed relative to the first body. Relative rotation is prevented.

In other words, prismatic joints can be used to represent pistons and other sliding objects.

In this tutorial I am going to show how to create a prismatic joint, and later I’ll show you some real-world examples.

The script is the one you can find at Box2D joints: Revolute Joint – Building motors with some modifications in order to create prismatic joints.

Line 15: Declaring the_prism_joint variable, b2PrismaticJointDef type. This is how I define prismatic joints.

Line 49: Initializing the prismatic joint. This is the most difficult and unclear part of the process. The first two parameters represent the objects tied by the joint, the third one is the point where to tie the joint and the fourth is a vector representing the movement allowed. In this case, being an horizontal vector, the movement allowed is horizontal

Notice about this 4th parameter that values inside b2Vec2() cannot be greater than 1 or you may experience strange results.

All in all, there is no need to write (100,0) to represent an horizontal vector, when you can just use (1,0)

Lines 50-51: upperTranslation and lowerTranslation represent the upper and lower translation limits, in meters. To avoid strange results, try to keep

lowerTranslation <= 0 <= upperTranslation

Line 52: Enabling the limits for upper and lower translations. If you don't set enableLimit to true, limits won't be considered.

Lines 53-55: Adding a simple motor. For a brief introduction about motors, refer to Box2D joints: Revolute Joint - Building motors.

Line 56: Adding the joint.

And this is the result. Try to drag the box

And this is the source code ready to be downloaded.

  • Gawd

    First! :D

  • noooooooo

  • In unrelated news: ‘My works’ page is still empty. Seems like a good place to showcase your games(I don’t see them listed anywhere else on the site).

    Now on topic, I’m sure those will be usefull when I’ll finally start makeing something box2d

  • Ben

    Is there a way to have it “spring” back with force instead of just a constant speed? Would that be the motor settings?

  • Enjoyed reading your blog in general… I forget what bought me here in the first place… searching for something or other.

    Best of luck with your games :)

  • Pingback: Box2D breakout prototype : Emanuele Feronato()

  • Dinkyfish

    Just wanted to stop by and say a massive thank you for all your tutorials. You’ve clearly put a huge amount of work into them and they’ve helped me out on many occasions. Keep up the good work!

  • Mukul

    Is there a way to manipulate the friction on the slide?
    Does the friction depend on the friction of the body sliding?

  • Avkash

    Hello all,
    Any one can help me about how can make zoom effect something like game 99-bricks

  • Viraj

    Very good tutorial..But i am just not getting how the body goes back to original position after mouse up or where is the code for that..

  • Very helpful this tutorial, I searched in google Box2D and I came across this post, thanks.