Getting Strange Rotation from 1 line of code.

Apr 9, 2009 at 8:39 AM
I've got my paddle rotating based on the right thumbstick. If the stick is more than 0.1 to the right, this executes:

                rotateVelocity = 0.25f;
               maxRotationDistance = 0.785f;
                if (RState == RotatePaddleState.Neutral)
                {
                    _rectangleBody.AngularVelocity = 0;
                    _pivotJoint.UpperLimit = 6.2f;
                    _pivotJoint.LowerLimit = 6.2f - maxRotationDistance;
                    _rectangleBody.Rotation = 6.2f;
                }

                RState = RotatePaddleState.RotateRight;
                _rectangleBody.AngularVelocity -= velocityAdd;
                if (_rectangleBody.AngularVelocity < -maxRotationVelocity)
                    _rectangleBody.AngularVelocity = -maxRotationVelocity;
                if (input.CurrentGamePadState.ThumbSticks.Right.X > 0.1 &&
                    _rectangleBody.Rotation < 6.2f - maxRotationDistance * input.CurrentGamePadState.ThumbSticks.Right.X)
                    _rectangleBody.Rotation = 6.2f - maxRotationDistance * input.CurrentGamePadState.ThumbSticks.Right.X;

The last line is what is causing my problem. It is causing some strange interaction with the code that returns the paddle to horizontal, if the thumbstick is no longer active:

                rotateVelocity = 0.25f;
                _rectangleBody.AngularVelocity += rotateVelocity;
                if (_rectangleBody.AngularVelocity > maxRotationVelocity)
                    _rectangleBody.AngularVelocity = maxRotationVelocity;

                if (_rectangleBody.Rotation > 6.195f)
                {
                    _rectangleBody.Rotation = 0;
                    _rectangleBody.AngularVelocity = 0;
                    RState = RotatePaddleState.Neutral;
                    _pivotJoint.LowerLimit = 0;
                    _pivotJoint.UpperLimit = 0.01f;
                }

What's happening is that when the thumbstick is released, the paddle starts returning from rotation. When the second bit of code executes, the paddle does a full spin before returning to horizontal.
Where I think this issue comes in is that the last if statement isn't executing until after the paddle does the full counter-clockwise spin.

I'm mighty confused and going to bed, if anyone has insight, please to sharing!

~Eric
Apr 9, 2009 at 6:45 PM
Im also in the same situation,
whats happening is it is measuring the length of the thumbstick,
so if you push it down it will measure the length as 0,
then it will multiply your angle by 0, which would equal 0.
And 0 is horizantal in radians.
I am also wondering how to control the rotation by the thumbstick to aim my characters gun,
i was about to ask a similar question with other problems.
Apr 10, 2009 at 3:30 AM
What you have to realize as far as thumbstick/rotation goes is that 0 is basically equivalent to something just over 6.2f. So if you're at horizontal and need to rotate left, you first set rotation to 6.2, and then go from there. when you get near 6.2 and are going to the right, set rotation to 0.

If I've written that confusingly, just download the simple samples, and go into one where you rotate the agent with the triggers, and output the rotation of the object. You'll see what I'm saying.

Yeah, I'm going to keep working on this, I'm so close to it working perfectly, this is just a little frustrating. But hey, nothing worth doing is easy, right?
Apr 10, 2009 at 6:35 PM
Well i just ate a ice cream bar,
that waas pretty easy
But i guess programming and stealing a strawberry short cake bar are different things.
I think the way i set up my revolute joint and angle joint are causing the problems not the way im rotating them.