Box2D Forums

It is currently Sun May 19, 2013 3:52 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: [SOLVED]Lever.
PostPosted: Wed May 23, 2012 6:52 am 
Offline

Joined: Fri Oct 07, 2011 9:39 am
Posts: 21
Hello, here's an image showing what I'm trying to achieve:

Image

Image

I know I have to use Joint (I think so)

I think I will use custom body type for the stick of this lever (green colour) with 2 sensors, one on the left side and one on the right (to determine which side its being touched after contact with player's body, so lever will be pushed left or right)

But I have no idea how to prevent this stick from 360 rotation.

Before trying to create this lever, I managed to create 'rotating cross' like this (its rotating constantly)

Image

With this code:

Code:
public static void createCross(TextureRegion boxTextureRegion, TextureRegion crossTextureRegion, PhysicsWorld physicsWorld, Scene scene, float x, float y, int speed)
   {
      final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(10, 0.2f, 0.5f);

      final Sprite anchorFace = new Sprite(x, y, 30, 30, boxTextureRegion);
      anchorFace.setCullingEnabled(true);
      final Body anchorBody = PhysicsFactory.createBoxBody(physicsWorld, anchorFace, BodyType.StaticBody, objectFixtureDef);

      final Sprite movingFace = new Sprite(x-85, y -85, crossTextureRegion);
      movingFace.setCullingEnabled(true);
      final Body movingBody = Utils.createCrossBody(physicsWorld, movingFace, objectFixtureDef, BodyType.DynamicBody);
      movingBody.setUserData("tile");

      final Line connectionLine = new Line(x + 30 / 2, y + 30 / 2, x + 30 / 2, y + 30 / 2);

      scene.attachChild(connectionLine);
      scene.attachChild(anchorFace);
      scene.attachChild(movingFace);

      physicsWorld.registerPhysicsConnector(new PhysicsConnector(anchorFace, anchorBody, true, true)
      {
         @Override
         public void onUpdate(final float pSecondsElapsed)
         {
            super.onUpdate(pSecondsElapsed);
            final Vector2 movingBodyWorldCenter = movingBody.getWorldCenter();
            connectionLine.setPosition(connectionLine.getX1(), connectionLine.getY1(), movingBodyWorldCenter.x * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, movingBodyWorldCenter.y * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
         }
      });
      physicsWorld.registerPhysicsConnector(new PhysicsConnector(movingFace, movingBody, true, true));

      final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
      revoluteJointDef.initialize(anchorBody, movingBody, anchorBody.getWorldCenter());
      revoluteJointDef.enableMotor = true;
      revoluteJointDef.motorSpeed = speed;
      revoluteJointDef.maxMotorTorque = 200;

      physicsWorld.createJoint(revoluteJointDef);
   }


(I'm using andengine, so there are additional classes such as Sprite etc)

I'm trying to modify this code to make it act as a lever, but no luck.

Any hints please?


Top
 Profile  
 
 Post subject: Re: [SOLVED]Lever.
PostPosted: Wed May 23, 2012 1:21 pm 
Offline

Joined: Fri Oct 07, 2011 9:39 am
Posts: 21
Problem solved, solution:

Code:
        final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
        revoluteJointDef.initialize(anchorBody, movingBody, anchorBody.getWorldCenter());
        revoluteJointDef.enableLimit = true;
        revoluteJointDef.upperAngle = MathUtils.degToRad(40);
        revoluteJointDef.lowerAngle = MathUtils.degToRad(-40);

        physicsWorld.createJoint(revoluteJointDef);   


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group