Creating a Balance body

Howdy!
Im trying to create a body (with 2 shapes) to simulate a balance (not sure if i called it right)!
so first shape is a rectangle like 200x12.5 with density>0
second shape is a circle with its radius 12.5 and density=0.
The body is made of these 2 shapes.
But the problem is its nto static, the body should have been pined because the second shape has density 0 right? and the rectangle should have moved like in a circle because its not static like a balance! Instead it falls down like the circle has density aswell.
Why this doesnt happen?
If im not doing something wrong what would be the method to achieve what i want?

Heres the code:

Code: Select all


package 
{
   import Box2D.Dynamics.b2World;
   import flash.display.MovieClip;
   import Box2D.Common.Math.b2Vec2;
   import Box2D.Collision.b2AABB;
   import Box2D.Collision.Shapes.b2PolygonDef;
   import Box2D.Collision.Shapes.b2CircleDef;
   import flash.display.Sprite;
   import Box2D.Dynamics.b2Body;
   import Box2D.Dynamics.b2BodyDef;
   import flash.events.Event;
   import Box2D.Collision.b2ContactPoint;
   import Box2D.Collision.b2Manifold;
   import Box2D.Dynamics.Contacts.b2Contact;
   import Box2D.Collision.b2Collision;
   import Box2D.Collision.b2ContactPoint;
   import Box2D.Dynamics.b2ContactListener;
   import flash.events.MouseEvent;
   import flash.events.EventDispatcher;
   import flash.display.DisplayObject;
   import flash.geom.Point;
   import flash.display.DisplayObjectContainer;
   
   public class WoodRectangle5BalanceActor extends Actor
   {
      public var woodRectangle5BalanceBody:b2Body;

      public function WoodRectangle5BalanceActor(parent:DisplayObjectContainer, location:Point, initVel:Point)
      {
         var woodRectangle5BalanceSprite=new WoodRectangle5Balance();
         parent.addChild(woodRectangle5BalanceSprite);
         
         var woodRectangle5BalanceShapeDef:b2PolygonDef=new b2PolygonDef();
         woodRectangle5BalanceShapeDef.vertexCount=4;
           b2Vec2(woodRectangle5BalanceShapeDef.vertices[0]).Set(200/Vals.RATIO, -12.5/Vals.RATIO);
           b2Vec2(woodRectangle5BalanceShapeDef.vertices[1]).Set(200/Vals.RATIO, 12.5/Vals.RATIO);
         b2Vec2(woodRectangle5BalanceShapeDef.vertices[2]).Set(-200/Vals.RATIO, 12.5/Vals.RATIO);
         b2Vec2(woodRectangle5BalanceShapeDef.vertices[3]).Set(-200/Vals.RATIO, -12.5/Vals.RATIO);
         woodRectangle5BalanceShapeDef.density=5;
         woodRectangle5BalanceShapeDef.friction=0.65;
         woodRectangle5BalanceShapeDef.restitution=0;
         
         
         var woodRectangle5BalanceShapeDef2:b2CircleDef=new b2CircleDef();
         woodRectangle5BalanceShapeDef2.radius=12.5/Vals.RATIO;
         woodRectangle5BalanceShapeDef2.density=0;
         woodRectangle5BalanceShapeDef2.friction=0.65;
         woodRectangle5BalanceShapeDef2.restitution=0;
         
         
         var woodRectangle5BalanceBodyDef:b2BodyDef=new b2BodyDef();
         woodRectangle5BalanceBodyDef.position.Set(location.x/Vals.RATIO, location.y/Vals.RATIO);
         

         woodRectangle5BalanceBody=Vals.world.CreateBody(woodRectangle5BalanceBodyDef);
         woodRectangle5BalanceBody.CreateShape(woodRectangle5BalanceShapeDef);
         woodRectangle5BalanceBody.CreateShape(woodRectangle5BalanceShapeDef2);
         woodRectangle5BalanceBody.SetMassFromShapes();
         woodRectangle5BalanceBody.m_angularDamping=0.05;
         
         
         
         
         var velocityVector:b2Vec2=new b2Vec2(initVel.x/Vals.RATIO, initVel.y/Vals.RATIO);
         woodRectangle5BalanceBody.SetLinearVelocity(velocityVector);
         
         super(woodRectangle5BalanceBody, woodRectangle5BalanceSprite);
      }

   }
   
}




Thanks 😀.
Hmm maybe just make 2 separate bodies, the circle with radius slightly lower than that of the rectangle and then make a hole with isSensor in the rectangle and put it like that?
The entire body has to have a mass of 0 if you want it to be static. If you don't want the rectangle-circle body to move at all, don't use the SetMassFromShapes() function.

I assume that the rectangle is the balance beam and the circle the center or fulcrum of the balance, so setting the entire body static is probably not what you want and you just want the circle to be static. For this, you need two separate bodies, set the circle to one body (I think you can set the density to 0 and the SetMassFromShapes() function will realize that the average density is 0) and set the rectangle to the other one. Then, you connect the two bodies with a revolute joint.
Thanks for the help.
How would i create a revolute joint, could you post me an exemple after my code?
Yeah i got it, i created the joint, thanks for the info mate 😀

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