How to change joint position of Revolute joint (Box2D)

Discuss issues specific the Java port of Box2D
Khubaib
Posts: 13
Joined: Sat Jul 12, 2014 12:44 pm

How to change joint position of Revolute joint (Box2D)

Postby Khubaib » Sat Jul 12, 2014 12:50 pm

I want to know if there is a way to change the joint position of a revolute joint. Like as provided in one of the examples from the book "Nature of the code", the windmill example, i want to change the joint position so that the joint is made at either end of the box2 (the one that is rotating). I tried setting its MassData it moved the joint to the right place but the motor function is not functioning.
i want this: https://www.dropbox.com/s/nkakvpr4p3iyyoj/joint.png

Code: Select all

class Windmill {
 
  RevoluteJoint joint;
  Box box1;
  Box box2;
 
  Windmill(float x, float y) {
 
    box1 = new Box(x, y-20, 120, 10, radians(0), false); [img][/img]
    box2 = new Box(x-60, y, 10, 40, 0, true);
 
    // Define joint as between two bodies
    RevoluteJointDef rjd = new RevoluteJointDef();
      rjd.initialize(box1.body, box2.body, box1.body.getWorldCenter());
    // Turning on a motor (optional)
    rjd.motorSpeed = -PI*3; 
    rjd.maxMotorTorque = 1000.0;
    rjd.enableMotor = true; 
 
      // Create the joint
    joint = (RevoluteJoint) box2d.world.createJoint(rjd);
  }
 
  // Turn the motor on or off
  void toggleMotor() {
    joint.enableMotor(!joint.isMotorEnabled());
  }
 
  boolean motorOn() {
    return joint.isMotorEnabled();
  }
 
 
  void display() {
    box2.display();
    box1.display();
    // Draw anchor just for debug
    Vec2 anchor = box2d.coordWorldToPixels(box1.body.getWorldCenter());
    fill(255);
    noStroke();
    ellipse(anchor.x, anchor.y, 10, 10);
  }
}

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: How to change joint position of Revolute joint (Box2D)

Postby irresistible force » Sat Jul 12, 2014 7:05 pm

As mentioned in the Box2D manual:
"The initialization function assumes that the bodies are already in the correct position."
If the two bodies are created in the positions as shown in the 'what I want' side of your picture, then I would expect this to work just fine (assuming box1.body.getWorldCenter() returns that pivot point as shown in the picture).

You may find it more convenient to set up the joint anchor positions manually instead of using the initialize function. This page might help: http://www.iforce2d.net/b2dtut/joints-revolute

Khubaib
Posts: 13
Joined: Sat Jul 12, 2014 12:44 pm

Re: How to change joint position of Revolute joint (Box2D)

Postby Khubaib » Sat Jul 12, 2014 10:08 pm

Thanks a lot. It worked when i changed the localAnchor points of the joint. Thanks for the link it is very useful :)


Return to “Java”



Who is online

Users browsing this forum: No registered users and 1 guest