Android Question Rotate body with libGDX

noclass1980

Active Member
Licensed User
Longtime User
Hi, how do I rotate a body by 180 degrees in libGDX when the LG_TouchDown event fires? I can do the transform way for an instant change but I like to roate the body around its centre over a fixed time period, say 2-5 seconds. My body is currently set as World.BODYTYPE_Static rather than dynamic. Thanks in advance.
 

noclass1980

Active Member
Licensed User
Longtime User
Thanks for the suggestion. I've got a bottle with balls in it so i want to rotate to tip out the balls. The bottle is a Body so how do I rotate the Body. I tried your Tumbler example with the jd.enablemotor=false initially and put a jd.enablemotor=true in the LG_TouchDown sub but nothing happened. I know the TouchDown event was firing because I logged the X position of the Touch. I need the bottle to move when the user touches the screen. Any suggestions? Thanks as always! :)
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Thanks, I got that far too. I tried to rotate it by using the Transform2 function in the LG_Update and incrementally changing the angle of the body. That worked smoothly (both sprite and body moving together) but of course it breaks the contacts with other bodies. The balls that were inside the bottle then fall out the bottom! I want the balls to stay inside the bottle until the bottle is at an angle where the balls naturally pour out.

I thought the angle property was read only? I looked at your example and tried to rotate my sprite (using either the Rotation or the Rotate function) which it did but the body remained stationary. I think I need to attach the bodie and the sprite together somehow but not sure how to do it. I've attached the code for information. Line 159 uses the Transform and Line 160 is the Sprite rotation. Thanks
 

Attachments

  • PhysicsBodyExamples.zip
    47 KB · Views: 331
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Thanks, I got that far too. I tried to rotate it by using the Transform2 function in the LG_Update and incrementally changing the angle of the body. That worked smoothly (both sprite and body moving together) but of course it breaks the contacts with other bodies. The balls that were inside the bottle then fall out the bottom! I want the balls to stay inside the bottle until the bottle is at an angle where the balls naturally pour out.

I thought the angle property was read only? I looked at your example and tried to rotate my sprite (using either the Rotation or the Rotate function) which it did but the body remained stationary. I think I need to attach the bodie and the sprite together somehow but not sure how to do it. I've attached the code for information. Line 159 uses the Transform and Line 160 is the Sprite rotation. Thanks
Ah yes, you're right. The angle property is read only. I didn't use Box2D since many months...
If your body is rotated by the user, you probably need to add a mouse joint as I did in Box2D_Web to transform any finger move in push or pull actions.
I know that walterf25 did an example similar to yours. You should contact him.
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
The body is not rotated by the user, I used the TouchDown as a means to start the rotation. The intent in the future is to add a button to start the rotation. Thanks anyway.
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Ok, I've solved the problem using the code below in the LG_Update sub. The bottle can be rotated by setting it's angular velocity and watching the angle. As it reaches the desired angle, the angular velocity is reduced to prevent it overshooting and then finally set to zero.
B4X:
If RotateBottle=True Then
    If Rotated=False Then
    Bodies.AngularVelocity=0.5
    Rotated = True
    Else
    Bodies.AngularVelocity=Bodies.AngularVelocity
    End If
 
    If Bodies.Angle>2.9 AND Bodies.Angle<2.999 Then Bodies.AngularVelocity=0.25
    If Bodies.Angle>3.1 AND Bodies.Angle<3.13 Then Bodies.AngularVelocity=0.1
    If Bodies.Angle>=3.14 Then Bodies.AngularVelocity=0
Log("angvel " & Bodies.AngularVelocity)
    End If
    Log(Bodies.Angle)

The two Boolean flags are set in the LG_TouchDown sub which initiates the rotation
B4X:
Sub LG_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
Log("bodang = " & Bodies.Angle)
Log(ScreenX)
'these logs are used to check that the TouchDown event is firing correctly
RotateBottle = True
Rotated=False
End Sub

Hope this helps someone else
 
Upvote 0
Top