I'm trying to build a system that has a bucket that travels up, rotates 180 degrees, travels back down, rotates 180 degrees again, then starts over. I'm using a prismatic joint to get it to move in a straight line, which seems to be working fine, but getting the revolute joint working has been giving me a headache for about 3 hours now. The first time I run it, it works correctly, but once it's upside-down, trying to create a new revolute joint just fails miserably and I'm not sure why. This is what I have so far:
For some reason, it's somehow rotating to a new angle instantly, regardless of the speed or maximum torque. Help?
Code:
final Vec2 prismaticAxis = new Vec2(0,-1);
//The bucket starts out at the bottom
final Vec2 bottomAnchor = new Vec2(bodyBase.getPosition());
//We want to rotate around a point 2 units to the right of the center of the bucket
bottomAnchor.x += 2;
//And the top anchor is 12 units away from the bottom anchor
final Vec2 topAnchor = new Vec2(bottomAnchor);
topAnchor.y -= 12;
bucketMoverDef = new PrismaticJointDef();
bucketMoverDef.initialize(groundBody, bodyBase, bottomAnchor, prismaticAxis);
//As per the top anchor, the translation limits are set up to be 12 units away from the starting point
bucketMoverDef.lowerTranslation = -12;
bucketMoverDef.upperTranslation = 12;
bucketMoverDef.enableLimit = true;
bucketMoverDef.maxMotorForce = 100;
bucketMoverDef.motorSpeed = 3;
bucketMoverDef.enableMotor = true;
bucketMover = (PrismaticJoint) world.createJoint(bucketMoverDef);
bucketRotatorDef = new RevoluteJointDef();
bucketRotatorDef.enableLimit = true;
bucketRotatorDef.maxMotorTorque = 100;
bucketRotatorDef.motorSpeed = 3;
bucketRotatorDef.enableMotor = true;
movingStep = new Steppable() {
private void switchToRotating(boolean atTop) {
world.destroyJoint(bucketMover);
removeSteppable(this);
addSteppable(rotatingStep);
//At this point, it's done traveling vertically and needs to start rotating. I have no idea what to do here to make this work
bucketRotatorDef.initialize(groundBody, bodyBase, atTop ? topAnchor : bottomAnchor );
//Note that the reference angle is 0 the first time, but -pi the second time. Not sure what that means or how to handle it
bucketRotatorDef.upperAngle = atTop ? MathUtils.PI : MathUtils.TWOPI;
bucketRotatorDef.lowerAngle = atTop ? -100 : -100;
bucketRotator = (RevoluteJoint) world.createJoint(bucketRotatorDef);
}
public void step(float dt) {
switch(bucketMover.m_limitState) {
case AT_LOWER:
//We've reached the bottom of the system, start rotating to move up (and set up our own speed for later)
bucketMoverDef.motorSpeed = 3;
switchToRotating(false);
break;
case AT_UPPER:
//We've reached the top of the system, start rotating to move down (and set up our own speed for later)
bucketMoverDef.motorSpeed = -3;
switchToRotating(true);
break;
}
}
};
rotatingStep = new Steppable() {
private void switchToMoving(boolean atTop) {
//This section seems to work fine
world.destroyJoint(bucketRotator);
removeSteppable(this);
addSteppable(movingStep);
bucketMoverDef.initialize(groundBody, bodyBase, atTop ? topAnchor : bottomAnchor, prismaticAxis);
bucketMover = (PrismaticJoint) world.createJoint(bucketMoverDef);
}
public void step(float dt) {
//The limitState seems pretty much random approx. 100% of the time after the first successful rotation.
switch (bucketRotator.m_limitState) {
case AT_UPPER:
switchToMoving(bodyBase.getPosition().y < 7);
break;
case AT_LOWER:
switchToMoving(bodyBase.getPosition().y < 7);
break;
}
}
};
addSteppable(movingStep);
//The bucket starts out at the bottom
final Vec2 bottomAnchor = new Vec2(bodyBase.getPosition());
//We want to rotate around a point 2 units to the right of the center of the bucket
bottomAnchor.x += 2;
//And the top anchor is 12 units away from the bottom anchor
final Vec2 topAnchor = new Vec2(bottomAnchor);
topAnchor.y -= 12;
bucketMoverDef = new PrismaticJointDef();
bucketMoverDef.initialize(groundBody, bodyBase, bottomAnchor, prismaticAxis);
//As per the top anchor, the translation limits are set up to be 12 units away from the starting point
bucketMoverDef.lowerTranslation = -12;
bucketMoverDef.upperTranslation = 12;
bucketMoverDef.enableLimit = true;
bucketMoverDef.maxMotorForce = 100;
bucketMoverDef.motorSpeed = 3;
bucketMoverDef.enableMotor = true;
bucketMover = (PrismaticJoint) world.createJoint(bucketMoverDef);
bucketRotatorDef = new RevoluteJointDef();
bucketRotatorDef.enableLimit = true;
bucketRotatorDef.maxMotorTorque = 100;
bucketRotatorDef.motorSpeed = 3;
bucketRotatorDef.enableMotor = true;
movingStep = new Steppable() {
private void switchToRotating(boolean atTop) {
world.destroyJoint(bucketMover);
removeSteppable(this);
addSteppable(rotatingStep);
//At this point, it's done traveling vertically and needs to start rotating. I have no idea what to do here to make this work
bucketRotatorDef.initialize(groundBody, bodyBase, atTop ? topAnchor : bottomAnchor );
//Note that the reference angle is 0 the first time, but -pi the second time. Not sure what that means or how to handle it
bucketRotatorDef.upperAngle = atTop ? MathUtils.PI : MathUtils.TWOPI;
bucketRotatorDef.lowerAngle = atTop ? -100 : -100;
bucketRotator = (RevoluteJoint) world.createJoint(bucketRotatorDef);
}
public void step(float dt) {
switch(bucketMover.m_limitState) {
case AT_LOWER:
//We've reached the bottom of the system, start rotating to move up (and set up our own speed for later)
bucketMoverDef.motorSpeed = 3;
switchToRotating(false);
break;
case AT_UPPER:
//We've reached the top of the system, start rotating to move down (and set up our own speed for later)
bucketMoverDef.motorSpeed = -3;
switchToRotating(true);
break;
}
}
};
rotatingStep = new Steppable() {
private void switchToMoving(boolean atTop) {
//This section seems to work fine
world.destroyJoint(bucketRotator);
removeSteppable(this);
addSteppable(movingStep);
bucketMoverDef.initialize(groundBody, bodyBase, atTop ? topAnchor : bottomAnchor, prismaticAxis);
bucketMover = (PrismaticJoint) world.createJoint(bucketMoverDef);
}
public void step(float dt) {
//The limitState seems pretty much random approx. 100% of the time after the first successful rotation.
switch (bucketRotator.m_limitState) {
case AT_UPPER:
switchToMoving(bodyBase.getPosition().y < 7);
break;
case AT_LOWER:
switchToMoving(bodyBase.getPosition().y < 7);
break;
}
}
};
addSteppable(movingStep);
For some reason, it's somehow rotating to a new angle instantly, regardless of the speed or maximum torque. Help?