Rotating with obstacles around point

Topics: Developer Forum
Aug 9, 2012 at 6:34 AM
Edited Aug 9, 2012 at 6:35 AM

Hi,

I am trying to google it but I found more answers and for now I just can´t get right what I want.

I have obstacles and first I want to set it to world and rotate it around center of that obstacles and then if user touch screen and it is freedrag I want to rotate with obstacles around the point out of their bodies (exactly around center of screen).

I found some solutions with Revolution joint, Fixed RJ, recalculating vectores and matrixes but I didn´t help (or just I use it wrong I am not sure). I think there should be easy solution where I can set center of fixture of obstacle to exact point (center of screen) and then adding value to body property rotation I am rotating with object.

This is how I load obstacles:

for (int i = 0; i < 5; i++)
{
       _obstacles[i] = BodyFactory.CreateRectangle(World, 2f, 0.2f, 1f);
       _obstacles[i].IsStatic = true;
       _obstacles[i].Restitution = 0.2f;
       _obstacles[i].Friction = 0.2f;
}
_obstacles[2].Position = new Vector2(3f, 2f);
_obstacles[3].Position = new Vector2(3.5f, 5f);
_obstacles[3].Rotation -= 0.8f;
_obstacles[4].Position = new Vector2(2f, 5f);
_obstacles[4].Rotation += 0.8f;

_obstacle = new Sprite(ScreenManager.Assets.TextureFromShape(_obstacles[0].FixtureList[0].Shape,
                                                                    MaterialType.Dots,
                                                                    Color.SandyBrown, 0.8f)); 

And then I have handle for input and I want to start obstacles rotating around exact point which is center of screen:

foreach (GestureSample gesture in input.Gestures)
            {
                if (gesture.GestureType == GestureType.FreeDrag)
                {
                    if ((gesture.Position.X - gesture.Position2.X) > 0)
                    {
                        foreach (Body obstacle in _obstacles)
                        {
                            obstacle.Rotation += 0.01f;
                        }
                    }
                    else if ((gesture.Position.X - gesture.Position2.X) < 0)
                    {
                        foreach (Body obstacle in _obstacles)
                        {
                            obstacle.Rotation += -0.01f;
                        }
                    }
                }
            }

Thanks for help and sorry I am begginer in this.



 

Sep 13, 2012 at 1:45 PM

Noone can help me with this problem? 

I add image to better describe my problem. I want to have that brown obstacle which can be rotated around that red point and can be rotated to both position which are shown like light brown.

image with rotation

Sep 13, 2012 at 5:17 PM

You can use a revolute joint between your body and an extra static body at the center of rotation. You can also use a weld joint between them. Make sure you specify correct local anchors.

Or perhaps easier (because joints are a bit soft) you can make a fixture that is already offset and positioned away from the center of the Body. You can build the fixture from vertices directly (PolygonShape.Set()) or use PolygonShape.SetAsBox() passing appropriate center (aka Body.Position+offset) and angle. This way when you rotate the body the fixture will rotate rigidly with it.

Sep 21, 2012 at 10:44 AM

Hi Bibo

Have you solved the problem?

I'm trying to make a clock hand which the user can move around on the screen (making it look like a clock time moving forward).   I'm having troubles figuring out how to do it, and thought maybe something in your problem could help me.

Thanks

Dec 3, 2012 at 4:54 AM

Hi Bibo,

Have you found the answer to your problem,

Also have you noticed any jitter when the object rotates around the point.

Thanks

Ronak

Coordinator
Dec 6, 2012 at 11:52 PM

What jerrysb proposed are the solutions you have at hand. Rotating a body is done at the center point (what we call a centroid) of the shapes that are attached to the body. The point of rotation can be changed to be something else by translating the vertices of a polygon before adding it to the body.

Like this:

//This is the anchor that the RevoluteJoint will attach to
Body staticAnchor = BodyFactory.CreateRectangle(World, 1f, 1f, 1f);

//The center body that is the other body the RevoluteJoint attaches to
Vertices center = PolygonTools.CreateRectangle(0.5f, 0.5f);

//Left and right boxes that are offset
Vertices left = PolygonTools.CreateRectangle(1, 1, new Vector2(-5, 0), 0);
Vertices right = PolygonTools.CreateRectangle(1, 1, new Vector2(5, 0), 0);

//Make the 3 lists of vertices into a single body. This will make all of them move together.
Body body = BodyFactory.CreateCompoundPolygon(World, new List<Vertices>() { center, left, right }, 1, new Vector2());
body.BodyType = BodyType.Dynamic;

//Add the revolute joint to make the bodies rotate.
RevoluteJoint joint = new RevoluteJoint(staticAnchor, body, Vector2.Zero, Vector2.Zero);
World.AddJoint(joint);

//Apply some angular impulse to make the rotation start
body.ApplyAngularImpulse(25f);

------

See the code from above in this picture.