Here you go (see below). Note that in Box2D I don't think you can have the body instantly exist at the new angle based on the control position. Therefore, you need to have the body move at a moderate pace from its starting angle to its ending angle. Its possible that collisions could occur while the body is moving if there are other bodies in its path.
In the following code, I have setup (as a sample) the
left joystick to manipulate the body (that is attached to the RevoluteJoint) by using setAngularVelocity. Conversely, the
right joystick manipulates the body by using setLinearVelocity. These are two different ways to make the body move about its joint.
setAngularVelocity however will cause the body to infinitely rotate until the joystick is let go, or until the body reaches a pre-established angle limit, or until you apply custom code to stop its movement at the desired time. A "limit" can be applied by setting values on the joint itself similar to this:
enableLimit = true
lowerAngle = MathUtils.degToRad(25)
upperAngle = MathUtils.degToRad(135)
Alternatively you can choose to constantly detect the current angle of the body, and then stop all movement (by no longer calling setAngularVelocity) when you have reached a desired angle, by using this:
myBody.getAngle()
setLinearVelocity on the other hand will naturally (gradually) bring the body to an angle that somewhat correlates with the position of the joystick. And it will stop there even if the joystick is still being held down. It will only move again if the joystick is moved in a different direction.
Note that I am using "multipliers" such as 40f and 10f. This is necessary due to the density of your body as well as the torque of your joint. If you give your body a high value for density or a high value for torque, then you need to use a high value for the multiplier when trying to move the body.
For your RevoluteJoint, you can start experimenting by setting the following values:
motorSpeed = 0
maxMotorTorque = 20
density = 2 (this one is applied to your body, not the joint)
Using java Syntax Highlighting
private void myJoysticks() {
// SETUP THE LEFT JOYSTICK
final float x1 = 0;
final float y1 = mCameraHeight - this.mJoystickBaseTextureRegion.getHeight();
leftJoystick = new AnalogOnScreenControl(x1, y1, this.mCamera, this.mJoystickBaseTextureRegion, this.mJoystickKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() {
@Override
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
myBody.setAngularVelocity(pValueY * 40f);
}
@Override
public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
// Do Nothing
}
});
leftJoystick.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
leftJoystick.getControlBase().setAlpha(0.5f);
leftJoystick.refreshControlKnobPosition();
this.mScene.setChildScene(leftJoystick);
// SETUP THE RIGHT JOYSTICK
final float x2 = mCameraWidth - this.mJoystickBaseTextureRegion.getWidth();
final float y2 = mCameraHeight - this.mJoystickBaseTextureRegion.getHeight();
rightJoystick = new AnalogOnScreenControl(x2, y2, this.mCamera, this.mJoystickBaseTextureRegion, this.mJoystickKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() {
@Override
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
final Vector2 velocity;
velocity = Vector2Pool.obtain(pValueX * 10f, pValueY * 10f);
myBody.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
}
@Override
public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
// Do Nothing
}
});
rightJoystick.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
rightJoystick.getControlBase().setAlpha(0.5f);
rightJoystick.refreshControlKnobPosition();
leftJoystick.setChildScene(rightJoystick);
}
Parsed in 0.038 seconds, using
GeSHi 1.0.8.4