前回の記事では、jointについての基本的な情報について紹介しました。 we briefly mentioned what Revolute joint is: it forces two bodies to share a common anchor point, often called a hinge point. The revolute joint has a single degree of freedom: the relative rotation of the two bodies. This is called the joint angle. Before reading make sure you read introduction, and use same version of engines!
Click Here to read joints introduction.
Have a look on the image posted below, showing example of revolute joint in practise (image from BOX2D Manual page) showing two bodies connected with revolute joint:
// Create green rectangle final Rectangle greenRectangle = new Rectangle(400, 240, 40, 40, getVertexBufferObjectManager()); greenRectangle.setColor(Color.GREEN); scene.attachChild(greenRectangle); // Create red rectangle final Rectangle redRectangle = new Rectangle(460, 240, 120, 10, getVertexBufferObjectManager()); redRectangle.setColor(Color.RED); scene.attachChild(redRectangle); // Create body for green rectangle (Static) final Body greenBody = PhysicsFactory.createBoxBody(physicsWorld, greenRectangle, BodyType.StaticBody, PhysicsFactory.createFixtureDef(0, 0, 0)); // Create body for red rectangle (Dynamic, for our arm) final Body redBody = PhysicsFactory.createBoxBody(physicsWorld, redRectangle, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(5, 0.5f, 0.5f)); physicsWorld.registerPhysicsConnector(new PhysicsConnector(redRectangle, redBody, true, true)); // Create revolute joint, connecting those two bodies final RevoluteJointDef revoluteJointDef = new RevoluteJointDef(); revoluteJointDef.initialize(greenBody, redBody, greenBody.getWorldCenter()); revoluteJointDef.enableMotor = true; revoluteJointDef.motorSpeed = -1; revoluteJointDef.maxMotorTorque = 100; physicsWorld.createJoint(revoluteJointDef); // Attach box2d debug renderer if you want to see debug. scene.attachChild(new DebugRenderer(physicsWorld, getVertexBufferObjectManager()));
Important note: while setting position of the physics element, such as body position, or position of the anchor of the joint, always remember about using proper unit, you need meters, not pixels (to receive desired effect) To convert pixels to meters, you can do the following trick
float pixelsInMeters = pixelsValue / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;
Question: How to change direction of the motor movement?While creating revolute joint definition, and setting motorSpeed value , simply multiply your value by -1 (in other words, change the sign of your value)
Questions: What is maximum motor torque value?The maximum motor torque used to achieve the desired motor speed, if for instance, in our example, we would use much lower value (ex. 50 instead of 100) - motor torque would be to weak to perform full 360 degrees rotation.
Question: How to adjust joint values after creation, on the runtime?People often are making silly mistake, keeping reference to the RevoluteJointDef (joint definition) and than changing its values, expecting to result in any change, but it will not change anything, because its only a definition used to build Joint. To do it properly, we need to keep reference for our joint, just like that:
// Our reference private RevoluteJoint revoluteJoint; // While creating joint revoluteJoint = (RevoluteJoint) physicsWorld.createJoint(revoluteJointDef);
By doing that, we are free to modify our joint latter on, ex. changing motor speed.