Can't get my FixedLinearSpring to behave.

Mar 28, 2009 at 11:06 AM
I'm trying to dynamically create and dispose a fixedLinearSpring that will behave as follows: When the user tilts the right thumbstick down, the paddle will be drawn back on a spring. On release, the paddle will spring back to where it started. Once it reaches that point, the spring is disposed. However, I am getting really strange behavior out of the spring. Using the physicsView, I can see that the spring is angled far to the bottom right, and the rectangle body gets shoved to the side and reacts to the spring which is nowhere near the body.

I'm hoping it's something simple that's going wrong, some logic error. Can someone help? If the problem isn't obvious, I'll figure out how to post a video, because this is just strange.

My code:
public bool DoRightStick(InputState input, PhysicsSimulator physicsSimulator)
        {

            if (input.CurrentGamePadState.ThumbSticks.Right.Y < -0.1f)
            {
                
                if (springPosition == -1)
                {
                    _paddleSpring = SpringFactory.Instance.CreateFixedLinearSpring(physicsSimulator,
                                                _rectangleBody, _rectangleBody.Position, _rectangleBody.Position,
                                                2, 0.1f);
                    _rectangleBody.LinearVelocity = Vector2.Zero;
                    springPosition = _rectangleBody.Position.Y;
                }

                _rectangleBody.LinearVelocity.Y -= 10;
                if (_rectangleBody.Position.Y < springPosition -
                    100 * -input.CurrentGamePadState.ThumbSticks.Right.Y)
                    _rectangleBody.Position = new Vector2(_rectangleBody.Position.X, springPosition -
                    100 * -input.CurrentGamePadState.ThumbSticks.Right.Y);

                _paddleSpring.Enabled = true;
                //set spring
                return false;
            }

            //stick isn't down but returning from spring
            else if (State == PaddleState.SpringEnabled)
            {
                if (_rectangleBody.Position.Y > springPosition)
                {
                    _rectangleBody.Position = new Vector2(_rectangleBody.Position.X,springPosition);
                    _rectangleBody.LinearVelocity = Vector2.Zero;
                    _paddleSpring.Dispose();
                }


            }

            return true;
        }

Any help would be very appreciated.

~Eric
Mar 28, 2009 at 11:47 AM
Didn't want to just edit my post in case someone saw something in it, but it looks like there must be a problem in my spring initialization. I took away all user input and just created the spring on initialization. 

The issue is still happening. Here is how I did my initialization:

_paddleSpring = SpringFactory.Instance.CreateFixedLinearSpring(physicsSimulator,
                                                _rectangleBody, _rectangleBody.Position, _rectangleBody.Position,
                                                2, 0.1f);
In debugging, the world and body attach points are identical (as I desired). However, the rest length is 640. The other thing I noticed is that _difference is <-512,-384>. It is worth noting that 512, 384 is the exact position of my body, and the world anchor point.

Given the fact that when the spring initializes, it is at a 45 degree angle, those numbers seem significant. I guess I'm just going to dive into the source code to figure out what all that means.
Developer
Mar 28, 2009 at 2:28 PM
In your code you set the attachment point for the body to its world position. A local coordinate is expected for that position. If you change that parameter to new Vector(0,0) the spring will attach to the center of _rectangle body. I think this will give you what you are looking for.