Is it possible to set rotation of join with screen control?

  ... the Physics Box2D Extension for AndEngine.

Is it possible to set rotation of join with screen control?

Postby Sleepy_Weasel » Fri Feb 10, 2012 8:36 pm

I've successfully set a joint, and I've got a screen control for rotation, but now Im wondering if its possible to set the rotation of the joint to the same degree of the rotation of the control. Has anyone done this?
Sleepy_Weasel
 
Posts: 37
Joined: Wed Jan 25, 2012 1:47 am

Re: Is it possible to set rotation of join with screen contr

Postby RealMayo » Fri Feb 10, 2012 8:53 pm

Yes I've done something similar to this with RevoluteJoint and AnalogOnScreenControl. You just setAngularVelocity on the body attached to the joint based on logic surrounding the X Y coordinates of the movement of the control.
User avatar
RealMayo
 
Posts: 1683
Joined: Sat Sep 03, 2011 9:25 pm
Location: Chicago, IL

Re: Is it possible to set rotation of join with screen contr

Postby Sleepy_Weasel » Tue Feb 14, 2012 12:14 am

Do you have an example of this by any chance?
Sleepy_Weasel
 
Posts: 37
Joined: Wed Jan 25, 2012 1:47 am

Re: Is it possible to set rotation of join with screen contr

Postby RealMayo » Tue Feb 14, 2012 2:44 am

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)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 private void myJoysticks() {
  2. // SETUP THE LEFT JOYSTICK
  3.                         final float x1 = 0;
  4.                         final float y1 = mCameraHeight - this.mJoystickBaseTextureRegion.getHeight();
  5.                         leftJoystick = new AnalogOnScreenControl(x1, y1, this.mCamera, this.mJoystickBaseTextureRegion, this.mJoystickKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() {
  6.                                 @Override
  7.                                 public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
  8.                                                 myBody.setAngularVelocity(pValueY * 40f);                                              
  9.                                 }
  10.  
  11.                                 @Override
  12.                                 public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
  13.                                         // Do Nothing
  14.                                 }
  15.                         });
  16.                         leftJoystick.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
  17.                         leftJoystick.getControlBase().setAlpha(0.5f);
  18.                         leftJoystick.refreshControlKnobPosition();
  19.                         this.mScene.setChildScene(leftJoystick);
  20.  
  21. // SETUP THE RIGHT JOYSTICK                    
  22.                         final float x2 = mCameraWidth - this.mJoystickBaseTextureRegion.getWidth();
  23.                         final float y2 = mCameraHeight - this.mJoystickBaseTextureRegion.getHeight();
  24.                         rightJoystick = new AnalogOnScreenControl(x2, y2, this.mCamera, this.mJoystickBaseTextureRegion, this.mJoystickKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() {
  25.                                 @Override
  26.                                 public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {                        
  27.                                         final Vector2 velocity;
  28.                                         velocity = Vector2Pool.obtain(pValueX * 10f, pValueY * 10f);                                   
  29.                                         myBody.setLinearVelocity(velocity);
  30.                                         Vector2Pool.recycle(velocity);                                                                                 
  31.                                 }
  32.  
  33.                                 @Override
  34.                                 public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
  35.                                         // Do Nothing
  36.                                 }
  37.                         });
  38.                         rightJoystick.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
  39.                         rightJoystick.getControlBase().setAlpha(0.5f);
  40.                         rightJoystick.refreshControlKnobPosition();
  41.                         leftJoystick.setChildScene(rightJoystick);
  42.                 }
  43.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Last edited by RealMayo on Tue Feb 14, 2012 7:15 pm, edited 1 time in total.
User avatar
RealMayo
 
Posts: 1683
Joined: Sat Sep 03, 2011 9:25 pm
Location: Chicago, IL

Re: Is it possible to set rotation of join with screen contr

Postby Sleepy_Weasel » Tue Feb 14, 2012 7:07 pm

Working fantastic. Thanks again!
Sleepy_Weasel
 
Posts: 37
Joined: Wed Jan 25, 2012 1:47 am

Re: Is it possible to set rotation of join with screen contr

Postby Sleepy_Weasel » Tue Feb 14, 2012 8:42 pm

Im trying to eliminate movement of the joint when the player moves. In essence when the joystick is over 0,0 or not moving, enable limits on the joint, but when the joystick is moved to remove those limits. Is there a reason this wont work?

Syntax: [ Download ] [ Hide ]
  1. if (pValueX == x1 && pValueY == x1) { 
  2. AxeHandleBody.getAngle(); 
  3. revjoint2.enableLimit = true; 
  4. revjoint2.lowerAngle=AxeHandleBody.getAngle(); 
  5. revjoint2.upperAngle=AxeHandleBody.getAngle(); 
  6. } else { 
  7. revjoint2.enableLimit=false; 
  8. velocity2 = Vector2Pool.obtain(pValueX * 6f, pValueY * 6f); 
  9. AxeHandleBody.setLinearVelocity(velocity2); 
  10. Vector2Pool.recycle(velocity2); 
  11. swinging = false; 
  12.  


The enable limit = false doesnt seem to allow the joint to move. Do I need to redo the upper/lower angles?
Sleepy_Weasel
 
Posts: 37
Joined: Wed Jan 25, 2012 1:47 am

Re: Is it possible to set rotation of join with screen contr

Postby Sleepy_Weasel » Thu Feb 23, 2012 6:36 pm

Anyone have a solution to the fixed joint rotation?
Sleepy_Weasel
 
Posts: 37
Joined: Wed Jan 25, 2012 1:47 am

Re: Is it possible to set rotation of join with screen contr

Postby RealMayo » Thu Feb 23, 2012 6:50 pm

I've had the same need, to limit the joint when the player moves. The only solution I have been able to come up with is to keep the joint active and limitless, but increase the Density and Torque A LOT so that player movement does not provide enough force to affect the joint.
User avatar
RealMayo
 
Posts: 1683
Joined: Sat Sep 03, 2011 9:25 pm
Location: Chicago, IL

Re: Is it possible to set rotation of join with screen contr

Postby Sleepy_Weasel » Thu Feb 23, 2012 7:12 pm

Ahh good idea. I'll do that. Thanks.
Sleepy_Weasel
 
Posts: 37
Joined: Wed Jan 25, 2012 1:47 am


Return to Physics Box2D

Who is online

Users browsing this forum: ganondorfan, laclan and 13 guests