Prismatic Joint doesn't work

I am trying to make a pressure sensitive button.

I have two bodies. A static base, and a dynamic rectangle of thickness .25 for the button, which is initially above the base.
I create a Prismatic joint between the two bodies using initialize with translation axis of (0,1)
I gave it translation limits of 0 and .25
and a motor with max force 100 and speed -.75

However, it does not work at all like I expected. First off, Prismatic Joint is supposed to prevent side to side motion, but I can still push the button around side to side.
Also, when there's no weight, the button slowly rises to the ceiling. I thought translation limits were supposed to prevent this.

What am I dong wrong?
Did you look at the example in the testbed? How is your setup different?

Can you modify the testbed example to reproduce your problem?
I can't build the Testbed. Where can I get a GLUI binary to link to?
It is included.
Well it was pretty confusing because the files weren't in the places where the Code::Blocks project expected them to be, but I eventually figured it out.

Here's a testbed example. The button continually rises no matter what, completely ignoring all constraints.

Code:
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef PRISMATIC2_H
#define PRISMATIC2_H

class Prismatic2 : public Test
{
public:
   Prismatic2()
   {
      b2Body* base = NULL;
      {
         b2BodyDef def;
         def.type = b2_staticBody;
         def.position.Set(0,0);
         def.fixedRotation = true;
         base = m_world->CreateBody(&def);

            b2PolygonShape poly;
            poly.SetAsBox( 1.625, 0.1875 );
         base->CreateFixture(&poly);
      }

      b2Body* button = NULL;
      {
         b2BodyDef def;
         def.type = b2_dynamicBody;
         def.position.Set(0,.5);
         def.fixedRotation = true;
         button = m_world->CreateBody(&def);

            b2PolygonShape poly;
            poly.SetAsBox( 1.125, 0.125 );
         button->CreateFixture(&poly);
      }

      {
         b2PrismaticJointDef pjd;

         // Bouncy limit
         pjd.Initialize(base, button, b2Vec2(0.0f, 0.0f), b2Vec2(0.0f, -1.0f));

         // Non-bouncy limit
         //pjd.Initialize(ground, body, b2Vec2(-10.0f, 10.0f), b2Vec2(1.0f, 0.0f));

         pjd.motorSpeed = -0.5f;
         pjd.maxMotorForce = 100.0f;
         pjd.enableMotor = true;
         pjd.lowerTranslation = 0.0f;
         pjd.upperTranslation = 0.25f;
         pjd.enableLimit = true;

         m_joint = (b2PrismaticJoint*) m_world->CreateJoint(&pjd);
      }
   }

   void Keyboard(unsigned char key)
   {
      switch (key)
      {
      case 'l':
         m_joint->EnableLimit(!m_joint->IsLimitEnabled());
         break;

      case 'm':
         m_joint->EnableMotor(!m_joint->IsMotorEnabled());
         break;

      case 'p':
         m_joint->SetMotorSpeed(-m_joint->GetMotorSpeed());
         break;
      }
   }

   void Step(Settings* settings)
   {
      Test::Step(settings);
      m_debugDraw.DrawString(5, m_textLine, "Keys: (l) limits, (m) motors, (p) speed");
      m_textLine += 15;
      float32 force = m_joint->GetMotorForce();
      m_debugDraw.DrawString(5, m_textLine, "Motor Force = %4.0f", (float) force);
      m_textLine += 15;
   }

   static Test* Create()
   {
      return new Prismatic2;
   }

   b2PrismaticJoint* m_joint;
};

#endif

Storyyeller,

Based upon memory, I think either (or both) of these two suggestions might help. Not sure exactly why, but they helped me with a similar problem.

1) Change:
Code:
// Bouncy limit
pjd.Initialize(base, button, b2Vec2(0.0f, 0.0f), b2Vec2(0.0f, -1.0f));
To:
Code:
// Bouncy limit
pjd.Initialize(base, button, button->GetWorldCenter(), b2Vec2(0.0f, -1.0f));



2) Change one or both occurances of:
Code:
def.fixedRotation = true;
To:
Code:
def.fixedRotation = false;



BTW, As I recall, if solution 1) works, then you probably won't need to do solution 2).

Hope this helps,
Q
Neither one had any change, although they shouldn't affect things anyway. Prismatic joints already fix rotation, and it doesn't really matter where the anchor point is, given that we're only dealing with translational motion.
I just switched to using a Line Joint and it works perfectly.

I still don't understand why the prismatic joint wasn't working though.
If you don't set the density of your dynamic bodies, then Box2D sets the mass to one and sets the inertia tensor to infinity (using zero to represent infinity).

This causes the prismatic joint solver to give up because no amount of torque can control the rotation of dynamic body.

So this post proves to me once and for all that default argument values are bad.
It only proves that one choice of defaults is bad. One down, infinity to go 😉. I think defaults should represent what people expect physically from simple objects. So it'd make more sense to default the density of bodies than default the mass. I thought that was where you were going with encouraging use of ResetMassData over SetMassData?

Also, dynamic should default over static as people expect things to be free until they explicitly fix them, not visa versa. And so on.

Edit: Oh, you meant for CreateFixture(...). That seems fair.

Original thread: https://box2d.org/forum/viewtopic.php?t=4532