Initial orientation/angle of body/revolute joint

Topics: User Forum
May 23, 2012 at 6:29 PM
Edited May 23, 2012 at 9:28 PM

I am new trying to create a body that "orbits" at a given radius. I created a center body with a FixedRevoluteJoint driven by a motor. The orbiting body is a rectangle that is attached to the center with a weld joint.

This works fine, but I would like to be able to set the initial angle/orientation (i.e. the angle of the axis between the center of the rectangle and the center). I tried SetTransform on the center but the result was consistently off and the rectangle is rotated.

Here is the code I am working with:

 

        public CreateOrbitingPlatform(World world, Vector2 position, float radius, float initialAngle, float width, float height)
        {
            center = BodyFactory.CreateCircle(world, 0.25f, 1000f);
            center.BodyType = BodyType.Dynamic;
            center.CollisionCategories = Category.None;
            center.CollidesWith = Category.None;
            //center.SetTransform(center.Position, radius);


            platform = BodyFactory.CreateRectangle(world, width, height, 5f);
            platform.BodyType = BodyType.Dynamic;

            motorJoint = JointFactory.CreateFixedRevoluteJoint(world, center, new Vector2(0f, 0f), position);

            motorJoint.MaxMotorTorque = 5;
            motorJoint.MotorSpeed = 0.5f;
            motorJoint.MotorTorque = 1f;
            motorJoint.MotorEnabled = true;

            weldJoint = JointFactory.CreateWeldJoint(world, platform, center, new Vector2(0f, 0f), new Vector2(0, radius));
        }

 

From debugging, I've noticed that after calling SetTransform, the angle of the body is set to the desired angle, but the angle of the revolute joint is double the body. After the first World step, the angles of both change instantly by the same amount, although this amount varies depending on what the initial angle was.

Seems like this should be simple, what am I missing?

May 23, 2012 at 9:30 PM
Edited May 23, 2012 at 9:42 PM

Its always nice to answer your own questions. I was chasing the wrong problem. I needed to position and orient the platform correctly in relation to the center. Here is the corrected code.

 

        public OrbitingPlatform(World world, Vector2 position, float radius, float initialAngle, 
                                                               float width, float height)
        {

            center = BodyFactory.CreateCircle(world, 0.25f, 1000f, position);
            center.BodyType = BodyType.Dynamic;
            center.CollisionCategories = Category.None;
            center.CollidesWith = Category.None;

            platform = BodyFactory.CreateRectangle(world, width, height, 5f); 
            platform.BodyType = BodyType.Dynamic;
            platform.SetTransform(new Vector2(position.X + radius * (float)Math.Cos(initialAngle), 
                                              position.Y + radius * (float)Math.Sin(initialAngle)), 
                                  -initialAngle);
            motorJoint = JointFactory.CreateFixedRevoluteJoint(world, center, 
                                                                new Vector2(0f, 0f), 
                                                                center.WorldCenter);

            motorJoint.MaxMotorTorque = 5;
            motorJoint.MotorEnabled = false;

            weldJoint = JointFactory.CreateWeldJoint(world, platform, center, 
                                       new Vector2(0f, 0f), 
                                       new Vector2(radius * (float)Math.Cos(initialAngle), 
                                                   radius * (float)Math.Sin(initialAngle)));
        }

 

The changes are marked in red.