1
votes

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.

1

1 Answers

0
votes

I change my way how to do this and I don´t make every obstacle separately but I made class for obstacles(bordes) which looks something like this:

public Maze(World world, GameplayScreen screen, Vector2 position)
        {

            _agentBody = BodyFactory.CreateBody(world, position);
            _agentBody.BodyType = BodyType.Dynamic;
            _agentBody.IsStatic = true;
            _agentBody.Restitution = 0.2f;
            _agentBody.Friction = 0.2f;

            _offset = ConvertUnits.ToDisplayUnits(1f);
            _agentBody.CreateFixture(new PolygonShape(PolygonTools.CreateRectangle(1f, 0.05f, new Vector2(0f, 1f), 0), 1f));
            _agentBody.CreateFixture(new PolygonShape(PolygonTools.CreateRectangle(1f, 0.05f, new Vector2(0f, -1f), 0), 1f));
            _agentBody.CreateFixture(new PolygonShape(PolygonTools.CreateRectangle(0.05f, 1f, new Vector2(1f, 0f), 0), 1f));
            _agentBody.CreateFixture(new PolygonShape(PolygonTools.CreateRectangle(0.05f, 1f, new Vector2(-1f, 0f), 0), 1f));   
            _screen = screen;

            //GFX
        AssetCreator creator = _screen.ScreenManager.Assets;
        _box = new Sprite(creator.TextureFromVertices(PolygonTools.CreateRectangle(1f, 0.05f),
                                                       MaterialType.Blank, Color.White, 1f));

    }

Now I have problem with bouncing but that´s another thing and this question is solved.